1 package com.jdk7.chapter4; 2 3 import java.util.HashMap; 4 import java.util.Hashtable; 5 import java.util.Iterator; 6 import java.util.Map; 7 import java.util.TreeMap; 8 9 /** 10 * 集合之形式三》映射(Map),根据键得到值,不允许'键'重复,允许'值'重复,包含HashMap、HashTable、LinkedHashMap、TreeMap 11 * HashMap:允许最多一个'键'为null,多个'值'为null; 不支持线程同步(即多个线程可以同时写HashMap) 12 * HashTable:不允许键或值为null,支持线程同步(即同一时刻只能一个线程写HashTable) 13 * LinkedHashMap:保存记录的插入顺序,先插入的记录会先得到,允许最多一个'键'为null,多个'值'为null 14 * TreeMap:按键升序排列,不允许'键'为null,允许多个'值'为null 15 * @author Administrator 16 * 17 */ 18 public class MapTest { 19 public static void init(Map map){ 20 if(map==null){ 21 System.out.println("映射不能为空!"); 22 } 23 map.put("001", "Hello"); 24 map.put("002", "Lily"); 25 map.put("003", "Lucy"); 26 map.put("004", "Hanmei"); 27 } 28 public static void printMap(Map map){ 29 if(map==null){ 30 System.out.println("映射为空"); 31 } 32 //先获取Map的所有键,获取的键值为无重复的Set,再依次遍历key集合 33 Iterator it = map.keySet().iterator(); 34 Object key = null; 35 Object value = null; 36 System.out.println("=======遍历key======="); 37 while(it.hasNext()){ 38 key = it.next(); 39 value = map.get(key); 40 System.out.println("key: "+key+" value: "+value); 41 } 42 43 Iterator itentry = map.entrySet().iterator(); 44 Map.Entry entry = null; 45 System.out.println("=======遍历集合中的每条记录======="); 46 while(itentry.hasNext()){ 47 entry = (Map.Entry)itentry.next(); 48 System.out.println("key: "+entry.getKey()+" value: "+entry.getValue()); 49 } 50 System.out.println(); 51 } 52 53 public static boolean containsKey(Map map, Object obj){ 54 if(map==null){ 55 return false; 56 } 57 return map.containsKey(obj); 58 } 59 60 public static boolean containsValue(Map map, Object obj){ 61 if(map==null){ 62 return false; 63 } 64 return map.containsValue(obj); 65 } 66 /** 67 * 68 * @param map 69 70 public static void add(Map map){ 71 MapTest.init(map); 72 System.out.println("Map中是否包含Key>"+MapTest.containsKey(map, null)); 73 System.out.println("Map中是否包含Value>"+MapTest.containsValue(map, "2222")); 74 map.put(null, "4444"); 75 map.put(null, "2222"); 76 map.put("005", null); 77 map.put("006", null); 78 MapTest.printMap(map); 79 } 80 */ 81 public static void add(Map map,Object key, Object value){ 82 MapTest.init(map); 83 if(!(MapTest.containsKey(map, key) && MapTest.containsValue(map, value))){ 84 map.put(key, value); 85 } 86 } 87 88 public static void HashMap(){ 89 System.out.println("===============HashMap映射==============="); 90 Map map = new HashMap(); 91 MapTest.add(map, null, "4444"); 92 MapTest.add(map, null, "2222"); 93 MapTest.add(map, "005", null); 94 MapTest.add(map, "006", null); 95 MapTest.printMap(map); 96 } 97 98 public static void HashTable(){ 99 System.out.println("===============HashTable映射==============="); 100 Map map = new Hashtable(); 101 MapTest.init(map); 102 Object key = "005"; 103 Object value = "700"; 104 if(!(MapTest.containsKey(map, key) && MapTest.containsValue(map, value))){ 105 map.put(key, value); 106 } 107 MapTest.printMap(map); 108 } 109 110 public static void linkedHashMap(){ 111 System.out.println("===============linkedHashMap映射==============="); 112 Map map = new java.util.LinkedHashMap(); 113 MapTest.add(map, null, "4444"); 114 MapTest.add(map, null, "2222"); 115 MapTest.add(map, "005", null); 116 MapTest.add(map, "006", null); 117 MapTest.printMap(map); 118 } 119 120 public static void treeMap(){ 121 System.out.println("===============treeMap映射==============="); 122 Map map = new TreeMap(); 123 MapTest.init(map); 124 Object key = "005"; 125 Object value = null; 126 if(!(MapTest.containsKey(map, key) && MapTest.containsValue(map, value))){ 127 map.put(key, value); 128 } 129 MapTest.printMap(map); 130 } 131 public static void main(String[] args) { 132 MapTest mt = new MapTest(); 133 mt.HashMap(); 134 mt.HashTable(); 135 mt.linkedHashMap(); 136 mt.treeMap(); 137 } 138 } 139 140 执行结果: 141 ===============HashMap映射=============== 142 =======遍历key======= 143 key: null value: 2222 144 key: 004 value: Hanmei 145 key: 005 value: null 146 key: 006 value: null 147 key: 001 value: Hello 148 key: 002 value: Lily 149 key: 003 value: Lucy 150 =======遍历集合中的每条记录======= 151 key: null value: 2222 152 key: 004 value: Hanmei 153 key: 005 value: null 154 key: 006 value: null 155 key: 001 value: Hello 156 key: 002 value: Lily 157 key: 003 value: Lucy 158 159 ===============HashTable映射=============== 160 =======遍历key======= 161 key: 005 value: 700 162 key: 004 value: Hanmei 163 key: 003 value: Lucy 164 key: 002 value: Lily 165 key: 001 value: Hello 166 =======遍历集合中的每条记录======= 167 key: 005 value: 700 168 key: 004 value: Hanmei 169 key: 003 value: Lucy 170 key: 002 value: Lily 171 key: 001 value: Hello 172 173 ===============linkedHashMap映射=============== 174 =======遍历key======= 175 key: 001 value: Hello 176 key: 002 value: Lily 177 key: 003 value: Lucy 178 key: 004 value: Hanmei 179 key: null value: 2222 180 key: 005 value: null 181 key: 006 value: null 182 =======遍历集合中的每条记录======= 183 key: 001 value: Hello 184 key: 002 value: Lily 185 key: 003 value: Lucy 186 key: 004 value: Hanmei 187 key: null value: 2222 188 key: 005 value: null 189 key: 006 value: null 190 191 ===============treeMap映射=============== 192 =======遍历key======= 193 key: 001 value: Hello 194 key: 002 value: Lily 195 key: 003 value: Lucy 196 key: 004 value: Hanmei 197 key: 005 value: null 198 =======遍历集合中的每条记录======= 199 key: 001 value: Hello 200 key: 002 value: Lily 201 key: 003 value: Lucy 202 key: 004 value: Hanmei 203 key: 005 value: null