Map in Java

In this tutorial, we will learn about the Map Interface in Java. Map stores the values in Key-Value pair and each pair is known as an entry.

 

Values in the Map can be duplicate but always must be unique. The Map interface is implemented by different Java classes, such as HashMap, HashTable, and TreeMap. Each class provides different functionality and can be either synchronized or not.

 

A map has the form Map <K, V> where:

K : Specifies the Keys

V : Specifies the Values

Map in Java

 

Methods in Map

  1. public Object put(object key,Object value): This method is used to insert an entry in this map.
  2. public void putAll(Map map): This method is used to insert the specified map in this map.
  3. public Object remove(object key): This method is used to delete an entry for the specified key.
  4. public Object get(Object key): This method is used to return the value for the specified key.
  5. public boolean containsKey(Object key): This method is used to search the specified key from this map.
  6. public boolean containsValue(Object value): This method is used to search the specified value from this map.

 

Implementation of  Map in Java

Map is an interface which can be implemented by three classes in Java –

  • HashMap : HashMap makes no guarantees concerning the order of iteration. HashMap doesn’t maintain the insertion order of elements.

  • TreeMap : TreeMap stores its elements in a red-black tree. Treemap orders its elements based on their values i.e Treemap sorts it’s elements on the basis of these values internally, and it’s is substantially slower than HashMap.

  • LinkedHashMap : LinkedHashMap orders its elements based on the order in which they were inserted.

In the coming tutorials, we will learn about the implementation of all these classes in brief.

Selenium Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

0 Comment

Leave a Reply