java学习--集合框架(3)

01)Map(概述)

  1. /*  
  2.  * Map<K, V>:该集合存储键值对。一对一对的往里存,而且要保证键值的唯一性。 
  3.  *      1:添加: 
  4.  *          put(K key, V value): 
  5.  *          putAll(Map<? extends K,? extends V> m): 
  6.  *      2:删除: 
  7.  *          clear(): 
  8.  *          remove(Object key): 
  9.  *      3:判断: 
  10.  *          containsKey(Object key): 
  11.  *          containsValue(Object value): 
  12.  *          isEmpty(): 
  13.  *      4:获取: 
  14.  *          get(Object key): 
  15.  *          size(): 
  16.  *          values(): 
  17.  *          **entrySet(): 
  18.  *          **keySet(): 
  19.  *  
  20.  * |---Map 
  21.  *      |---Hashtable 
  22.  *      |---HashMatp 
  23.  *      |---TreeMap 
  24.  */  

02)Map子类对象的特点

  1. /* 
  2.  * |---Map 
  3.  *      |---Hashtable:底层是哈希表数据结构,不可以存入Null值和Null键。该集合是线程同步的。jdk1.0:效率低 
  4.  *          为了成功地在哈希表中存储和获取对象,用作键的对象必须实现 hashCode 方法和 equals 方法。 
  5.  *      |---HashMap:底层是哈希表数据结构,并允许使用Null值和Null键。该集合是线程不同步的。jdk1.2:效率高 
  6.  *      |---TreeMap:底层是二叉树数据结构,该集合是线程不同步的。可以用于Map集合中键进行排序。 
  7.  *  
  8.  * Map和Set很像。其实Set底层使用的就是Map集合。 
  9.  *  
  10.  */  

03)Map——共性方法

  1. /* 
  2.  * Map——共性方法。 
  3.  */  
  4. public class Map03 {  
  5.     public static void sop(Object obj){  
  6.         System.out.println(obj);  
  7.     }  
  8.     public static void main(String[] args) {  
  9.         method_1();  
  10.         method_2();  
  11.     }  
  12.     public static void method_1(){  
  13.         Map<String, String> map = new HashMap<String, String>();  
  14.         //添加元素。  
  15.         map.put("01""汤姆一号");  
  16.         map.put("02""汤姆二号");  
  17.         map.put("03""汤姆三号");  
  18.         map.put("05""汤姆四号");  
  19.         map.put("06""汤姆五号");  
  20.         sop("containsKey = " + map.containsKey("02"));  
  21.         sop("(04)remove = " + map.remove("04"));  
  22.         sop("(05)get = " + map.get("05"));  
  23.         sop("(04)get = " + map.get("04"));  
  24.         sop(map.put("07""汤姆七号"));  
  25.         sop("Map " + map);  
  26.         System.out.println("--------------------------------------------------------------");  
  27.     }  
  28.     public static void method_2(){  
  29.         Map<String, String> map = new HashMap<String, String>();  
  30.         //添加元素。  
  31.         map.put("01""汤姆一号");  
  32.         map.put("02""汤姆二号");  
  33.         map.put("03""汤姆三号");  
  34.         map.put("05""汤姆四号");  
  35.         map.put("06""汤姆五号");  
  36.           
  37.         //获取集合中的所以元素。  
  38.         Collection<String> coll = map.values();  
  39.         sop("coll " + coll);  
  40.         sop("Map " + map);  
  41.         //添加元素,如果出现添加相同的键,那么后添加的值会覆盖原有的键所对应的值。  
  42.         //put方法会返回覆盖的值。  
  43.         sop("(06)Put = " + map.put("06""错误汤姆编号"));//后加值将替代原值。  
  44.         sop("(06)(null)Put = " + map.put("06"null));  
  45.         sop("Map " + map);  
  46.         System.out.println("--------------------------------------------------------------");  
  47.     }  
  48. }  
运行结果如下图所示:

04)Map——keySet

  1. /* 
  2.  * Map集合的两种取出方式: 
  3.  * Map集合的取出原理:将map集合转成set集合。再通过迭代器取出。 
  4.  * 1:Set<K> keySet:将Map中所有的键存入到Set集合中,因为Set具备迭代器,所以可以通过迭代方式取出所有的键。 
  5.  *              再根据get方法,获取每一个键的值。 
  6.  * 2:Set<Map.Entry<K, V>> entrySet:将map集合中的映射关系存入到了set集合中。 
  7.  *          而这个关系的数据类型就是:Map.Entry。 
  8.  */  
  9. public class MapkeySetDemo {  
  10.     public static void sop(Object obj){  
  11.         System.out.println(obj);  
  12.     }  
  13.     public static void main(String[] args) {  
  14.         ks_method();  
  15.     }  
  16.     public static void ks_method(){  
  17.         Map<String, String> map = new HashMap<String, String>();  
  18.         //添加元素。  
  19.         map.put("01""汤姆一号");  
  20.         map.put("02""汤姆二号");  
  21.         map.put("03""汤姆三号");  
  22.         map.put("05""汤姆五号");  
  23.         map.put("06""汤姆六号");  
  24.           
  25.         //先获取Map集合中所有键的Set集合。keySet()方法。  
  26.         Set<String> keySet = map.keySet();  
  27.         //通过set集合,获取其迭代器。  
  28.         for (Iterator<String> it = keySet.iterator(); it.hasNext();){  
  29.             String key = it.next();  
  30.             //键被获取后,可以通过Map集合的get(key)方法获取其对应的值。  
  31.             sop("(键)key = " + key);  
  32.             String value = map.get(key);  
  33.             sop("(值) = " + value);  
  34.         }  
  35.     }  
  36. }  
运行结果如下图所示:

05)Map——entrySet

  1. /*  
  2.  * Map集合的两种取出方式: 
  3.  * 1:Set<K> keySet: 
  4.  * 2:Set<Map.Entry<K, V>> entrySet:将map集合中的映射关系存入到了set集合中。 
  5.  *          而这个关系的数据类型就是:Map.Entry。 
  6.  *      K getKey():返回与此项对应的键。 
  7.  *      V getValue():返回与此项对应的值。 
  8.  */  
  9. public class MapentrySetDemo {  
  10.     public static void sop(Object obj){  
  11.         System.out.println(obj);  
  12.     }  
  13.     public static void main(String[] args) {  
  14.         ets_method();  
  15.     }  
  16.     public static void ets_method(){  
  17.         Map<Integer, String> map = new HashMap<Integer, String>();  
  18.         map.put(01"汤姆一号");  
  19.         map.put(02"汤姆二号");  
  20.         map.put(03"汤姆三号");  
  21.         map.put(05"汤姆五号");  
  22.         map.put(06"汤姆六号");  
  23.           
  24.         //将map集合中的映射关系取出。存入到Set集合中。  
  25.         Set<Map.Entry<Integer, String>> entrySet = map.entrySet();  
  26.         for (Iterator<Map.Entry<Integer, String>> it = entrySet.iterator(); it.hasNext();){  
  27.             Map.Entry<Integer, String> me = it.next();  
  28.             Integer key = me.getKey();  
  29.             String value = me.getValue();  
  30.             sop(key + " = " + value);  
  31.         }  
  32.     }  
  33. }  
  34.   
  35.   
  36. //(Map.Entry)的真相示例。  
  37. //interface Map{  
  38. //  public static interface Entry{  
  39. //      public abstract Object getKey();  
  40. //      public abstract Object getValue();  
  41. //  }  
  42. //}  
  43. //class HashMap implements Map{  
  44. //  class XX implements Entry{  
  45. //      public abstract Object getKey(){  
  46. //            
  47. //      }  
  48. //      public abstract Object getValue(){  
  49. //            
  50. //      }  
  51. //  }  
  52. //}  
运行结果如下图所示:

06)Map——HashSet(练习)

  1. /* 
  2.  * 每一个学生都有对应的归属地。 
  3.  * (学生)Student,(地址)String。 
  4.  * 学生属性:(姓名),(年龄)。 
  5.  * 注意:姓名和年龄相同的视为同一个学生。 
  6.  * 保证学生的唯一性。 
  7.  *  
  8.  * 1:描述学生。 
  9.  * 2:定义map容器。将学生作为键,地址作为值。存入。 
  10.  * 3:获取map集合中的元素。 
  11.  */  
  12. public class HashMapTest {//使用HashMap  
  13.     public static void sop(Object obj){  
  14.         System.out.println(obj);  
  15.     }  
  16.     public static void main(String[] args) {  
  17.         method();  
  18.     }  
  19.     public static void method(){  
  20.         HashMap<Student, String> hm = new HashMap<Student, String>();  
  21.         hm.put(new Student("汤姆一号"21), "金星");  
  22.         hm.put(new Student("汤姆二号"22), "木星");  
  23.         hm.put(new Student("汤姆三号"23), "水星");  
  24.         hm.put(new Student("汤姆四号"24), "火星");  
  25.         hm.put(new Student("汤姆五号"25), "土星");  
  26.         hm.put(new Student("汤姆二号"22), "月球");  
  27.         hm.put(new Student("汤姆三号"23), "土星");  
  28.           
  29.         //第一种取出方式 keySet  
  30.         Set<Student> keySet = hm.keySet();  
  31.         for (Student it : keySet){//it等于it.next()。  
  32.             sop(it + " " + hm.get(it));  
  33.         }  
  34.         System.out.println("------------------------");  
  35.         //第二种取出方式 entrySet  
  36.         Set<Map.Entry<Student, String>> entrySet = hm.entrySet();  
  37.         for (Map.Entry<Student, String> entry: entrySet){  
  38.             sop(entry.getKey() + " = " + entry.getValue());  
  39.         }  
  40.     }  
  41. }  
  42.   
  43. class Student implements Comparable<Student>{  
  44.     private String name;  
  45.     private int age;  
  46.     Student(String name, int age){  
  47.         this.name = name;  
  48.         this.age = age;  
  49.     }  
  50.     public int compareTo(Student s){//覆盖compareTo方法。  
  51.         int num = new Integer(this.age).compareTo(new Integer(s.age));  
  52.         if (num == 0)  
  53.             return this.name.compareTo(s.name);  
  54.         return num;  
  55.     }  
  56.     public int hashCode(){  
  57.         return name.hashCode() + age * age;  
  58.     }  
  59.     public boolean equals(Object obj){  
  60.         if (!(obj instanceof Student))  
  61.             throw new ClassCastException("类型不匹配");  
  62.         Student s = (Student)obj;  
  63.         return this.name.equals(s.name) && this.age == s.age;  
  64.     }  
  65.       
  66.     public String getName(){  
  67.         return name;  
  68.     }  
  69. //  public void setName(String name){  
  70. //      this.name = name;  
  71. //  }  
  72.     public int getAge(){  
  73.         return age;  
  74.     }  
  75. //  public void setAge(int age){  
  76. //      this.age = age;  
  77. //  }  
  78.     public String toString(){  
  79.         return name + ":" + age;  
  80.     }  
  81. }  
运行结果如下图所示:

07)Map——TreeMap(练习)

  1. /*  
  2.  * 需求:对学生对象的年龄进行升序排序。 
  3.  */  
  4. public class TreeMapTest {//使用TreeMap。  
  5.     public static void sop(Object obj){  
  6.         System.out.println(obj);  
  7.     }  
  8.     public static void main(String[] args) {  
  9.         method();  
  10.     }  
  11.     public static void method(){  
  12.         TreeMap<Student_T_P, String> tm = new TreeMap<Student_T_P, String>(new MyCom());  
  13.         tm.put(new Student_T_P("汤姆七号"27), "火星");  
  14.         tm.put(new Student_T_P("汤姆五号"25), "土星");  
  15.         tm.put(new Student_T_P("汤姆二号"22), "木星");//"木星"被"月球"覆盖。  
  16.         tm.put(new Student_T_P("汤姆三号"23), "水星");//"水星"被"土星"覆盖。  
  17.         tm.put(new Student_T_P("汤姆四号"24), "火星");  
  18.         tm.put(new Student_T_P("汤姆二号"22), "月球");  
  19.         tm.put(new Student_T_P("汤姆三号"22), "土星");  
  20.         tm.put(new Student_T_P("汤姆一号"21), "金星");  
  21.         tm.put(new Student_T_P("汤姆一号"26), "金星");  
  22.         //第一种取出方式 keySet  
  23.         Set<Student_T_P> keySet = tm.keySet();  
  24.         for (Student_T_P stu : keySet){//it等于it.next()。  
  25.             sop(stu + "(age)" + tm.get(stu));  
  26.         }  
  27.     }  
  28. }  
  29.   
  30. class Student_T_P implements Comparable<Student_T_P>{  
  31.     private String name;  
  32.     private int age;  
  33.     Student_T_P(String name, int age){  
  34.         this.name = name;  
  35.         this.age = age;  
  36.     }  
  37.     public int compareTo(Student_T_P s){  
  38.         int num = new Integer(this.age).compareTo(new Integer(s.age));  
  39.         if (num == 0)  
  40.             return this.name.compareTo(s.name);  
  41.         return num;  
  42.     }  
  43.     public int hashCode(){  
  44.         return name.hashCode() + age * age;  
  45.     }  
  46.     public boolean equals(Object obj){  
  47.         if (!(obj instanceof Student_T_P))  
  48.             throw new ClassCastException("类型不匹配");  
  49.         Student_T_P s = (Student_T_P)obj;  
  50.         return this.name.equals(s.name) && this.age == s.age;  
  51.     }  
  52.     public String getName(){  
  53.         return name;  
  54.     }  
  55.     public void setName(String name){  
  56.         this.name = name;  
  57.     }  
  58.     public int getAge(){  
  59.         return age;  
  60.     }  
  61.     public void setAge(int age){  
  62.         this.age = age;  
  63.     }  
  64.     public String toString(){  
  65.         return name + " = " + age;  
  66.     }  
  67. }  
  68. class MyCom implements Comparator{  
  69.     public int compare(Object o1, Object o2){  
  70.         Student_T_P stu1 = (Student_T_P)o1;  
  71.         Student_T_P stu2 = (Student_T_P)o2;  
  72.         //按age排序。  
  73.         int num = new Integer(stu1.getAge()).compareTo(new Integer(stu2.getAge()));  
  74.         if (num == 0)  
  75.             return stu1.getName().compareTo(stu2.getName());  
  76.         return num;  
  77.     }  
  78. }  
运行结果如下图所示:

08)字母出现的次数(TreeSet)

  1. /* 
  2.  * "afdhsafklhasdlweoradlfkj"获取该字符串中每个字母出现的次数。 
  3.  * 预期打印结果格式为:a(?)b(?)... 
  4.  *  
  5.  * 思路: 
  6.  * 1:将字符串转换成字符数组。因为要对每一个字母进行操作。 
  7.  * 2:定义一个map集合,因为打印结果的字母有顺序,所以使用treemap集合。 
  8.  * 3:遍历字符数组。 
  9.  *      将每一个字母作为键去查map集合。 
  10.  *      如果返回null,将该字母和1存入到map集合中。 
  11.  *      如果返回不是null,说明该字母在map集合已经存在并有对应次数。 
  12.  *      那么就获取该次数并进行自增。,然后将该字母和自增后的次数存入到map集合中。覆盖调用原理键所对应的值。 
  13.  * 4:将map集合中的数据变成指定的字符串形式返回。 
  14.  */  
  15. public class TreeMapCountTest {  
  16.     public static void sop(Object obj){  
  17.         System.out.println(obj);  
  18.     }  
  19.     public static void main(String[] args) {  
  20.         sop(method("afdhsb3afafkl>c-kkfahasdsdg=l,weorafdgsd&l5fkj"));  
  21.     }  
  22.     public static String method(String s){  
  23.         //将字串符转换成字符数组。  
  24.         char[] ch = s.toCharArray();  
  25.         TreeMap<Character, Integer> tm = new TreeMap<Character, Integer>();  
  26.         int count = 0;  
  27.         for (int i = 0; i < ch.length; i++){  
  28.             if(!(ch[i]>='a' && ch[i]<='z' || ch[i]>='A' && ch[i]<='Z'))//如果键不是在a~z之间。  
  29.                 continue;//退出本次循环。  
  30.             Integer value = tm.get(ch[i]);//通过键获取值。  
  31.             if (value != null)//如果有值。  
  32.                 count = value;  
  33.             count++;//将计数器加1。  
  34.             tm.put(ch[i], count);//将键与值一起存入。  
  35.             count = 0;//清零。  
  36.         }  
  37.           
  38.         StringBuilder sb = new StringBuilder();  
  39.         Set<Map.Entry<Character, Integer>> entrySet = tm.entrySet();  
  40.         for(Map.Entry<Character, Integer> key : entrySet){  
  41.             sb.append(key.getKey() + "(" + key.getValue() + ")");//添加键与值。  
  42.         }  
  43.         return sb.toString();  
  44.     }  
  45. }  
运行结果如下图所示:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值