Java基础之集合框架(三)--Map、HashMap、TreeMap

[java]  view plain copy
  1. /* 
  2. Map集合:该集合存储键值对。一对一对往里存。而且要保证键的唯一性。 
  3.     1,添加。 
  4.         put(K key, V value)  
  5.         putAll(Map<? extends K,? extends V> m)  
  6.  
  7.     2,删除。 
  8.         clear()  
  9.         remove(Object key)  
  10.  
  11.     3,判断。 
  12.         containsValue(Object value)  
  13.         containsKey(Object key)  
  14.         isEmpty()  
  15.  
  16.  
  17.     4,获取。 
  18.         get(Object key)  
  19.         size()  
  20.         values()  
  21.  
  22.         entrySet()  
  23.         keySet()  
  24.  
  25. Map 
  26.     |--Hashtable:底层是哈希表数据结构,不可以存入null键null值。该集合是线程同步的。jdk1.0.效率低。 
  27.     |--HashMap:底层是哈希表数据结构,允许使用 null 值和 null 键,该集合是不同步的。将hashtable替代,jdk1.2.效率高。 
  28.     |--TreeMap:底层是二叉树数据结构。线程不同步。可以用于给map集合中的键进行排序。 
  29.  
  30.  
  31. 和Set很像。 
  32. 其实大家,Set底层就是使用了Map集合。 
  33.  
  34.  
  35. */  
  36. import java.util.*;  
  37. class  MapDemo  
  38. {  
  39.     public static void main(String[] args)   
  40.     {  
  41.         Map<String,String> map = new HashMap<String,String>();  
  42.   
  43.         //添加元素,添加元素,如果出现添加时,相同的键。那么后添加的值会覆盖原有键对应值。  
  44.         //并put方法会返回被覆盖的值。  
  45.         System.out.println("put:"+map.put("01","zhangsan1"));  
  46.         System.out.println("put:"+map.put("01","wnagwu"));  
  47.         map.put("02","zhangsan2");  
  48.         map.put("03","zhangsan3");  
  49.   
  50.         System.out.println("containsKey:"+map.containsKey("022"));  
  51.         //System.out.println("remove:"+map.remove("02"));  
  52.   
  53.         System.out.println("get:"+map.get("023"));  
  54.   
  55.         map.put("04",null);  
  56.         System.out.println("get:"+map.get("04"));  
  57.         //可以通过get方法的返回值来判断一个键是否存在。通过返回null来判断。  
  58.   
  59.   
  60.           
  61.         //获取map集合中所有的值。  
  62.         Collection<String> coll = map.values();  
  63.   
  64.         System.out.println(coll);  
  65.         System.out.println(map);  
  66.   
  67.   
  68.     }  
  69. }  

[java]  view plain copy
  1. /* 
  2. map集合的两种取出方式: 
  3. 1,Set<k> keySet:将map中所有的键存入到Set集合。因为set具备迭代器。 
  4.     所有可以迭代方式取出所有的键,在根据get方法。获取每一个键对应的值。 
  5.          
  6.  
  7.     Map集合的取出原理:将map集合转成set集合。在通过迭代器取出。 
  8.  
  9.  
  10. 2,Set<Map.Entry<k,v>> entrySet:将map集合中的映射关系存入到了set集合中, 
  11.                 而这个关系的数据类型就是:Map.Entry 
  12.  
  13.                 Entry其实就是Map中的一个static内部接口。 
  14.                 为什么要定义在内部呢? 
  15.                 因为只有有了Map集合,有了键值对,才会有键值的映射关系。 
  16.                 关系属于Map集合中的一个内部事物。 
  17.                 而且该事物在直接访问Map集合中的元素。 
  18.  
  19. */  
  20.   
  21. import java.util.*;  
  22.   
  23.   
  24. class MapDemo2   
  25. {  
  26.     public static void main(String[] args)   
  27.     {  
  28.         Map<String,String> map = new HashMap<String,String>();  
  29.   
  30.         map.put("02","zhangsan2");  
  31.         map.put("03","zhangsan3");  
  32.         map.put("01","zhangsan1");  
  33.         map.put("04","zhangsan4");  
  34.   
  35.         //将Map集合中的映射关系取出。存入到Set集合中。  
  36.         Set<Map.Entry<String,String>> entrySet = map.entrySet();  
  37.   
  38.         Iterator<Map.Entry<String,String>> it = entrySet.iterator();  
  39.   
  40.         while(it.hasNext())  
  41.         {  
  42.             Map.Entry<String,String> me = it.next();  
  43.             String key = me.getKey();  
  44.             String value = me.getValue();  
  45.   
  46.             System.out.println(key+":"+value);  
  47.   
  48.         }  
  49.   
  50.         /* 
  51.         //先获取map集合的所有键的Set集合,keySet(); 
  52.         Set<String> keySet = map.keySet(); 
  53.  
  54.         //有了Set集合。就可以获取其迭代器。 
  55.         Iterator<String> it = keySet.iterator(); 
  56.  
  57.         while(it.hasNext()) 
  58.         { 
  59.             String key = it.next(); 
  60.             //有了键可以通过map集合的get方法获取其对应的值。 
  61.             String value  = map.get(key); 
  62.             System.out.println("key:"+key+",value:"+value); 
  63.         } 
  64.  
  65.         */  
  66.   
  67.     }  
  68. }  
  69.   
  70.   
  71. /* 
  72. Map.Entry 其实Entry也是一个接口,它是Map接口中的一个内部接口。 
  73.  
  74.  
  75.  
  76.  
  77. interface Map 
  78. { 
  79.     public static interface Entry 
  80.     { 
  81.         public abstract Object getKey(); 
  82.         public abstract Object getValue(); 
  83.  
  84.     } 
  85. } 
  86.  
  87. class HashMap implements Map 
  88. { 
  89.     class Hahs implements Map.Entry 
  90.     { 
  91.         public  Object getKey(){} 
  92.         public  Object getValue(){} 
  93.     } 
  94.      
  95. } 
  96. */  


keySet方法图例


entrySet方法图例:



[java]  view plain copy
  1. /* 
  2. map扩展知识。 
  3.  
  4. map集合被使用是因为具备映射关系。 
  5.  
  6. "yureban"   Student("01" "zhangsan"); 
  7.  
  8. "yureban" Student("02" "lisi"); 
  9.  
  10. "jiuyeban" "01" "wangwu"; 
  11. "jiuyeban" "02" "zhaoliu"; 
  12.  
  13. 一个学校有多个教室。每一个教室都有名称。 
  14.  
  15.  
  16. */  
  17. import java.util.*;  
  18.   
  19. class Student  
  20. {  
  21.     private String id;  
  22.     private String name;  
  23.     Student(String id,String name)  
  24.     {  
  25.         this.id = id;  
  26.         this.name = name;  
  27.     }  
  28.     public String toString()  
  29.     {  
  30.         return id+":::"+name;  
  31.     }  
  32. }  
  33. class  MapDemo3  
  34. {  
  35.   
  36.     public static void demo()  
  37.     {  
  38.         HashMap<String,List<Student>> czbk = new HashMap<String,List<Student>>();  
  39.   
  40.         List<Student> reyu = new ArrayList<Student>();  
  41.         List<Student> jiuye = new ArrayList<Student>();  
  42.   
  43.         czbk.put("yureban",reyu);  
  44.         czbk.put("jiuyeban",jiuye);  
  45.   
  46.         reyu.add(new Student("01","zhagnsa"));  
  47.         reyu.add(new Student("04","wangwu"));  
  48.         jiuye.add(new Student("01","zhouqi"));  
  49.         jiuye.add(new Student("02","zhaoli"));  
  50.   
  51.   
  52.         Iterator<String> it = czbk.keySet().iterator();  
  53.   
  54.         while(it.hasNext())  
  55.         {  
  56.             String roomName = it.next();  
  57.             List<Student> room = czbk.get(roomName);  
  58.               
  59.             System.out.println(roomName);  
  60.             getInfos(room);  
  61.         }  
  62.   
  63.           
  64.   
  65.       
  66.     }  
  67.     public static void getInfos(List<Student> list)  
  68.     {  
  69.         Iterator<Student> it = list.iterator();  
  70.         while(it.hasNext())  
  71.         {  
  72.             Student s = it.next();  
  73.             System.out.println(s);  
  74.         }  
  75.     }  
  76.   
  77.   
  78.   
  79.   
  80.     public static void main(String[] args)   
  81.     {  
  82.          demo();  
  83.           
  84.         /* 
  85.         HashMap<String,List<Student>> czbk = new HashMap<String,List<Student>>(); 
  86.  
  87.         HashMap<String,String> yure = new HashMap<String,String>(); 
  88.          
  89.         HashMap<String,String> jiuye = new HashMap<String,String>(); 
  90.  
  91.         czbk.put("yureban",yure); 
  92.         czbk.put("jiuyeban",jiuye); 
  93.  
  94.  
  95.         yure.put("01","zhagnsan"); 
  96.         yure.put("02","lisi"); 
  97.  
  98.         jiuye.put("01","zhaoliu"); 
  99.         jiuye.put("02","wangwu"); 
  100.  
  101.  
  102.  
  103.         //遍历czbk集合。获取所有的教室。 
  104.         Iterator<String> it = czbk.keySet().iterator(); 
  105.  
  106.         while(it.hasNext()) 
  107.         { 
  108.             String roomName = it.next(); 
  109.             HashMap<String,String> room = czbk.get(roomName); 
  110.              
  111.             System.out.println(roomName); 
  112.             getStudentInfo(room); 
  113.         } 
  114.  
  115.          
  116. //      getStudentInfo(jiuye); 
  117. //      getStudentInfo(yure); 
  118. */  
  119.           
  120.     }  
  121.     public static void getStudentInfo(HashMap<String,String> roomMap)  
  122.     {  
  123.         Iterator<String> it = roomMap.keySet().iterator();  
  124.   
  125.         while(it.hasNext())  
  126.         {  
  127.             String id = it.next();  
  128.             String name = roomMap.get(id);  
  129.             System.out.println(id+":"+name);  
  130.         }  
  131.     }  
  132. }  

Map相关练习:

[java]  view plain copy
  1. /* 
  2. 每一个学生都有对应的归属地。 
  3. 学生Student,地址String。 
  4. 学生属性:姓名,年龄。 
  5. 注意:姓名和年龄相同的视为同一个学生。 
  6. 保证学生的唯一性。 
  7.  
  8.  
  9.  
  10. 1,描述学生。 
  11.  
  12. 2,定义map容器。将学生作为键,地址作为值。存入。 
  13.  
  14. 3,获取map集合中的元素。 
  15.  
  16. */  
  17.   
  18. import java.util.*;  
  19. class Student implements Comparable<Student>  
  20. {  
  21.     private String name;  
  22.     private int age;  
  23.     Student(String name,int age)  
  24.     {  
  25.         this.name = name;  
  26.         this.age = age;  
  27.     }  
  28.       
  29.     public int compareTo(Student s)  
  30.     {  
  31.         int num = new Integer(this.age).compareTo(new Integer(s.age));  
  32.   
  33.         if(num==0)  
  34.             return this.name.compareTo(s.name);  
  35.         return num;  
  36.     }  
  37.   
  38.     public int hashCode()  
  39.     {  
  40.         return name.hashCode()+age*34;  
  41.     }  
  42.     public boolean equals(Object obj)  
  43.     {  
  44.         if(!(obj instanceof Student))  
  45.             throw new ClassCastException("类型不匹配");  
  46.   
  47.         Student s = (Student)obj;  
  48.   
  49.         return this.name.equals(s.name) && this.age==s.age;  
  50.           
  51.   
  52.     }  
  53.     public String getName()  
  54.     {  
  55.         return name;  
  56.     }  
  57.     public int getAge()  
  58.     {  
  59.         return age;  
  60.     }  
  61.     public String toString()  
  62.     {  
  63.         return name+":"+age;  
  64.     }  
  65. }  
  66.   
  67.   
  68.   
  69. class  MapTest  
  70. {  
  71.     public static void main(String[] args)   
  72.     {  
  73.         HashMap<Student,String> hm = new HashMap<Student,String>();  
  74.   
  75.         hm.put(new Student("lisi1",21),"beijing");  
  76.         hm.put(new Student("lisi1",21),"tianjin");  
  77.         hm.put(new Student("lisi2",22),"shanghai");  
  78.         hm.put(new Student("lisi3",23),"nanjing");  
  79.         hm.put(new Student("lisi4",24),"wuhan");  
  80.   
  81.         //第一种取出方式 keySet  
  82.   
  83.         Set<Student> keySet = hm.keySet();  
  84.   
  85.         Iterator<Student> it = keySet.iterator();  
  86.   
  87.         while(it.hasNext())  
  88.         {  
  89.             Student stu = it.next();  
  90.             String addr = hm.get(stu);  
  91.             System.out.println(stu+".."+addr);  
  92.         }  
  93.   
  94.   
  95.         //第二种取出方式 entrySet  
  96.         Set<Map.Entry<Student,String>> entrySet = hm.entrySet();  
  97.   
  98.         Iterator<Map.Entry<Student,String>> iter = entrySet.iterator();  
  99.           
  100.         while(iter.hasNext())  
  101.         {  
  102.             Map.Entry<Student,String> me = iter.next();  
  103.             Student stu = me.getKey();  
  104.             String addr = me.getValue();  
  105.             System.out.println(stu+"........."+addr);  
  106.         }  
  107.     }  
  108. }  

[java]  view plain copy
  1. /* 
  2. 需求:对学生对象的年龄进行升序排序。 
  3.  
  4. 因为数据是以键值对形式存在的。 
  5. 所以要使用可以排序的Map集合。TreeMap。 
  6. */  
  7. import java.util.*;  
  8.   
  9. class StuNameComparator implements Comparator<Student>  
  10. {  
  11.     public int compare(Student s1,Student s2)  
  12.     {  
  13.         int num = s1.getName().compareTo(s2.getName());  
  14.         if(num==0)  
  15.             return new Integer(s1.getAge()).compareTo(new Integer(s2.getAge()));  
  16.   
  17.         return num;  
  18.     }  
  19. }  
  20.   
  21.   
  22. class  MapTest2  
  23. {  
  24.     public static void main(String[] args)   
  25.     {  
  26.         TreeMap<Student,String> tm = new TreeMap<Student,String>(new StuNameComparator());  
  27.   
  28.         tm.put(new Student("blisi3",23),"nanjing");  
  29.         tm.put(new Student("lisi1",21),"beijing");  
  30.         tm.put(new Student("alisi4",24),"wuhan");  
  31.         tm.put(new Student("lisi1",21),"tianjin");  
  32.         tm.put(new Student("lisi2",22),"shanghai");  
  33.   
  34.           
  35.         Set<Map.Entry<Student,String>> entrySet = tm.entrySet();  
  36.   
  37.         Iterator<Map.Entry<Student,String>> it = entrySet.iterator();  
  38.   
  39.         while(it.hasNext())  
  40.         {  
  41.             Map.Entry<Student,String> me = it.next();  
  42.   
  43.             Student stu = me.getKey();  
  44.             String addr = me.getValue();  
  45.             System.out.println(stu+":::"+addr);  
  46.         }  
  47.     }  
  48. }  

[java]  view plain copy
  1. /* 
  2. 练习: 
  3. "sdfgzxcvasdfxcvdf"获取该字符串中的字母出现的次数。 
  4.  
  5. 希望打印结果:a(1)c(2)..... 
  6.  
  7. 通过结果发现,每一个字母都有对应的次数。 
  8. 说明字母和次数之间都有映射关系。 
  9.  
  10. 注意了,当发现有映射关系时,可以选择map集合。 
  11. 因为map集合中存放就是映射关系。 
  12.  
  13.  
  14. 什么使用map集合呢? 
  15. 当数据之间存在这映射关系时,就要先想map集合。 
  16.  
  17. 思路: 
  18. 1,将字符串转换成字符数组。因为要对每一个字母进行操作。 
  19.  
  20. 2,定义一个map集合,因为打印结果的字母有顺序,所以使用treemap集合。 
  21.  
  22. 3,遍历字符数组。 
  23.     将每一个字母作为键去查map集合。 
  24.     如果返回null,将该字母和1存入到map集合中。 
  25.     如果返回不是null,说明该字母在map集合已经存在并有对应次数。 
  26.     那么就获取该次数并进行自增。,然后将该字母和自增后的次数存入到map集合中。覆盖调用原理键所对应的值。 
  27.  
  28. 4,将map集合中的数据变成指定的字符串形式返回。 
  29.  
  30.  
  31.  
  32. */  
  33. import java.util.*;  
  34. class  MapTest3  
  35. {  
  36.     public static void main(String[] args)   
  37.     {  
  38.         String s= charCount("ak+abAf1c,dCkaAbc-defa");  
  39.         System.out.println(s);  
  40.     }  
  41.       
  42.     public static String charCount(String str)  
  43.     {  
  44.         char[] chs = str.toCharArray();  
  45.   
  46.         TreeMap<Character,Integer> tm = new TreeMap<Character,Integer>();  
  47.   
  48.           
  49.         int count = 0;  
  50.         for(int x=0; x<chs.length; x++)  
  51.         {  
  52.               
  53.   
  54.             if(!(chs[x]>='a' && chs[x]<='z' || chs[x]>='A' && chs[x]<='Z'))  
  55.                 continue;  
  56.   
  57.             Integer value = tm.get(chs[x]);  
  58.   
  59.               
  60.             if(value!=null)  
  61.                 count = value;  
  62.             count++;  
  63.             tm.put(chs[x],count);//直接往集合中存储字符和数字,为什么可以,因为自动装箱。  
  64.   
  65.             count = 0;  
  66.             /* 
  67.             if(value==null) 
  68.             { 
  69.                 tm.put(chs[x],1); 
  70.             } 
  71.             else 
  72.             { 
  73.                 value = value + 1; 
  74.                 tm.put(chs[x],value); 
  75.             } 
  76.             */  
  77.   
  78.   
  79.         }  
  80.   
  81.         //System.out.println(tm);  
  82.   
  83.         StringBuilder sb = new StringBuilder();  
  84.   
  85.         Set<Map.Entry<Character,Integer>> entrySet = tm.entrySet();  
  86.         Iterator<Map.Entry<Character,Integer>>  it = entrySet.iterator();  
  87.   
  88.         while(it.hasNext())  
  89.         {  
  90.             Map.Entry<Character,Integer> me = it.next();  
  91.             Character ch = me.getKey();  
  92.             Integer value = me.getValue();  
  93.             sb.append(ch+"("+value+")");  
  94.         }  
  95.   
  96.   
  97.   
  98.         return sb.toString();  
  99.     }  
  100.   
  101. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1.方法中的内部类能不能访问方法中的局部变量,为什么? 2.分析运行结果,说明原理。(没有分析结果不得分) import java.util.ArrayList; class Data { int val; } public class Test { public static void main(String args[]) { Data data = new Data(); ArrayList list = new ArrayList(); for (int i = 100; i < 103; i++) { data.val = i; list.add(data); } for (Data d : list) { System.out.println(d.val); } } } 3.编写一个类,在main方法中定义一个Map对象(采用泛型), * 加入若干个对象,然后遍历并打印出各元素的key和value。 4.编写一个类,在main方法中定义一个Map对象(采用泛型), * 加入若干个对象,然后遍历并打印出各元素的key和value。 5.定义一个文件输入流,调用read(byte[] b)方法将exercise.txt * 文件中的所有内容打印出来(byte数组的大小限制为5)。 6.存在一个JavaBean,它包含以下几种可能的属性: 1:boolean/Boolean 2:int/Integer 3:String 4:double/Double 属性未知,现在要给这些属性设置默认值,以下是要求的默认值: String类型的默认值为字符串 www.itheima.com int/Integer类型的默认值为100 boolean/Boolean类型的默认值为true double/Double的默认值为0.01D. 只需要设置带有getXxx/isXxx/setXxx方法的属性,非JavaBean属性不设置,请用代码实现 7.有一个类为ClassA,有一个类为ClassB,在ClassB中有一个方法b,此方法抛出异常, * 在ClassA类中有一个方法a,请在这个方法中调用b,然后抛出异常。 * 在客户端有一个类为TestC,有一个方法为c ,请在这个方法中捕捉异常的信息。 * 完成这个例子,请说出java中针对异常的处理机制。 8.编写一个程序,获取10个1至20的随机数,要求随机数不能重复。 9.编写一个程序,它先将键盘上输入的一个字符串转换成十进制整数, * 然后打印出这个十进制整数对应的二进制形式。 * 这个程序要考虑输入的字符串不能转换成一个十进制整数的情况, * 并对转换失败的原因要区分出是数字太大,还是其中包含有非数字字符的情况。 * 提示:十进制数转二进制数的方式是用这个数除以2, * 余数就是二进制数的最低位,接着再用得到的商作为被除数去除以2, * 这次得到的余数就是次低位,如此循环,直到被除数为0为止。 * 其实,只要明白了打印出一个十进制数的每一位的方式 * (不断除以10,得到的余数就分别是个位,十位,百位), * 就很容易理解十进制数转二进制数的这种方式。 10.有100个人围成一个圈,从1开始报数,报到14的这个人就要退出。然后其他人重新开始,从1报数,到14退出。 * 问:最后剩下的是100人中的第几个人?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值