Java Map interface with Example

A map in Java is an object that stores the data in between keys and values or key/values pairs. Keys and values both are objects. A map contains unique key but values may be duplicated. There is one key point about maps that are important to mention at the outset; they don't implement the collection interface. The map interface maps the unique key to values. A key is an object that is used to retrieve a value at a later date. If you have a key and a value then you can store the value in the map object. After the value is stored, you can retrieve by using its key.

Java Map Hierarchy:

There are two interfaces for implementing a Map in Java these are Map and SortedMap, and three classes these classes are HashMap, LinkedHashMap, and TreeMap.
Java Map interface with example

Why we use Maps?

if we want to store the data in keys and values or key/values pair then we should go for maps. Some examples are:
1. A map of pin code and cities.
2. A map of student roll no: and student marks.
3. A map of employee address and employee mobile no:
4. A map of universities and professors.

Methods of Java Map interface:

1. void clear(): This method is used to removes all the key/values pairs from the invoking maps.
2. boolean containsKey(Object k): This method returns true if the invoking map contains k as a key. Otherwise, it returns false.
3. boolean containsValue(Object v): This method returns true if the invoking map contains v as a value. Otherwise, it returns false.
4. boolean equals(Object obj): This method is used to compare the specified object with the map.
5. V get(Object k): This method returns the value associated with the key k and it returns null if the key is not found.
6. int hashCode(): This method returns the hashcode of the invoking maps.
7. boolean isEmpty(): It returns true if the invoking map is empty. Otherwise, it returns false.
8. V put(key K, value V): This method is used to put an entry in this map.
9. void putAll(Map map): It is used to insert the specified maps in this map.
10. V remove(Object k): This method is used to removes the entry whose key equals to k.
11. int size(): This method returns the number of key/value pair in the map.
12. Set keySet(): This method is used to returns the set that contains the keys in the invoking map.
13. Set entrySet(): This method is used to returns a set that contains the entries in the map.
14. Collection values(): This method is used to returns a collection containing the values in the map.


Java Map interface Example


import java.util.Map;
import java.util.HashMap;

class JavaMapExample{
 public static void main(String args[]){
  
  //creating a map
  Map<Integer, String> map = new HashMap<Integer, String>();
  
  //adding values in this map
  map.put(101, "Rahul");
  map.put(102, "Amit");
  map.put(103, "Vijay");
  map.put(104, "Suresh");
  map.put(105, "John");
  
  System.out.println("Map entries are: "+map);
  
  //returns the value associated with the key 
  System.out.println("getting value is: "+map.get(103));
  
  /*returns true if the invoking map contains k as a key. 
  Otherwise, it returns false.*/
  System.out.println("Check map contains specified key or not: "+map.containsKey(104));
  
  /*returns true if the invoking map contains v as a value. 
  Otherwise, it returns false.*/
  System.out.println("Check map contains specified value or not: "+map.containsValue("Ramesh"));
  
  //returns the number of key/values pair in the map
  System.out.println("key/values pair in the map is: "+map.size());
  
  //removes the entry whose key equals to k(105).
  System.out.println("Removing entry: "+map.remove(105));
  
  //returns the hashcode of the invoking maps.
  System.out.println("The map hash code is: "+map.hashCode());

                //return the set that contains the keys in the invoking map
  System.out.println("Key set is: "+map.keySet());
  
  //removes all the key/values pairs from the invoking maps.
  map.clear();
  System.out.println("Removing all entries from map: "+map);
  
 }
}

Output:
Java Map with example



You may also enjoy collection articles:
How to remove a specified element in the ArrayList?
LinkedList vs ArrayList in a collection
How to implement a stack data structure in Java?
NavigableSet in Java with Example
TreeSet in Java with Example
Comparable vs Comparator in Java with Example

ArrayDeque in Java with an example

Java Map interface with Example Java Map interface with Example Reviewed by Prashant Srivastava on March 18, 2019 Rating: 5

No comments:

Powered by Blogger.