码迷,mamicode.com
首页 > 编程语言 > 详细

JAVA学习--集合Map的使用

时间:2015-01-31 00:02:42      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:

* Map接口
 *         |-----HashMap:Map的主要实现类
 *         |-----LinkedHashMap:使用链表维护添加进Map中的顺序。故遍历Map时,是按添加的顺序遍历的。
 *         |-----TreeMap:按照添加进Map中的元素的key的指定属性进行排序。要求:key必须是同一个类的对象!
 *                 针对key:自然排序   vs 定制排序
 *         |-----Hashtable:古老的实现类,线程安全,不建议使用。
 *             |----Properties:常用来处理属性文件。键和值都为String类型的




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

     Object put(Object key,Object value):向Map中添加一个元素
      Object remove(Object key):按照指定的key删除此key-value
      void putAll(Map t)
      void clear():清空
      Object get(Object key):获取指定key的value值。若无此key,则返回null
      boolean containsKey(Object  key)
      boolean containsValue(Object value)
      int size():返回集合的长度
      boolean isEmpty() boolean equals(Object obj)
      HashMap: 1.key是用Set来存放的,不可重复。value是用Collection来存放的,可重复
     一个key-value对,是一个Entry。所有的Entry是用Set存放的,也是不可重复的。
     2.向HashMap中添加元素时,会调用key所在类的equals()方法,判断两个key是否相同。若相同 则只能添加 进后添加的那个元素。
 
 1 @Test
 2     public void test1() {
 3         Map map = new HashMap();
 4         map.put("AA", 213);
 5         map.put("BB", 456);
 6         map.put("BB", 45);
 7         map.put(123, "CC");
 8         map.put(null, null);
 9         map.put(new Person("DD", 23), 89);
10         map.put(new Person("DD", 23), 87);
11         System.out.println(map.size());
12         System.out.println(map);
13         map.remove("BB");
14         System.out.println(map);
15         Object value = map.get(1234);
16         System.out.println(value);
17     }

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

    * 如何遍历Map Set keySet() Collection values() Set entrySet()
 
 1 @Test
 2     public void test2() {
 3         Map map = new HashMap();
 4         map.put("AA", 213);
 5         map.put("BB", 45);
 6         map.put(123, "CC");
 7         map.put(null, null);
 8         map.put(new Person("DD", 23), 89);
 9 
10         // 1.遍历key集。
11         Set set = map.keySet();
12         for (Object obj : set) {
13             System.out.println(obj);
14         }
15         // 2.遍历value集
16         Collection values = map.values();
17         Iterator i = values.iterator();
18         while (i.hasNext()) {
19             System.out.println(i.next());
20         }
21         // 3.如何遍历key-value对。
22         // 方式一:
23         Set set1 = map.keySet();
24         for (Object obj : set1) {
25             System.out.println(obj + "----->" + map.get(obj));
26         }
27         // 方式二:
28         Set set2 = map.entrySet();
29         for (Object obj : set2) {
30             Map.Entry entry = (Map.Entry) obj;
31             // System.out.println(entry.getKey() + "---->" + entry.getValue());
32             System.out.println(entry);
33         }
34     }

------------------------------------------------------------------------------------------------------------
**LinkedHashMap用法

 

 1 @Test
 2     public void test3() {
 3         Map map = new LinkedHashMap();
 4         map.put("AA", 213);
 5         map.put("BB", 45);
 6         map.put(123, "CC");
 7         map.put(null, null);
 8         map.put(new Person("DD", 23), 89);
 9 
10         Set set1 = map.keySet();
11         for (Object obj : set1) {
12             System.out.println(obj + "----->" + map.get(obj));
13         }
14     }
 1 // 自然排序
 2     @Test
 3     public void test4() {
 4         Map map = new TreeMap();
 5         map.put(new Person("AA", 23), 89);
 6         map.put(new Person("MM", 22), 79);
 7         map.put(new Person("GG", 23), 99);
 8         map.put(new Person("JJ", 13), 69);
 9 
10         Set set1 = map.keySet();
11         for (Object obj : set1) {
12             System.out.println(obj + "----->" + map.get(obj));
13         }
14     }
 1  // 定制排序
 2     @Test
 3     public void test5() {
 4         Comparator com = new Comparator() {
 5             public int compare(Object o1, Object o2) {
 6                 if (o1 instanceof Customer && o2 instanceof Customer) {
 7                     Customer c1 = (Customer) o1;
 8                     Customer c2 = (Customer) o2;
 9                     int i = c1.getId().compareTo(c2.getId());
10                     if (i == 0) {
11                         return c1.getName().compareTo(c2.getName());
12                     }
13                     return i;
14                 }
15                 return 0;
16             }
17         };
18         TreeMap map = new TreeMap(com);
19         map.put(new Customer("AA", 1001), 87);
20         map.put(new Customer("CC", 1001), 67);
21         map.put(new Customer("MM", 1004), 77);
22         map.put(new Customer("GG", 1002), 97);
23        
24         Set set1 = map.keySet();
25         for (Object obj : set1) {
26             System.out.println(obj + "----->" + map.get(obj));
27         }
28     }

-----------------------------------------------------------------------------------以下为填充集合的类

  1 class Customer {
  2  
  3    private String name;
  4     private Integer id;
  5     public String getName() {
  6         return name;
  7     }
  8     public void setName(String name) {
  9         this.name = name;
 10     }
 11     public Integer getId() {
 12         return id;
 13     }
 14     public void setId(Integer id) {
 15         this.id = id;
 16     }
 17     public Customer(String name, Integer id) {
 18         super();
 19         this.name = name;
 20         this.id = id;
 21     }
 22     public Customer() {
 23         super();
 24     }
 25     @Override
 26     public String toString() {
 27         return "Customer [name=" + name + ", id=" + id + "]";
 28     }
 29     @Override
 30     public int hashCode() {
 31         final int prime = 31;
 32         int result = 1;
 33         result = prime * result + ((id == null) ? 0 : id.hashCode());
 34         result = prime * result + ((name == null) ? 0 : name.hashCode());
 35         return result;
 36     }
 37     @Override
 38     public boolean equals(Object obj) {
 39         if (this == obj)
 40             return true;
 41         if (obj == null)
 42             return false;
 43         if (getClass() != obj.getClass())
 44             return false;
 45         Customer other = (Customer) obj;
 46         if (id == null) {
 47             if (other.id != null)
 48                 return false;
 49         } else if (!id.equals(other.id))
 50             return false;
 51         if (name == null) {
 52             if (other.name != null)
 53                 return false;
 54         } else if (!name.equals(other.name))
 55             return false;
 56         return true;
 57     }
 58    
 59 }
 60 
 61 class Person implements Comparable{
 62     private String name;
 63     private Integer age;
 64     public String getName() {
 65         return name;
 66     }
 67     public void setName(String name) {
 68         this.name = name;
 69     }
 70     public Integer getAge() {
 71         return age;
 72     }
 73     public void setAge(Integer age) {
 74         this.age = age;
 75     }
 76     public Person() {
 77         super();
 78     }
 79     public Person(String name, Integer age) {
 80         super();
 81         this.name = name;
 82         this.age = age;
 83     }
 84     @Override
 85     public String toString() {
 86         return "Person [name=" + name + ", age=" + age + "]";
 87     }
 88     //static int init = 1000;
 89     @Override
 90     public int hashCode() {//return age.hashCode() + name.hashCode();没下述的健壮性好。
 91         final int prime = 31;
 92         int result = 1;
 93         result = prime * result + ((age == null) ? 0 : age.hashCode());
 94         result = prime * result + ((name == null) ? 0 : name.hashCode());
 95         return result;
 96         //return init++;//不能这样用
 97     }
 98     @Override
 99     public boolean equals(Object obj) {
100         if (this == obj)
101             return true;
102         if (obj == null)
103             return false;
104         if (getClass() != obj.getClass())
105             return false;
106         Person other = (Person) obj;
107         if (age == null) {
108             if (other.age != null)
109                 return false;
110         } else if (!age.equals(other.age))
111             return false;
112         if (name == null) {
113             if (other.name != null)
114                 return false;
115         } else if (!name.equals(other.name))
116             return false;
117         return true;
118     }
119     //当向TreeSet中添加Person类的对象时,依据此方法,确定按照哪个属性排列。
120     @Override
121     public int compareTo(Object o) {
122         if(o instanceof Person){
123             Person p = (Person)o;
124             //return this.name.compareTo(p.name);
125             //return -this.age.compareTo(p.age);
126             int i = this.age.compareTo(p.age);
127             if(i == 0){
128                 return this.name.compareTo(p.name);
129             }else{
130                 return i;
131             }
132         }
133         return 0;
134     }
135    
136 }

 

JAVA学习--集合Map的使用

标签:

原文地址:http://www.cnblogs.com/zhangfan94/p/4263338.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!