Knowledge of the Java map collection

Source: Internet
Author: User

  1. /**
  2. * Map is used to store key-value pairs, do not allow key duplication, and values can be duplicated.
  3. * (1) HashMap is one of the most commonly used maps, it stores data according to the hashcode value of the key, it can get its value directly according to the key, and has fast access speed.
  4. * HashMap allows a maximum of one record's key to be null, allowing the value of multiple records to be null.
  5. * HashMap does not support thread synchronization, which means that multiple threads can write HashMap at any one time, which may result in inconsistent data.
  6. * If synchronization is required, you can use the Collections.synchronizedmap (HashMap map) method to make the HASHMAP capable of synchronizing.
  7. * (2) Hashtable is similar to HashMap, the difference is: it does not allow the record key or value is empty;
  8. * It supports thread synchronization, where only one thread can write hashtable at any one time, however, this also causes the hashtable to be slow to write.
  9. * (3) Linkedhashmap saves the insertion Order of records, and when traversing linkedhashmap with Iteraor, the first record must be inserted first.
  10. * It is slower than hashmap when traversing.
  11. * (4) TreeMap is able to sort the records it saves according to the key, by default it is sorted in ascending order, or a sort comparer can be specified. When traversing TreeMap with Iteraor,
  12. * The resulting records are sequenced.
  13. */
  14. /**
  15. * Demonstrate the implementation class for each map
  16. */
  17. public class TestMap {
  18. /**
  19. * Initialize a 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. The keys in the map are not duplicated, if you insert two key values of the same record,
  29. Then the inserted record overwrites the first inserted record
  30. Map.put (Key, key.tostring () + "0"); }
  31. }
  32. }
  33. /**
  34. * Output a 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. Use iterators to traverse the keys of a map, based on key values
  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. Or use iterators to traverse Map Records Map.entry
  49. Map.entry Entry = null;
  50. it = map.entryset (). iterator ();
  51. while (It.hasnext ()) {
  52. A map.entry represents a record
  53. entry = (map.entry) it.next ();
  54. The key and value of the record can be obtained by entry
  55. System.out.println ("key:" + entry.getkey () + "; Value: "+ entry.getvalue ());
  56. }
  57. }
  58. }
  59. /**
  60. * Determine if a map contains a key
  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. * Determine if a map contains a value
  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. * Demo HashMap
  85. */
  86. public static void Testhashmap () {
  87. Map myMap = new HashMap ();
  88. Init (MYMAP);
  89. The HashMap key can be null
  90. Mymap.put (NULL, "ddd");
  91. The value of HashMap can be null
  92. Mymap.put ("AAA", NULL);
  93. Output (MYMAP);
  94. }
  95. /**
  96. * Demo Hashtable
  97. */
  98. public static void Testhashtable () {
  99. Map myMap = new Hashtable ();
  100. Init (MYMAP);
  101. The Hashtable key cannot be null
  102. Mymap.put (NULL, "ddd");
  103. The value of Hashtable cannot be null
  104. Mymap.put ("AAA", NULL);
  105. Output (MYMAP);
  106. }
  107. /**
  108. * Demo Linkedhashmap
  109. */
  110. public static void Testlinkedhashmap () {
  111. Map myMap = new Linkedhashmap ();
  112. Init (MYMAP);
  113. The LINKEDHASHMAP key can be null
  114. Mymap.put (NULL, "ddd");
  115. The value of Linkedhashmap can be null
  116. Mymap.put ("AAA", NULL);
  117. Output (MYMAP);
  118. }
  119. /**
  120. * Demo TreeMap
  121. */
  122. public static void Testtreemap () {
  123. Map myMap = new TreeMap ();
  124. Init (MYMAP);
  125. The TREEMAP key cannot be null
  126. Mymap.put (NULL, "ddd");
  127. The value of TreeMap cannot be null
  128. Mymap.put ("AAA", NULL);
  129. Output (MYMAP);
  130. }
  131. public static void Main (string[] args) {
  132. System.out.println ("Using HashMap");
  133. Testmap.testhashmap ();
  134. System.out.println ("Using Hashtable");
  135. Testmap.testhashtable ();
  136. System.out.println ("Using Linkedhashmap");
  137. Testmap.testlinkedhashmap ();
  138. System.out.println ("Using TreeMap");
  139. Testmap.testtreemap ();
  140. Map myMap = new HashMap ();
  141. Testmap.init (MYMAP);
  142. SYSTEM.OUT.PRINTLN ("New initialization of a Map:mymap");
  143. Testmap.output (MYMAP);
  144. Clear Map
  145. Mymap.clear ();
  146. System.out.println ("Will mymap clear, mymap empty?" "+ mymap.isempty ());
  147. Testmap.output (MYMAP);
  148. Mymap.put ("AAA", "AAAA");
  149. Mymap.put ("BBB", "bbbb");
  150. Determine if a map contains a key or a value
  151. System.out.println ("Mymap contains key AAA?") "+ Testmap.containskey (MyMap," AAA "));
  152. System.out.println ("Mymap contains value aaaa?") "+ Testmap.containsvalue (myMap," AAAA "));
  153. Delete a record in a map by key
  154. Mymap.remove ("AAA");
  155. System.out.println ("Remove key AAA, Mymap contains key AAA?") "+ Testmap.containskey (MyMap," AAA "));
  156. Get the number of records for a map
  157. System.out.println ("MyMap contains the number of records:" + mymap.size ());
  158. }
  159. }

Knowledge of the Java map collection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.