- /**
- * Map is used to store key-value pairs, do not allow key duplication, and values can be duplicated.
- * (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.
- * HashMap allows a maximum of one record's key to be null, allowing the value of multiple records to be null.
- * HashMap does not support thread synchronization, which means that multiple threads can write HashMap at any one time, which may result in inconsistent data.
- * If synchronization is required, you can use the Collections.synchronizedmap (HashMap map) method to make the HASHMAP capable of synchronizing.
- * (2) Hashtable is similar to HashMap, the difference is: it does not allow the record key or value is empty;
- * 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.
- * (3) Linkedhashmap saves the insertion Order of records, and when traversing linkedhashmap with Iteraor, the first record must be inserted first.
- * It is slower than hashmap when traversing.
- * (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,
- * The resulting records are sequenced.
- */
- /**
- * Demonstrate the implementation class for each map
- */
- public class TestMap {
- /**
- * Initialize a 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 ());
- The keys in the map are not duplicated, if you insert two key values of the same record,
- Then the inserted record overwrites the first inserted record
- Map.put (Key, key.tostring () + "0"); }
- }
- }
- /**
- * Output a map
- * @param map
- */
- public static void output (map map) {
- if (map! = null) {
- Object key = null;
- Object value = null;
- Use iterators to traverse the keys of a map, based on key values
- Iterator it = map.keyset (). Iterator ();
- while (It.hasnext ()) {
- key = it.next ();
- value = map.get (key);
- System.out.println ("key:" + key + "; Value: "+ value");
- }
- Or use iterators to traverse Map Records Map.entry
- Map.entry Entry = null;
- it = map.entryset (). iterator ();
- while (It.hasnext ()) {
- A map.entry represents a record
- entry = (map.entry) it.next ();
- The key and value of the record can be obtained by entry
- System.out.println ("key:" + entry.getkey () + "; Value: "+ entry.getvalue ());
- }
- }
- }
- /**
- * Determine if a map contains a key
- * @param map
- * @param key
- * @return
- */
- public static Boolean ContainsKey (map map, Object key) {
- if (map! = null) {
- return Map.containskey (key);
- }
- return false;
- }
- /**
- * Determine if a map contains a value
- * @param map
- * @param value
- * @return
- */
- public static Boolean Containsvalue (map map, Object value) {
- if (map! = null) {
- return Map.containsvalue (value);
- }
- return false;
- }
- /**
- * Demo HashMap
- */
- public static void Testhashmap () {
- Map myMap = new HashMap ();
- Init (MYMAP);
- The HashMap key can be null
- Mymap.put (NULL, "ddd");
- The value of HashMap can be null
- Mymap.put ("AAA", NULL);
- Output (MYMAP);
- }
- /**
- * Demo Hashtable
- */
- public static void Testhashtable () {
- Map myMap = new Hashtable ();
- Init (MYMAP);
- The Hashtable key cannot be null
- Mymap.put (NULL, "ddd");
- The value of Hashtable cannot be null
- Mymap.put ("AAA", NULL);
- Output (MYMAP);
- }
- /**
- * Demo Linkedhashmap
- */
- public static void Testlinkedhashmap () {
- Map myMap = new Linkedhashmap ();
- Init (MYMAP);
- The LINKEDHASHMAP key can be null
- Mymap.put (NULL, "ddd");
- The value of Linkedhashmap can be null
- Mymap.put ("AAA", NULL);
- Output (MYMAP);
- }
- /**
- * Demo TreeMap
- */
- public static void Testtreemap () {
- Map myMap = new TreeMap ();
- Init (MYMAP);
- The TREEMAP key cannot be null
- Mymap.put (NULL, "ddd");
- The value of TreeMap cannot be null
- Mymap.put ("AAA", NULL);
- Output (MYMAP);
- }
- public static void Main (string[] args) {
- System.out.println ("Using HashMap");
- Testmap.testhashmap ();
- System.out.println ("Using Hashtable");
- Testmap.testhashtable ();
- System.out.println ("Using Linkedhashmap");
- Testmap.testlinkedhashmap ();
- System.out.println ("Using TreeMap");
- Testmap.testtreemap ();
- Map myMap = new HashMap ();
- Testmap.init (MYMAP);
- SYSTEM.OUT.PRINTLN ("New initialization of a Map:mymap");
- Testmap.output (MYMAP);
- Clear Map
- Mymap.clear ();
- System.out.println ("Will mymap clear, mymap empty?" "+ mymap.isempty ());
- Testmap.output (MYMAP);
- Mymap.put ("AAA", "AAAA");
- Mymap.put ("BBB", "bbbb");
- Determine if a map contains a key or a value
- System.out.println ("Mymap contains key AAA?") "+ Testmap.containskey (MyMap," AAA "));
- System.out.println ("Mymap contains value aaaa?") "+ Testmap.containsvalue (myMap," AAAA "));
- Delete a record in a map by key
- Mymap.remove ("AAA");
- System.out.println ("Remove key AAA, Mymap contains key AAA?") "+ Testmap.containskey (MyMap," AAA "));
- Get the number of records for a map
- System.out.println ("MyMap contains the number of records:" + mymap.size ());
- }
- }
Knowledge of the Java map collection