HashMap学习

 1 import java.util.*;
 2 
 3 public class UpdateStu {
 4     public static void main(String[] args) {
 5         //创建HashMap集合
 6         Map<String,String> map = new HashMap<>();
 7         //往集合中添加数据
 8         map.put("01","小王");
 9         map.put("02","小李");
10         map.put("03","小张");
11         map.put("04","小明");
12         //判断集合中有没有key"01"的映射
13         System.out.println("集合中是否有KEY=01的映射:"+map.containsKey("01"));
14         //判断集合中有没有value"小明"的映射
15         System.out.println("集合中是否有VALUE=小明的映射:"+map.containsValue("小明"));
16         //获取key=02的映射值
17         System.out.println("获取key=02的映射值:"+map.get("02"));
18         //获取所有的Key集合
19         Set<String> set = map.keySet();
20         Iterator<String> it = set.iterator();
21         System.out.println("集合中所有的Key元素有:");
22         while(it.hasNext()){
23             System.out.println(it.next());
24         }
25         //获取所有的Value
26         Collection<String> coll = map.values();
27         it = coll.iterator();
28         System.out.println("集合中所有的Value元素有:");
29         while(it.hasNext()){
30             System.out.println(it.next());
31         }
32     }
33 }

 1 import java.util.*;
 2 
 3 class Emp {
 4     private String e_id;
 5     private String e_name;
 6     public Emp( String e_id,String e_name) {
 7         this.e_id = e_id;
 8         this.e_name = e_name;
 9     }
10     public String getE_id() {
11         return e_id;
12     }
13     public void setE_id(String e_id) {
14         this.e_id = e_id;
15     }
16     public String getE_name() {
17         return e_name;
18     }
19     public void setE_name(String e_name) {
20         this.e_name = e_name;
21     }
22 }
23 
24 public class MapText {
25     public static void main(String[] args) {
26         //创建HashMap集合
27         Map<String,String> map = new HashMap<>();
28         //创建对象
29         Emp emp = new Emp("01", "小王");
30         Emp emp2 = new Emp("03", "小张");
31         Emp emp3 = new Emp("04", "小明");
32         Emp emp4 = new Emp("02", "小李");
33         //往集合中加入数据
34         map.put(emp.getE_id(), emp.getE_name());
35         map.put(emp2.getE_id(), emp2.getE_name());
36         map.put(emp3.getE_id(), emp3.getE_name());
37         map.put(emp4.getE_id(), emp4.getE_name());
38         //获取HashMap对象的key对象集合
39         Set<String> set = map.keySet();
40         Iterator<String> it = set.iterator();
41         System.out.println("HashMap类实现的Map集合,无序:");
42         while(it.hasNext()){
43             String str = (String)it.next();
44             String name = (String)map.get(str);
45             System.out.println(str+" "+name);
46         }
47         //创建TreeMap集合
48         TreeMap<String,String> treemap = new TreeMap<>();
49         //将HashMap集合中的数据加入到TreeMap中
50         treemap.putAll(map);
51         //获取TreeMap对象的key对象集合
52         Iterator<String> iter = treemap.keySet().iterator();
53         System.out.println("TreeMap类实现的Map集合,键对象升序:");
54         while(iter.hasNext()){
55             String str = (String)iter.next();
56             String name = (String)map.get(str);
57             System.out.println(str+" "+name);
58         }
59     }
60 }

posted @ 2018-02-12 21:04  祁俊辉  阅读(173)  评论(0编辑  收藏  举报