Java Map Interface

Java Map Interface

What is Java Map Interface? 

  • The Java Map interface is a part of the Java Collections Framework, which provides a way to store and manipulate groups of objects.
  • A Map is an object that maps keys to values, where each key is unique.
  • The Map interface provides methods for adding, removing, and accessing elements by their key.
  • To understand the Java Map Interface, Read the Complete Article.

How to use Map?:

To use a Map in Java, you need to follow these basic steps:

  • Import the Map class and the specific Map implementation that you want to use, such as HashMap or TreeMap.
  • Create a new instance of the Map implementation using the constructor.
  • Use the put() method to add key-value pairs to the map. The key is used to retrieve the associated value later.
  • Use the get() method to retrieve values from the map using the key.
  • Use the remove() method to remove key-value pairs from the map.
  • Use the keySet(), values(), or entrySet() methods to get a Set, Collection, or Set of Map.Entry objects, respectively, that you can then iterate over to access all key-value pairs in the map.
Java Map Interface
Java Map SubInterafce

Methods in the Map interface:

  • Some of the most commonly used methods in the Map interface are:
    • put(key, value) – Adds a key-value pair to the map.
    • get(key) – Returns the value associated with the given key, or null if the key is not found.
    • remove(key) – Removes the key-value pair associated with the given key.
    • containsKey(key) – Returns true if the map contains the given key, otherwise false.
    • containsValue(value) – Returns true if the map contains the given value, otherwise false.
    • size() – Returns the number of key-value pairs in the map.

Let’s look at the Implementation of the Map Interface to perform certain operations.

Example 1: Implementation of the Map Interface using HashMap Class

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

public class Main
{
  public static void main (String[]args)
  {
    // create a new HashMap instance
    Map < String, Integer > myMap = new HashMap <> ();

    // add key-value pairs to the map
    myMap.put ("Ashish", 25);
    myMap.put ("Ayush", 30);
    myMap.put ("Rishi", 42);

    // get the value associated with a key
    int johnAge = myMap.get ("Ashish");
      System.out.println ("Ashish's age is " + johnAge);

    // remove a key-value pair from the map
      myMap.remove ("Ayush");

    // print the size of the map
      System.out.println ("Size of the map: " + myMap.size ());

    // iterate over the key-value pairs in the map
    for (Map.Entry < String, Integer > entry:myMap.entrySet ())
      {
	String name = entry.getKey ();
	int age = entry.getValue ();
	  System.out.println (name + " is " + age + " years old.");
      }
  }
}

Output

Ashish's age is 25
Size of the map: 2
Ashish is 25 years old.
Rishi is 42 years old.

Example 2 : Implementation of the Map Interface using Tree Map Class

Run
import java.util.Map;

import java.util.TreeMap;

 
public class Main
{
  
public static void main (String[]args)
  {
    
      // create a new TreeMap instance
      Map < String, Integer > myMap = new TreeMap <> ();
    
 
      // add key-value pairs to the map
      myMap.put ("Ayush", 25);
    
myMap.put ("Ashley", 30);
    
myMap.put ("Bob", 42);
    
 
      // get the value associated with a key
    int AyushAge = myMap.get ("Ayush");
    
System.out.println ("Ayush's age is " + AyushAge);
    
 
      // remove a key-value pair from the map
      myMap.remove ("Ashley");
    
 
      // print the size of the map
      System.out.println ("Size of the map: " + myMap.size ());
    
 
      // iterate over the key-value pairs in the map
  for (Map.Entry < String, Integer > entry:myMap.entrySet ())
      {
	
String name = entry.getKey ();
	
int age = entry.getValue ();
	
System.out.println (name + " is " + age + " years old.");

} 
} 
} 

Output

Ayush's age is 25
Size of the map: 2
Ayush is 25 years old.
Bob is 42 years old.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription