集合—Map—Test

来源:互联网 发布:微信域名举报监控 编辑:程序博客网 时间:2024/06/13 21:10

 

/*每一个学生都有对应的归属地。学生Student,地址String 。学生属性:姓名,年龄。注意:姓名和年龄相同的视为同一个学生,保证学生的唯一性。1,描述学生。2,定义map容器。将学生作为键,地址作为值,存入。3,获取map集合中元素。*/import java.util.*;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)//实现接口,就必须复写compareTo方法,让学生具备自然顺序{int num = new Integer(this.age).compareTo(new Integer(s.age));if(num == 0)return this.name.compareTo(s.name);return num;}public int hashCode()//覆盖hashCode方法{return name.hashCode()+age*34;}public boolean equals(Object obj)//复写equals方法,姓名和年龄相同就是同一个人{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;}}class MapTest{public static void main(String[] args){HashMap<Student,String> hm = new HashMap<Student,String>();hm.put(new Student("lisi",21),"beijing");hm.put(new Student("lisi",21),"tianjin");hm.put(new Student("lisi2",22),"shanghai");hm.put(new Student("lisi3",23),"nanjing");hm.put(new Student("lisi4",24),"wuhu");/*//第一种取出方式,keySetSet<Student> keySet = hm.keySet();Iterator<Student> it = keySet.iterator();while(it.hasNext()){Student stu = it.next();String str = hm.get(stu);System.out.println(stu+"..."+str);} *///第二种取出方式,entrySetSet<Map.Entry<Student,String>> entrySet = hm.entrySet();Iterator<Map.Entry<Student,String>> it = entrySet.iterator();while(it.hasNext()){Map.Entry<Student,String> me = it.next();Student stu = me.getKey();String str = me.getValue();System.out.println(stu+"..."+str);}}}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/*需求:对学生对象的年龄进行升序排序。因为数据是以键值对形式存在的。所以要使用可以排序的Map集合。TreeMap*/import java.util.*;class StudentComparator implements Comparator<Student>{public int compare(Student s1, Student s2){int num = s1.getName().compareTo(s2.getName());if(num==0)return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));return num;}}class MapTest2{public static void main(String[] args){TreeMap<Student,String>tm = new TreeMap<Student,String>(new StudentComparator());tm.put(new Student("blisi3",23),"nanjing");tm.put(new Student("lisi1",21),"beijing");tm.put(new Student("alisi4",24),"wuhu");//tm.put(new Student("lisi",21),"tianjin");tm.put(new Student("lisi2",22),"shanghai");Set<Map.Entry<Student,String>> entrySet = tm.entrySet();Iterator<Map.Entry<Student,String>> it = entrySet.iterator();while(it.hasNext()){Map.Entry<Student,String> me = it.next();Student stu = me.getKey();String str = me.getValue();System.out.println(stu+"..."+str);}}}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/*练习:"sdfgzxcvasdfxcvdf"获取该字符串中的字母出现的次数。希望打印结果:a(1)c(2)....通过结果发现,每一个字母都有对应的次数。说明字母和次数之间都有映射关系。注意:当发现有映射关系时,可以选择map集合。以为map集合中存放就是映射关系。什么时候使用map集合呢?当数据之间存在着映射关系时,就要想到map集合。思路:1,将字符串转换成字符数组。因为要对每一个字母进行操作。2,定义一个map集合,因为打印结果的字母有顺序,所以使用treemap集合。3,遍历字符数组。将每一个字母作为键去查map集合。如果返回null,将 该字母和1存入到map集合中。如果返回不是null,说明该字母在map集合 中已经存在并由对应次数。那么就获取该次数并进行自增。然后将该字 母和自增后的次数存入到map集合中。覆盖原来键所对应的值。4,将map集合中的数据变成指定的字符串形式返回。 */import java.util.*;class MapTest3{public static void main(String[] args){String s = charCount("sdf+gz,xcv-asdfxcvdf");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++;tm.put(chs[x],count);count = 0;//用过要清零/*if(value==null)//集合中没有{tm.put(chs[x],1);}else//集合中已经存在{value = value+1;tm.put(chs[x],value);}*/}//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+")");//取出后存入到StringBuilder}return sb.toString();}}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/*map集合扩展知识。map集合被使用是因为具备映射关系一个学校多个教室。每个教室有多个学生。*/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 MapDemo3{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("04","lisi"));jiuye.add(new Student("01","zhouqi"));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 main(String[] args){demo();/*HashMap<String,HashMap<String,String>> czbk = new HashMap<String,HashMap<String,String>>();HashMap<String,String> yure = new HashMap<String,String>();HashMap<String,String> jiuye = new HashMap<String,String>();czbk.put("yureban",yure);czbk.put("jiuyeban",jiuye);yure.put("01","zhangsan");yure.put("02","lisi");jiuye.put("01","zhaoliu");jiuye.put("02","wangwu");//遍历czbk集合,获取所有的教室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);//输出学生信息}*/}public static void getStudentInfo(HashMap<String,String> room)//传入学校和班级,获取学生信息{Iterator<String> it = room.keySet().iterator();while(it.hasNext()){String id = it.next();//获取键。String name = room.get(id);//通过键获取值System.out.println(id+": "+name);}}}

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

个人总结:
 

原创粉丝点击