1. 程式人生 > >Java 集合系列14之 Map總結(HashMap, Hashtable, TreeMap, WeakHashMap等使用場景)

Java 集合系列14之 Map總結(HashMap, Hashtable, TreeMap, WeakHashMap等使用場景)

  1 import java.util.HashMap;
  2 import java.util.Iterator;
  3 import java.util.Map;
  4 import java.util.WeakHashMap;
  5 import java.util.Date;
  6 import java.lang.ref.WeakReference;
  7 
  8 /**
  9  * @desc HashMap 和 WeakHashMap比較程式
 10  *
 11  * @author skywang
 12  * @email [email protected]
13 */ 14 public class CompareHashmapAndWeakhashmap { 15 16 public static void main(String[] args) throws Exception { 17 18 // 當“弱鍵”是String時,比較HashMap和WeakHashMap 19 compareWithString(); 20 // 當“弱鍵”是自定義型別時,比較HashMap和WeakHashMap 21 compareWithSelfClass();
22 } 23 24 /** 25 * 遍歷map,並列印map的大小 26 */ 27 private static void iteratorAndCountMap(Map map) { 28 // 遍歷map 29 for (Iterator iter = map.entrySet().iterator(); 30 iter.hasNext(); ) { 31 Map.Entry en = (Map.Entry)iter.next();
32 System.out.printf("map entry : %s - %s\n ",en.getKey(), en.getValue()); 33 } 34 35 // 列印HashMap的實際大小 36 System.out.printf(" map size:%s\n\n", map.size()); 37 } 38 39 /** 40 * 通過String物件測試HashMap和WeakHashMap 41 */ 42 private static void compareWithString() { 43 // 新建4個String字串 44 String w1 = new String("W1"); 45 String w2 = new String("W2"); 46 String h1 = new String("H1"); 47 String h2 = new String("H2"); 48 49 // 新建 WeakHashMap物件,並將w1,w2新增到 WeakHashMap中 50 Map wmap = new WeakHashMap(); 51 wmap.put(w1, "w1"); 52 wmap.put(w2, "w2"); 53 54 // 新建 HashMap物件,並將h1,h2新增到 WeakHashMap中 55 Map hmap = new HashMap(); 56 hmap.put(h1, "h1"); 57 hmap.put(h2, "h2"); 58 59 // 刪除HashMap中的“h1”。 60 // 結果:刪除“h1”之後,HashMap中只有 h2 ! 61 hmap.remove(h1); 62 63 // 將WeakHashMap中的w1設定null,並執行gc()。系統會回收w1 64 // 結果:w1是“弱鍵”,被GC回收後,WeakHashMap中w1對應的鍵值對,也會被從WeakHashMap中刪除。 65 // w2是“弱鍵”,但它不是null,不會被GC回收;也就不會被從WeakHashMap中刪除。 66 // 因此,WeakHashMap中只有 w2 67 // 注意:若去掉“w1=null” 或者“System.gc()”,結果都會不一樣! 68 w1 = null; 69 System.gc(); 70 71 // 遍歷並列印HashMap的大小 72 System.out.printf(" -- HashMap --\n"); 73 iteratorAndCountMap(hmap); 74 75 // 遍歷並列印WeakHashMap的大小 76 System.out.printf(" -- WeakHashMap --\n"); 77 iteratorAndCountMap(wmap); 78 } 79 80 /** 81 * 通過自定義類測試HashMap和WeakHashMap 82 */ 83 private static void compareWithSelfClass() { 84 // 新建4個自定義物件 85 Self s1 = new Self(10); 86 Self s2 = new Self(20); 87 Self s3 = new Self(30); 88 Self s4 = new Self(40); 89 90 // 新建 WeakHashMap物件,並將s1,s2新增到 WeakHashMap中 91 Map wmap = new WeakHashMap(); 92 wmap.put(s1, "s1"); 93 wmap.put(s2, "s2"); 94 95 // 新建 HashMap物件,並將s3,s4新增到 WeakHashMap中 96 Map hmap = new HashMap(); 97 hmap.put(s3, "s3"); 98 hmap.put(s4, "s4"); 99 100 // 刪除HashMap中的s3。 101 // 結果:刪除s3之後,HashMap中只有 s4 ! 102 hmap.remove(s3); 103 104 // 將WeakHashMap中的s1設定null,並執行gc()。系統會回收w1 105 // 結果:s1是“弱鍵”,被GC回收後,WeakHashMap中s1對應的鍵值對,也會被從WeakHashMap中刪除。 106 // w2是“弱鍵”,但它不是null,不會被GC回收;也就不會被從WeakHashMap中刪除。 107 // 因此,WeakHashMap中只有 s2 108 // 注意:若去掉“s1=null” 或者“System.gc()”,結果都會不一樣! 109 s1 = null; 110 System.gc(); 111 112 /* 113 // 休眠500ms 114 try { 115 Thread.sleep(500); 116 } catch (InterruptedException e) { 117 e.printStackTrace(); 118 } 119 // */ 120 121 // 遍歷並列印HashMap的大小 122 System.out.printf(" -- Self-def HashMap --\n"); 123 iteratorAndCountMap(hmap); 124 125 // 遍歷並列印WeakHashMap的大小 126 System.out.printf(" -- Self-def WeakHashMap --\n"); 127 iteratorAndCountMap(wmap); 128 } 129 130 private static class Self { 131 int id; 132 133 public Self(int id) { 134 this.id = id; 135 } 136 137 // 覆蓋finalize()方法 138 // 在GC回收時會被執行 139 protected void finalize() throws Throwable { 140 super.finalize(); 141 System.out.printf("GC Self: id=%d addr=0x%s)\n", id, this); 142 } 143 } 144 }