Saturday, April 13, 2019

Java Collection Frame Work - Map Interface


Map Interface

Map is an Interface in java.utils package. However it does not implements java.utils.Collection interface, but Map is still the part of Collection API you can say it second half of Collection Cinema.

Feature of Map 


  • Map is used to store object in key value pair.
  • key and value both are Object, Primitive values(int,char etc) not allowed like other Collection .
  • Map does not implement Collection interface.
  • Like the Set interface Map only allowed unique keys. if you call put(k,v) more than once with same key than latest value replaces the existing value for given key. 
  • Value may be repeated
  • The key and value represent an  entry of the Map, called entries
  • value can be null 
  • key can hold one and only null value.
  • By default  entries in Map are unordered while some implementation of Map interface provide ordered and sorted entries e.g. TreeMap
  • Map are not synchronized while legacy class Hashtable and ConcurrentHashMap are synchronize , performance wise Map is best choice if thread safety is not a matter.
  • The Map took constant time to perform operation like get put contains etc because it use hash table algorithm.
  • Map is best choice for Mock Implementation.
  • if no element exist in Map , it will throw a NoSuchElementException

Internal Storage and Retrieve Strategy of Map

Terminology to know 
bucket - bucket is array 

Map used Hash Table algorithm to store entries. When we put an entry to Map , the Map use hashcode() and equals() method to find associate buckets. it then store entry into the bucket. Different key produce same hashcode value this situation called collision, so they are stored in same buckets.

When we retrieve an entry from Map, we pass the respective key. again hashcode obtain for that key to locate bucket. After locating bucket Map then does the equality check with key when the matched key found map return the value of that keys.


Abstract Method of Map Interface

Add entry to Map<K,V>
  • V put(K key, V value) - add an entry to Map, if key is found already in Map then current value is replaced by new value of this key.
  • void putAll(Map<? extends K , ? extends V>map) - Copies all the entry from passed map to this map
  • default V putIfAbsent(K key, V value): it is default method of Map interface, Checks if the given key is present. If present, returns the existing value and does nothing. If absent, it will store the new entry and return null.
Remove entry from Map<K,V>
  • V remove(Object key) - remove the entry from map of given key if it is presented
  • default boolean remove(Object key, Object value) - Removes the entry only if the given key has given value in the collection
  • default V replace(K key, V value)- Replaces the value of given key with given value. Note: replace happen only if the key is present in the collection
  • default boolean replace(K key, V oldValue, V newValue) -  It will replace the value only if the given key has given old value.
  • default void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) - It applies the given function on all the entries of the collection. 
  • Void clear() - clear Map  
Get a Value for given Key
  • V get(Object key) - return the value of given key if found , if not found then return null
  • deafult V getOrDefault(Object key, V defaultValue) - if given key found in Map return value otherwise return default value. 
Check if map contain a key
  • boolean containsKey(Object key) throws ClassCastException, NullPointerException
Check if map contain a value
  • boolean containsValue(Object value) throws ClassCastException, NullPointerException
Check if map is empty or not 
  • boolean isEmpty()

Iterating the keys of Map

  • Set<K> keySet() - retrurned SET view of key contained in the map
Iterating the entries of Map
  • Set<Map.Entry<K,V>> entrySet() - returned Set View of entries contained in the map, this method  used for fetching entries from Map.
Iterating the value of Map
  • Collection<V> values()

Static Method of Map Interface for Creating Immutable Map

1. static<K,V> Map<K,V> of() method - This is overloaded method , this method is used to create Immutable Map upto 10 entries only

static <K,V> Map<K,V> of (K k1, V v1); 
static <K,V> Map<K,V> of (K k1, V v1, K k2, V v2); 
static <K,V> Map<K,V> of (K k1, V v1, K k2, V v2, K k3, V v3); 
static <K,V> Map<K,V> of (K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4); 
static <K,V> Map<K,V> of (K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5); 
static <K,V> Map<K,V> of (K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6); 
static <K,V> Map<K,V> of (K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7); 
static <K,V> Map<K,V> of (K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8); 
static <K,V> Map<K,V> of (K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9); 
static <K,V> Map<K,V> of (K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10); 
for example : - 
Map<String,String> employee=Map.of("name","Yogesh");

2. static<K,V>Map.Entry<K,V> entry(K k, V v) :  

This method is used Map.Entry type of object.

3.  static<K,V> Map<K,V> ofEntries(Map.Entry<? extends K,?extends V>...) method -  This method take varags of type Map.Entry i.e. if we want to create inline Map of infinite entries then we use ofEntries() method.

for example : - 
Map<String,String> map=Map.ofEntries(
                                                    Map.Entry("Country","India"),
                                                    Map.Entry("Capital","Delhi")
                                            );
Immutable Map : Immutable Map are not modifiable after creation.  Java 9 Provide static method of() and ofEntries() for this purpose. if you try to modified Immutable Map you will get UnsupportedOperationException.

Children of Map Interface
  • java.util.SortedMap
  • java.util.NavigableMap
  • java.util.concurrent.ConcurrentMap
  • java.util.concurrent.ConcurrentNavigableMap

Implementation of Map Interface

  • AbstractMap
  • Attributes
  • AuthProvider
  • ConcurrentHashMap
  • ConcurrentSkipListMap
  • EnumMap
  • HashMap
  • Hashtable
  • IdentityHashMap
  • LinkedHashMap
  • PrinterStateReasons
  • Properties
  • Provider
  • RenderingHints
  • SimpleBindings
  • TabularDataSupport
  • TreeMap
  • UIDefaults
  • WeakHashMap

No comments:

Post a Comment