Basic java sorting: Map interface and java sorting map interface
To implement key-value ing in Java, you can use the Map interface.
Add and access key-value pairs
Map <Integer, String> keyValues = new HashMap <Integer, String> (); keyValues. put (1, "1"); keyValues. put (2, "22"); keyValues. put (3, "333"); keyValues. put (3, "3, 3"); for (Map. entry <Integer, String> entry: keyValues. entrySet () {System. out. println (entry. getKey () + "" + entry. getValue ());}
1 12 223 3 3 3
When duplicate keys are added, the last added key will overwrite the values corresponding to the same key.
Fill in another Map
Map <Integer, String> keyValues = new HashMap <Integer, String> (); keyValues. put (1, "1"); keyValues. put (2, "22"); keyValues. put (3, "333"); keyValues. put (3, "three"); Map <Integer, String> keyValues2 = new HashMap <Integer, String> (); keyValues. put (4, "4444"); keyValues. put (5, "55555"); keyValues. putAll (keyValues2); for (Map. entry <Integer, String> entry: keyValues. entrySet () {System. out. println (entry. getKey () + "" + entry. getValue ());}
1 12 223 3 3 3 4 44445 55555
Some common methods
System. out. println (keyValues. get (5); // obtain the value of the key boolean isTrue = keyValues. containsKey (3); // whether or not a key int count = keyValues. size (); // number of key-value pairs Set <Integer> keys = keyValues. keySet (); // String output = keyValues. remove (5); // remove a key value. If no key value exists, null keyValues is returned. clear (); // clear all key values boolean isEmpty = keyValues. isEmpty (); // whether the number of key-value pairs is null