標籤:判斷 key syn 示範 刪除 code shm tag 能力
- /**
- * Map用於儲存索引值對,不允許鍵重複,值可以重複。
- * (1)HashMap是一個最常用的Map,它根據鍵的hashCode值儲存資料,根據鍵可以直接擷取它的值,具有很快的訪問速度。
- * HashMap最多隻允許一條記錄的鍵為null,允許多條記錄的值為null。
- * HashMap不支援線程的同步,即任一時刻可以有多個線程同時寫HashMap,可能會導致資料的不一致。
- * 如果需要同步,可以用Collections.synchronizedMap(HashMap map)方法使HashMap具有同步的能力。
- * (2)Hashtable與HashMap類似,不同的是:它不允許記錄的鍵或者值為空白;
- * 它支援線程的同步,即任一時刻只有一個線程能寫Hashtable,然而,這也導致了Hashtable在寫入時會比較慢。
- * (3)LinkedHashMap儲存了記錄的插入順序,在用Iteraor遍曆LinkedHashMap時,先得到的記錄肯定是先插入的。
- * 在遍曆的時候會比HashMap慢。
- * (4)TreeMap能夠把它儲存的記錄根據鍵排序,預設是按升序排序,也可以指定排序的比較子。當用Iteraor遍曆TreeMap時,
- * 得到的記錄是排過序的。
- */
- /**
- * 示範各個Map的實作類別
- */
- public class TestMap {
-
- /**
- * 初始化一個Map
- * @param map
- */
- public static void init(Map map){
- if (map != null){
- String key = null;
- for (int i=5; i>0; i--){
- key = new Integer(i).toString() + ".0";
- map.put(key, key.toString());
- //Map中的鍵是不重複的,如果插入兩個索引值一樣的記錄,
- //那麼後插入的記錄會覆蓋先插入的記錄
- map.put(key, key.toString() + "0"); }
- }
- }
- /**
- * 輸出一個Map
- * @param map
- */
- public static void output(Map map){
- if (map != null){
- Object key = null;
- Object value = null;
- //使用迭代器遍曆Map的鍵,根據鍵取值
- Iterator it = map.keySet().iterator();
- while (it.hasNext()){
- key = it.next();
- value = map.get(key);
- System.out.println("key: " + key + "; value: " + value );
- }
- //或者使用迭代器遍曆Map的記錄Map.Entry
- Map.Entry entry = null;
- it = map.entrySet().iterator();
- while (it.hasNext()){
- //一個Map.Entry代表一條記錄
- entry = (Map.Entry)it.next();
- //通過entry可以獲得記錄的鍵和值
- System.out.println("key: " + entry.getKey() + "; value: " + entry.getValue());
- }
- }
- }
- /**
- * 判斷map是否包含某個鍵
- * @param map
- * @param key
- * @return
- */
- public static boolean containsKey(Map map, Object key){
- if (map != null){
- return map.containsKey(key);
- }
- return false;
- }
- /**
- * 判斷map是否包含某個值
- * @param map
- * @param value
- * @return
- */
- public static boolean containsValue(Map map, Object value){
- if (map != null){
- return map.containsValue(value);
- }
- return false;
- }
- /**
- * 示範HashMap
- */
- public static void testHashMap(){
- Map myMap = new HashMap();
- init(myMap);
- //HashMap的鍵可以為null
- myMap.put(null,"ddd");
- //HashMap的值可以為null
- myMap.put("aaa", null);
- output(myMap);
- }
- /**
- * 示範Hashtable
- */
- public static void testHashtable(){
- Map myMap = new Hashtable();
- init(myMap);
- //Hashtable的鍵不能為null
- //myMap.put(null,"ddd");
- //Hashtable的值不能為null
- //myMap.put("aaa", null);
- output(myMap);
- }
- /**
- * 示範LinkedHashMap
- */
- public static void testLinkedHashMap(){
- Map myMap = new LinkedHashMap();
- init(myMap);
- //LinkedHashMap的鍵可以為null
- myMap.put(null,"ddd");
- //LinkedHashMap的值可以為null
- myMap.put("aaa", null);
- output(myMap);
- }
- /**
- * 示範TreeMap
- */
- public static void testTreeMap(){
- Map myMap = new TreeMap();
- init(myMap);
- //TreeMap的鍵不能為null
- //myMap.put(null,"ddd");
- //TreeMap的值不能為null
- //myMap.put("aaa", null);
- output(myMap);
- }
-
- public static void main(String[] args) {
- System.out.println("採用HashMap");
- TestMap.testHashMap();
- System.out.println("採用Hashtable");
- TestMap.testHashtable();
- System.out.println("採用LinkedHashMap");
- TestMap.testLinkedHashMap();
- System.out.println("採用TreeMap");
- TestMap.testTreeMap();
-
- Map myMap = new HashMap();
- TestMap.init(myMap);
- System.out.println("新初始化一個Map: myMap");
- TestMap.output(myMap);
- //清空Map
- myMap.clear();
- System.out.println("將myMap clear後,myMap空了麼? " + myMap.isEmpty());
- TestMap.output(myMap);
- myMap.put("aaa", "aaaa");
- myMap.put("bbb", "bbbb");
- //判斷Map是否包含某鍵或者某值
- System.out.println("myMap包含鍵aaa? "+ TestMap.containsKey(myMap, "aaa"));
- System.out.println("myMap包含值aaaa? "+ TestMap.containsValue(myMap, "aaaa"));
- //根據鍵刪除Map中的記錄
- myMap.remove("aaa");
- System.out.println("刪除鍵aaa後,myMap包含鍵aaa? "+ TestMap.containsKey(myMap, "aaa"));
- //擷取Map的記錄數
- System.out.println("myMap包含的記錄數: " + myMap.size());
- }
-
- }
java map集合的知識