To retrieve elements from a Map, you can use any of the following four methods:
- V get(Object K): It returns the value by the provided key, or null if the provided key is not in the map
- V getOrDefault(K, V): It returns the value by the provided key, or the provided (default) value if the provided key is not in the map
- Map.Entry<K,V> entry(K,V): A static method that converts the provided key-value pair into an immutable object of Map.Entry (immutable means it can be read, but cannot be changed)
- Map<K,V> ofEntries(Map.Entry<? extends K,? extends V>... entries): It creates an immutable map based on the provided entries
The following code demonstrates these methods:
Map<Integer, String> map = new HashMap<>();map.put(1, null ...