1. 程式人生 > >java map集合的知識

java map集合的知識

con ttr 使用 string 如果 ddd bsp contains 演示

  1. /**
  2. * Map用於存儲鍵值對,不允許鍵重復,值可以重復。
  3. * (1)HashMap是一個最常用的Map,它根據鍵的hashCode值存儲數據,根據鍵可以直接獲取它的值,具有很快的訪問速度。
  4. * HashMap最多只允許一條記錄的鍵為null,允許多條記錄的值為null。
  5. * HashMap不支持線程的同步,即任一時刻可以有多個線程同時寫HashMap,可能會導致數據的不一致。
  6. * 如果需要同步,可以用Collections.synchronizedMap(HashMap map)方法使HashMap具有同步的能力。
  7. * (2)Hashtable與HashMap類似,不同的是:它不允許記錄的鍵或者值為空;
  8. * 它支持線程的同步,即任一時刻只有一個線程能寫Hashtable,然而,這也導致了Hashtable在寫入時會比較慢。
  9. * (3)LinkedHashMap保存了記錄的插入順序,在用Iteraor遍歷LinkedHashMap時,先得到的記錄肯定是先插入的。
  10. * 在遍歷的時候會比HashMap慢。
  11. * (4)TreeMap能夠把它保存的記錄根據鍵排序,默認是按升序排序,也可以指定排序的比較器。當用Iteraor遍歷TreeMap時,
  12. * 得到的記錄是排過序的。
  13. */
  14. /**
  15. * 演示各個Map的實現類
  16. */
  17. public class TestMap {
  18. /**
  19. * 初始化一個Map
  20. * @param map
  21. */
  22. public static void init(Map map){
  23. if (map != null){
  24. String key = null;
  25. for (int i=5; i>0; i--){
  26. key = new Integer(i).toString() + ".0";
  27. map.put(key, key.toString());
  28. //Map中的鍵是不重復的,如果插入兩個鍵值一樣的記錄,
  29. //那麽後插入的記錄會覆蓋先插入的記錄
  30. map.put(key, key.toString() + "0"); }
  31. }
  32. }
  33. /**
  34. * 輸出一個Map
  35. * @param map
  36. */
  37. public static void output(Map map){
  38. if (map != null){
  39. Object key = null;
  40. Object value = null;
  41. //使用叠代器遍歷Map的鍵,根據鍵取值
  42. Iterator it = map.keySet().iterator();
  43. while (it.hasNext()){
  44. key = it.next();
  45. value = map.get(key);
  46. System.out.println("key: " + key + "; value: " + value );
  47. }
  48. //或者使用叠代器遍歷Map的記錄Map.Entry
  49. Map.Entry entry = null;
  50. it = map.entrySet().iterator();
  51. while (it.hasNext()){
  52. //一個Map.Entry代表一條記錄
  53. entry = (Map.Entry)it.next();
  54. //通過entry可以獲得記錄的鍵和值
  55. System.out.println("key: " + entry.getKey() + "; value: " + entry.getValue());
  56. }
  57. }
  58. }
  59. /**
  60. * 判斷map是否包含某個鍵
  61. * @param map
  62. * @param key
  63. * @return
  64. */
  65. public static boolean containsKey(Map map, Object key){
  66. if (map != null){
  67. return map.containsKey(key);
  68. }
  69. return false;
  70. }
  71. /**
  72. * 判斷map是否包含某個值
  73. * @param map
  74. * @param value
  75. * @return
  76. */
  77. public static boolean containsValue(Map map, Object value){
  78. if (map != null){
  79. return map.containsValue(value);
  80. }
  81. return false;
  82. }
  83. /**
  84. * 演示HashMap
  85. */
  86. public static void testHashMap(){
  87. Map myMap = new HashMap();
  88. init(myMap);
  89. //HashMap的鍵可以為null
  90. myMap.put(null,"ddd");
  91. //HashMap的值可以為null
  92. myMap.put("aaa", null);
  93. output(myMap);
  94. }
  95. /**
  96. * 演示Hashtable
  97. */
  98. public static void testHashtable(){
  99. Map myMap = new Hashtable();
  100. init(myMap);
  101. //Hashtable的鍵不能為null
  102. //myMap.put(null,"ddd");
  103. //Hashtable的值不能為null
  104. //myMap.put("aaa", null);
  105. output(myMap);
  106. }
  107. /**
  108. * 演示LinkedHashMap
  109. */
  110. public static void testLinkedHashMap(){
  111. Map myMap = new LinkedHashMap();
  112. init(myMap);
  113. //LinkedHashMap的鍵可以為null
  114. myMap.put(null,"ddd");
  115. //LinkedHashMap的值可以為null
  116. myMap.put("aaa", null);
  117. output(myMap);
  118. }
  119. /**
  120. * 演示TreeMap
  121. */
  122. public static void testTreeMap(){
  123. Map myMap = new TreeMap();
  124. init(myMap);
  125. //TreeMap的鍵不能為null
  126. //myMap.put(null,"ddd");
  127. //TreeMap的值不能為null
  128. //myMap.put("aaa", null);
  129. output(myMap);
  130. }
  131. public static void main(String[] args) {
  132. System.out.println("采用HashMap");
  133. TestMap.testHashMap();
  134. System.out.println("采用Hashtable");
  135. TestMap.testHashtable();
  136. System.out.println("采用LinkedHashMap");
  137. TestMap.testLinkedHashMap();
  138. System.out.println("采用TreeMap");
  139. TestMap.testTreeMap();
  140. Map myMap = new HashMap();
  141. TestMap.init(myMap);
  142. System.out.println("新初始化一個Map: myMap");
  143. TestMap.output(myMap);
  144. //清空Map
  145. myMap.clear();
  146. System.out.println("將myMap clear後,myMap空了麽? " + myMap.isEmpty());
  147. TestMap.output(myMap);
  148. myMap.put("aaa", "aaaa");
  149. myMap.put("bbb", "bbbb");
  150. //判斷Map是否包含某鍵或者某值
  151. System.out.println("myMap包含鍵aaa? "+ TestMap.containsKey(myMap, "aaa"));
  152. System.out.println("myMap包含值aaaa? "+ TestMap.containsValue(myMap, "aaaa"));
  153. //根據鍵刪除Map中的記錄
  154. myMap.remove("aaa");
  155. System.out.println("刪除鍵aaa後,myMap包含鍵aaa? "+ TestMap.containsKey(myMap, "aaa"));
  156. //獲取Map的記錄數
  157. System.out.println("myMap包含的記錄數: " + myMap.size());
  158. }
  159. }

java map集合的知識