Map Interface & Its Classes

Introduction:-

It was introduced in 1.5version. Implementation of the map can be done using the Map interface and SortedMap interface. Its interface contains a Key-Value pair associated with it. Key values are unique. however, It is useful when we want to access the element based on keys. Map interface presents in Java.util package. so, The map does not allow duplicate keys but it allows duplicate values.  

Classes of Map Interface:

  1. HashMap.
  2. TreeMap.
  3. LinkedHashmap.

Example:-

Methods in Java:

  • int size():- It returns the size of the map.
  • boolean isEmpty():- If the value of the map does not contain a key-value pair then it returns true.
  • boolean contains value(Object value):- if the map contains at least one key-value pair then it returns true.
  • void clear():- remove all key value-pair from the map.
  • V get(Object key):- It thus takes a key as a parameter and returns the value for that given key and if the key is not found returns Null.
  • V put(key, value):- therefore, This method adds the key-value pair into the map. If the value is already assigned to the key it will just replace the existing value with a new value.
  • V remove(Object key):- also Removes the value of the key from the map.
  • Collection <V> values():- It however returns a collection view of the values contained in the map.

Classes Of Map Interface:- 

1) HashMap<K, V>:-

It was introduced in Java 1.2. It stores the data in a key-value pair. The HashMap works on the Data Structure HashTable.HashMap implements serializable, clonable, and map<k,v>.HashMap allows Null keys but only one key and allows multiple null values. To use this class we need to import java. util.HashMap.

Constructors Of HashMap:-
  1. HashMap():- This constructor is used to construct the default HashMap.
  2. HashMap(int capacity):- It is also In Use to initialize the capacity of the HashMap.
  3. HashMap(int capacity, float load factor):- It is used to initialize the capacity as well as load factor of the HashMap.
  4. HashMap(Map<? extends K,? extends V> n):- It is used to initialize the Hashmap using elements of map n.
Operations on HashMap:-

Output:-

Advantages:-
  1. HashMap is unsynchronised.
  2. It stores elements in Key-value pair.
  3. Fast Access.
Disadvantages:-
  1. Use more main memory.
  2. thus, Similar key type will return in more collision.
  3. Order is not maintain.

2) TreeMap:-

TreeMap is In Useto store keys and values. TreeMap is a red-black tree based on NavigableMap implementation. Hierarchy of TreeMap class is like TreeMap implements NavigableMap Interface further this interface extends SortedMap Interface and finally, SortedMap interface extends Map interface. TreeMap belongs to java. util package.

however, It can contain duplicate values and unique keys. It is sort up according to the natural ordering of keys. Another way of sorting a comparator is provide by the creation of the map. thus, It depends on which constructor is In Use. It is unsynchronized. It can have multiple null values but cannot have null keys.

Constructor in TreeMap:-

1)TreeMap():- It constructs a  new empty treemap using the natural ordering of keys. however, Keys must have to implement the comparable interface.

Eg:- TreeMap <Integer, String>TM=new TreeMap<Integer,String>();

2)Treemap(Comparator comp):- It constructs the empty treemap and ordering is complete using the comparator. We also need external specification for sorting. 

Eg:- TreeMap <Integer,String>TM=new TreeMap<Integer,String>(new funct());

3)TreeMap(Map M):- however, This constructor is in use to initialize the treemap by taking an element from Map which is sorted by the natural order of keys.

Eg:- Map<Integer, String> TM=new HashMap<Integer,String>();

4)TreeMap(SortedMap sm):- This constructor is also In Use to initialize the treemap by taking an element from SortedMap.

Eg:- SortedMap<Integer, String> TM=new Sortedmap <Integer,String>();

Operations on TreeMap:-

Output:-

Advantages:-
  1. It allows the key-value pairs stored in sorted order.
  2. Retrieval speed of TreeMap is fast even if there are more elements.
Disadvantages:-
  1. A large amount of data unsuitable for print.
  2. thus, Does not display negative values and data that varies in magnitude.

3) LinkedHashMap :-

It is thus a combination of Hashtable and LinkedList with predictable iteration order. however, The difference between a hashmap and a linked hashmap is that a hashmap uses Doubly LinkedList. thus, The linked hash map maintains the order. It extends the HashMap class. so, LinkedHashMap is the same as HashMap for Null Values means it supports only one Null key and many null values. 

Constructor Of LinkedHashMap:-

1)LinkedHashmap():- This is Default constructor of LinkedHashMap.

Eg. LinkedHashMap<k,v> LHM=new LinkedHashMap<k,v>();

2)LinkedHashMap(int Capacity):- This constructor is in use when we want to initialize the capacity.

Eg.LinkedHashMap<k,v> LHM=new LinkedHashMap<k,v>(int capacity);

3)LinkedHashMap(Map<? extends k,​? extends v> map):- This Constructor is also used when we want to initialize the LinkedHashMap with the elements of a specific map.

Eg.LinkedHashMap<k,v>LHM=new LinkedHashMap<k, v>(Map<? extends k,​? extends v> map);

4)LinkedHashMap(int capacity, float fill ratio):- This constructor is in use when we want to initialize the capacity. The fill ratio is similar to the load factor. It determines the after the capacity gets full it will automatically increase the capacity by fill ratio unit. The default value of the fill ratio is 0.75.

Eg.LinkedHashMap<k, v>LHM = new LinkedHashMap<k, v>(int capacity, float fill ratio);

5)LinkedHashMap(int capacity, float fill ratio, Boolean order):- This is the same as the above constructor only difference is we can initialize whether we want to maintain insertion order or not.

Eg.LinkedHashMap<k, v>LHM = new LinkedHashMap<k, v>(int capacity, float fill ratio, Boolean order);

Operations on LinkedHashMap:-

Output:-

Advantages: 
  1. Insertion order is maintain.
Disadvantages:-
  1. thus, To maintain order the LinkedList is use.
  2. More memory usage.

Conclusion on Map Interface:-  

however, We successfully studied and discussed the Map Interface and its classes of Java collection framework.

written by: Suraj Raut

reviewed by: Soutik Maity

If you are Interested In Machine Learning You Can Check Machine Learning Internship Program
Also Check Other Technical And Non Technical Internship Programs

Leave a Comment

Your email address will not be published. Required fields are marked *