Map
Return type | Method | describe |
---|---|---|
V | get(Object key) | Get value by key |
V | put(Obejct k,Object v) | Add (replace) elements to the Map and return the previous Value; return null without previous elements |
V | remove(Object key) | Delete the element according to key and return the value corresponding to Value |
void | clear() | empty |
int | size() | Get the length of the set |
boolean | isEmpty() | Judge whether it is empty |
boolean | containsKey(Object object) | Determine whether the specified key exists |
boolean | containsValue(Object value) | Determine whether the specified value exists |
Set | keySet() | Collection of all key s |
Collection | values() | All value s |
HashMap
Store K-V and use key to distinguish.
import java.util.*; public class TestHashMap { public static void main(String[] args) { Map<Integer, String> map = new HashMap<>(); // New k returns null, old k returns old v String put = map.put(1, "A"); System.out.println("Not before. K Of V: " + put); put = map.put(1, "B"); System.out.println("The same before K Of V: " + put); System.out.println(map); // get: k returns v, no k returns null String v = map.get(2); System.out.println(v); v = map.get(1); System.out.println(v); // containsKey/containsValue boolean containsKey = map.containsKey(2); boolean containsValue = map.containsValue("B"); // remove: Delete k to return v; Delete none to return null String remove = map.remove(2); System.out.println(remove); remove = map.remove(1); System.out.println(remove); } }
Traverse:
package ah; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class TestMap1Hash { public static void main(String[] args) { Map<String, String> _map = new HashMap<String, String>(); _map.put("1", "name of a fictitious monkey with supernatural powers"); _map.put(null, "Bailong"); _map.put("2", "Enlightenment energy"); _map.put("3", "Wu Jing"); System.out.println("------foreach Grammatical traversal map(output K-V)------"); for (String _key : _map.keySet()) { System.out.print("key = " + _key); System.out.println(" value = " + _map.get(_key)); } System.out.println("------Java 8.forEach:Lambda------"); _map.forEach((k, v) -> System.out.println(k + ":" + v)); System.out.println("------Using iterators to iterate map(output V)------"); // 1. Collection of values Collection<String> _values = _map.values(); // 2. Obtaining iterators through Collection Iterator<String> it = _values.iterator(); // 3. Output value while (it.hasNext()) { String next = it.next(); System.out.println(next); } System.out.println("-----Map.Entry<K,V>-----"); // Map. Entry < K, V > is the internal interface of Map, which is called mapping item (key-value pair). Set<Entry<String, String>> entrySet = _map.entrySet(); System.out.println("=====Map.Entry<K,V>:for loop====="); for (Entry<String, String> entry : entrySet) { System.out.println(entry.getKey() + ":" + entry.getValue()); } System.out.println("=====Map.Entry<K,V>:iterator====="); Iterator<Entry<String, String>> iterator = entrySet.iterator(); while (iterator.hasNext()) { Entry<String, String> next = iterator.next(); System.out.println(next.getKey() + ":" + next.getValue()); } } }
LinkedHashMap
With its own linked list (record element order), it has predictable iteration order.
Hashtable
Hashtable is an older class that does not even follow the Java naming convention. Use as little as possible.
But Hashtable has an important subclass, java.util.Properties.
public class Properties extends Hashtable<Object,Object> {...}
Father and son are expensive, although Hashtable does not even have a standard name, it will not be eliminated. The Properties class is introduced in the IO section.
Hashtable is synchronous and slow.
The two map s are basically identical in use, but Hashtable is more restrictive and neither K nor V accepts null.——
- No acceptance
- No prompt
- runtime error
// X m.put(null, "NNN"); // X m.put("NNN", null);
HashMap compares Hashtable:
null key | null value | Duplicate key | |
---|---|---|---|
HashMap | ● | ● | × |
Hashtable | × | × | × |
JAVA 9:of Method
The static method of is added to the List interface, Set interface and Map interface to initialize the collection.
The number of collection elements is immutable, and adding new elements throws an Unsupported OperationException
The of method applies only to interfaces, not to implementation classes.
Set and Map cannot call the of method with duplicate elements. If duplicate, Illegal ArgumentException will be thrown.
import java.util.*; public class TestJ9of { public static void main(String[] args) { List<String> lst = List.of("a", "b"); // lst.add("c");//UnsupportedOperationException System.out.println(lst); // lang.IllegalArgumentException: duplicate element: a Set<String> set = Set.of("a", "b"); // Set<String> set = Set.of("a", "b","a"); System.out.println(set); Map<Integer, String> map = Map.of(1, "A", 2, "B"); System.out.println(map); } }