/**
2 * MapTest.java
3 *
4 * Provider: CoderDream's Studio
5 *
6 * History
7 * Date(DD/MM/YYYY) Author Description
8 * ----------------------
9 * Oct 31, 2007 CoderDream Created
10 */
11 package com.coderdream;
12
13 import java.util.HashMap;
14 import java.util.Iterator;
15 import java.util.Map;
16 import java.util.TreeMap;
17
18 /**
19 * @author CoderDream
20 */
21 public class MapTest {
22
23 /**
24 * <pre>
25 * 使用HashMap使用1:
26 * 用迭代器得到map.entrySet(),
27 * 然后通过迭代得到Map.Entry对象,最后打印。
28 * 打印结果未排序
29 * </pre>
30 *
31 */
32 public void f1() {
33 Map map = new HashMap();
34 map.put( " 1 " , " Level 1 " );
35 map.put( " 2 " , " Level 2 " );
36 map.put( " 3 " , " Level 3 " );
37 map.put( " 4 " , " Level 4 " );
38 map.put( " F " , " Level F " );
39 map.put( " Q " , " Level Q " );
40 Iterator it = map.entrySet().iterator();
41 while (it.hasNext()) {
42 Map.Entry e = (Map.Entry) it.next();
43 System.out.println( " Key: " + e.getKey() + " ; Value: "
44 + e.getValue());
45 }
46 }
47
48 /**
49 * <pre>
50 * 使用泛型模式说明Map中存储的对象类型
51 * </pre>
52 */
53 public void f2() {
54 Map < String, String > map = new HashMap < String, String > ();
55 map.put( " 1 " , " Mon. " );
56 map.put( " 1 " , " Monday " );
57 map.put( " one " , " Monday " );
58 Iterator < Map.Entry < String, String >> it = map.entrySet().iterator();
59
60 while (it.hasNext()) {
61 Map.Entry entry = it.next();
62 System.out.println(entry.getKey() + " : " + entry.getValue());
63 }
64 }
65
66 /**
67 * <pre>
68 * 通过集合Set来得到HashMap中的对象
69 * </pre>
70 *
71 */
72 public void f3() {
73 Map map = new HashMap();
74 map.put( " 1 " , " Mon. " );
75 map.put( " 1 " , " Monday " );
76 map.put( " one " , " Monday " );
77 Iterator it = map.keySet().iterator();
78 String tmpKey = null ;
79 while (it.hasNext()) {
80 tmpKey = (String) it.next();
81 System.out.println( " Key: " + tmpKey
82 + " ; Value: " + map.get(tmpKey));
83 }
84 }
85
86 /**
87 * <pre>
88 * 使用TreeMap代替HashMap,取得结果是排序后的结果
89 * </pre>
90 */
91 public void f4() {
92 Map map = new TreeMap();
93 map.put( " 1 " , " Level 1 " );
94 map.put( " 2 " , " Level 2 " );
95 map.put( " 3 " , " Level 3 " );
96 map.put( " 4 " , " Level 4 " );
97 map.put( " F " , " Level F " );
98 map.put( " Q " , " Level Q " );
99 Iterator it = map.entrySet().iterator();
100 while (it.hasNext()) {
101 Map.Entry e = (Map.Entry) it.next();
102 System.out.println( " Key: " + e.getKey()
103 + " ; Value: " + e.getValue());
104 }
105 }
106
107 /**
108 * @param args
109 */
110 public static void main(String[] args) {
111
112 MapTest mt = new MapTest();
113 System.out.println( " ----------f1()---------- " );
114 mt.f1();
115 System.out.println( " ----------f2()---------- " );
116 mt.f2();
117 System.out.println( " ----------f3()---------- " );
118 mt.f3();
119 System.out.println( " ----------f4()---------- " );
120 mt.f4();
121 }
122 }
输出结果:
2 * MapTest.java
3 *
4 * Provider: CoderDream's Studio
5 *
6 * History
7 * Date(DD/MM/YYYY) Author Description
8 * ----------------------
9 * Oct 31, 2007 CoderDream Created
10 */
11 package com.coderdream;
12
13 import java.util.HashMap;
14 import java.util.Iterator;
15 import java.util.Map;
16 import java.util.TreeMap;
17
18 /**
19 * @author CoderDream
20 */
21 public class MapTest {
22
23 /**
24 * <pre>
25 * 使用HashMap使用1:
26 * 用迭代器得到map.entrySet(),
27 * 然后通过迭代得到Map.Entry对象,最后打印。
28 * 打印结果未排序
29 * </pre>
30 *
31 */
32 public void f1() {
33 Map map = new HashMap();
34 map.put( " 1 " , " Level 1 " );
35 map.put( " 2 " , " Level 2 " );
36 map.put( " 3 " , " Level 3 " );
37 map.put( " 4 " , " Level 4 " );
38 map.put( " F " , " Level F " );
39 map.put( " Q " , " Level Q " );
40 Iterator it = map.entrySet().iterator();
41 while (it.hasNext()) {
42 Map.Entry e = (Map.Entry) it.next();
43 System.out.println( " Key: " + e.getKey() + " ; Value: "
44 + e.getValue());
45 }
46 }
47
48 /**
49 * <pre>
50 * 使用泛型模式说明Map中存储的对象类型
51 * </pre>
52 */
53 public void f2() {
54 Map < String, String > map = new HashMap < String, String > ();
55 map.put( " 1 " , " Mon. " );
56 map.put( " 1 " , " Monday " );
57 map.put( " one " , " Monday " );
58 Iterator < Map.Entry < String, String >> it = map.entrySet().iterator();
59
60 while (it.hasNext()) {
61 Map.Entry entry = it.next();
62 System.out.println(entry.getKey() + " : " + entry.getValue());
63 }
64 }
65
66 /**
67 * <pre>
68 * 通过集合Set来得到HashMap中的对象
69 * </pre>
70 *
71 */
72 public void f3() {
73 Map map = new HashMap();
74 map.put( " 1 " , " Mon. " );
75 map.put( " 1 " , " Monday " );
76 map.put( " one " , " Monday " );
77 Iterator it = map.keySet().iterator();
78 String tmpKey = null ;
79 while (it.hasNext()) {
80 tmpKey = (String) it.next();
81 System.out.println( " Key: " + tmpKey
82 + " ; Value: " + map.get(tmpKey));
83 }
84 }
85
86 /**
87 * <pre>
88 * 使用TreeMap代替HashMap,取得结果是排序后的结果
89 * </pre>
90 */
91 public void f4() {
92 Map map = new TreeMap();
93 map.put( " 1 " , " Level 1 " );
94 map.put( " 2 " , " Level 2 " );
95 map.put( " 3 " , " Level 3 " );
96 map.put( " 4 " , " Level 4 " );
97 map.put( " F " , " Level F " );
98 map.put( " Q " , " Level Q " );
99 Iterator it = map.entrySet().iterator();
100 while (it.hasNext()) {
101 Map.Entry e = (Map.Entry) it.next();
102 System.out.println( " Key: " + e.getKey()
103 + " ; Value: " + e.getValue());
104 }
105 }
106
107 /**
108 * @param args
109 */
110 public static void main(String[] args) {
111
112 MapTest mt = new MapTest();
113 System.out.println( " ----------f1()---------- " );
114 mt.f1();
115 System.out.println( " ----------f2()---------- " );
116 mt.f2();
117 System.out.println( " ----------f3()---------- " );
118 mt.f3();
119 System.out.println( " ----------f4()---------- " );
120 mt.f4();
121 }
122 }
输出结果:
----------
f1()
----------
Key: 3 ; Value: Level 3
Key: 2 ; Value: Level 2
Key: F; Value: Level F
Key: 1 ; Value: Level 1
Key: Q; Value: Level Q
Key: 4 ; Value: Level 4
---------- f2() ----------
1 :Monday
one:Monday
---------- f3() ----------
Key: 1 ; Value: Monday
Key: one; Value: Monday
---------- f4() ----------
Key: 1 ; Value: Level 1
Key: 2 ; Value: Level 2
Key: 3 ; Value: Level 3
Key: 4 ; Value: Level 4
Key: F; Value: Level F
Key: Q; Value: Level Q
Key: 3 ; Value: Level 3
Key: 2 ; Value: Level 2
Key: F; Value: Level F
Key: 1 ; Value: Level 1
Key: Q; Value: Level Q
Key: 4 ; Value: Level 4
---------- f2() ----------
1 :Monday
one:Monday
---------- f3() ----------
Key: 1 ; Value: Monday
Key: one; Value: Monday
---------- f4() ----------
Key: 1 ; Value: Level 1
Key: 2 ; Value: Level 2
Key: 3 ; Value: Level 3
Key: 4 ; Value: Level 4
Key: F; Value: Level F
Key: Q; Value: Level Q