一、集合—— Map 概述
import java.util.*;
class MapDemo
{
public static void main(String[] args)
{
Map<String,String> map = new HashMap<String,String>();
//添加元素
sop("put: "+map.put("01","zhangsan1"));//返回 null
sop("put: "+map.put("01","wangwu"));//返回zhangsan1,同时被wangwu覆盖。
map.put("02","zhangsan2");
map.put("03","zhangsan3");
map.put(null,"zhangsan4");//打印结果 null=zhangsan4
map.put("05",null);//打印结果 05=null
sop(map);//{null=zhangsan4, 05=null, 01=wangwu, 02=zhangsan2, 03=zhangsan3}
sop("containsKey: "+map.containsKey("02"));//判断该键是否存在 true
sop("containsValue: "+map.containsValue("zhangsan1"));//判断该值是否存在 false
//map.clear();//清空集合
sop("get: "+map.get("02"));//根据键获取值,也可用于判断该键是否存在。get: zhangsan2
sop("remove: "+map.remove("02"));//删除该键所在的元素,remove: zhangsan2
Collection<String> coll = map.values();
sop(coll);//[zhangsan4, null, wangwu, zhangsan3]
}
public static void sop(Object obj)
{
System.out.println(obj);
}
}
import java.util.*;
class MapDemo2
{
public static void main(String[] args)
{
Map<String,String> map = new HashMap<String,String>();
map.put("02","zhangsan2");
map.put("01","zhangsan1");
map.put("03","zhangsan3");
map.put("04","zhangsan4");
//先获取Map集合中所有键的集合,keySet();
Set<String> ks = map.keySet();
Iterator<String> it = ks.iterator();//此时ks是一个Set集合
while (it.hasNext())
{
String key = it.next();//获取Set集合中的元素,获取key
String value = map.get(key);//通过key获取value
System.out.println(key+" : "+value);
}
}
}
interface Map
{
public static interface Entry //接口中可以定义内部接口
{
public abstract Object getKey();
public abstract Object getValue();
}
}
class HashMap implements Map
{
class Haha implements Map.Entry
{
public Object getKey(){}
public Object getValue(){}
}
}
import java.util.*;
class MapDemo3
{
public static void main(String[] args)
{
Map<String,String> map = new HashMap<String,String>();
map.put("02","zhangsan2");
map.put("01","zhangsan1");
map.put("03","zhangsan3");
map.put("04","zhangsan4");
//将Map集合中的映射关系取出,存入到Set集合中
Set<Map.Entry<String,String>> ent = map.entrySet();
Iterator<Map.Entry<String,String>> it = ent.iterator();//迭代Set集合
while(it.hasNext())
{
Map.Entry<String,String> me = it.next();//获取Set集合中的元素
String key = me.getKey();//调用该元素的方法获取键
String value = me.getValue();//调用该元素的方法获取值
System.out.println(key+"...."+value);
}
}
}
import java.util.*;
class MapTest
{
public static void main(String[] args)
{
HashMap<Student,String> hm = new HashMap<Student,String>();
hm.put(new Student("lisi1",21),"beijing");
hm.put(new Student("lisi1",21),"tianjing");
hm.put(new Student("lisi2",22),"shanghai");
hm.put(new Student("lisi3",23),"nanjing");
hm.put(new Student("lisi4",24),"wuhan");
Set<Student> keyset = hm.keySet();
Iterator<Student> it = keyset.iterator();
while (it.hasNext())
{
Student s = it.next();
String addr = hm.get(s);
System.out.println(s+"..."+addr);
}
Set<Map.Entry<Student,String>> ent = hm.entrySet();
Iterator<Map.Entry<Student,String>> iter = ent.iterator();
while (iter.hasNext())
{
Map.Entry<Student,String> me = iter.next();
Student s = me.getKey();
String addr = me.getValue();
System.out.println(s+"......."+addr);
}
}
}
class Student implements Comparable<Student>
{
private String name;
private int age;
Student(String name, int age)
{
this.name = name;
this.age = age;
}
public int compareTo(Student s)
{
int num = this.age-s.age;
if (num==0)
{
return this.name.compareTo(s.name);
}
return num;
}
public int hashCode()
{
return name.hashCode()+age*37;
}
public boolean equals(Object obj)
{
if (!(obj instanceof Student))
throw new ClassCastException("不是学生");
Student s = (Student)obj;
return this.name.equals(s.name) && this.age==s.age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String toString()
{
return name+": "+age;
}
}
import java.util.*;
class MapTest2
{
public static void main(String[] args)
{
TreeMap<Student,String> tm = new TreeMap<Student,String>(new StuAgeCom());
tm.put(new Student("lisi7",21),"beijing");
tm.put(new Student("lisi3",23),"nanjing");
tm.put(new Student("lisi1",24),"wuhan");
tm.put(new Student("lisi2",22),"shanghai");
Set<Map.Entry<Student,String>> ent = tm.entrySet();//获取Set集合
Iterator<Map.Entry<Student,String>> it = ent.iterator();//迭代
while(it.hasNext())
{
Map.Entry<Student,String> me = it.next();//获取集合中的元素
Student stu = me.getKey();
String addr = me.getValue();
System.out.println(stu+"...."+addr);
}
}
}
class StuAgeCom implements Comparator<Student>
{
public int compare(Student s1, Student s2)
{
int num = new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));
//int num = s1.getAge() - s2.getAge();
if(num==0)
return s1.getName().compareTo(s2.getName());
return num;
}
}
import java.util.*;
class MapTest3
{
public static void main(String[] args)
{
System.out.println('a'-'c');
String str = "aapklabbccc++--..cdddd";
String s = charCount(str);
System.out.println(s);
}
public static String charCount(String str)
{
char[] chs = str.toCharArray();//字符串变数组
TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();//创建集合
int count = 0;
for (int x=0 ;x<chs.length ;x++ )//遍历数组
{
if(!(chs[x]>='a' && chs[x]<='z' || chs[x]>='A' && chs[x]<='Z'))
continue;
Integer value = tm.get(chs[x]);
if(value!=null)
count = value;
count = count+1;
tm.put(chs[x],count);
count = 0;
}
//System.out.println(tm);
StringBuilder sb = new StringBuilder();
Set<Map.Entry<Character,Integer>> entryset = tm.entrySet();
Iterator<Map.Entry<Character,Integer>> it = entryset.iterator();
while(it.hasNext())
{
Map.Entry<Character,Integer> me = it.next();
Character ch = me.getKey();
Integer value = me.getValue();
sb.append(ch+"("+value+") ");
}
return sb.toString();
}
}
import java.util.*;
class Student
{
private String id;
private String name;
Student(String id, String name)
{
this.id = id;
this.name = name;
}
public String toString()
{
return id+"::"+name ;
}
}
class MapDemo4
{
//主函数
public static void main(String[] args)
{
demo();
//demo_2();
}
//方式一:
public static void demo()
{
//创建一个学校集合
HashMap<String,List<Student>> czbk = new HashMap<String,List<Student>>();
//创建两个班级
List<Student> yure = new ArrayList<Student>();
List<Student> jiuye = new ArrayList<Student>();
//将班级加入学校
czbk.put("yureban",yure);
czbk.put("jiuyeban",jiuye);
//每个班级中添加学生对象
yure.add(new Student("01","zhangsan"));
yure.add(new Student("02","lisi"));
jiuye.add(new Student("01","wangwu"));
jiuye.add(new Student("02","zhaoliu"));
//迭代这个学校集合,取出每一个班级集合
Iterator<String> it = czbk.keySet().iterator();
while(it.hasNext())
{
String roomName = it.next();
List<Student> room = czbk.get(roomName);
System.out.println(roomName);
//调用迭代班级中学生的方法
getInfos(room);
}
}
//定义一个方法,用于迭代取出班级中的每一个学生对象
public static void getInfos(List<Student> list)
{
Iterator<Student> it = list.iterator();
while(it.hasNext())
{
Student s = it.next();
System.out.println(s);
}
}
//方式二:
public static void demo_2()
{
HashMap<String,HashMap<String,String>> czbk = new HashMap<String,HashMap<String,String>>();//创建一个学校集合
HashMap<String,String> yure = new HashMap<String,String>();//创建一个班级yure
HashMap<String,String> jiuye = new HashMap<String,String>();//创建一个班级jiuye
//将班级加入学校
czbk.put("yureban",yure);
czbk.put("jiuyeban",jiuye);
//将学生的id和name作为键值对传入班级集合中
yure.put("01","zhangsan");
yure.put("02","lisi");
jiuye.put("01","wangwu");
jiuye.put("02","zhaoliu");
//迭代这个学校集合,取出每一个班级
Iterator<String> it = czbk.keySet().iterator();
while(it.hasNext())
{
String roomName = it.next();
HashMap<String,String> room = czbk.get(roomName);
System.out.println(roomName);
//调用取出学生的方法。
getStudentInfo(room);
}
}
//定义一个方法,用于取出班级中的学生id和name
public static void getStudentInfo(HashMap<String,String> roomMap)
{
Iterator<String> it = roomMap.keySet().iterator();
while(it.hasNext())
{
String id = it.next();
String name = roomMap.get(id);
System.out.println(id+"..."+name);
}
}
}