• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Test Case Examples
  • Interview Questions
  • Interview Questions Asked
  • Java
  • Selenium
  • Manual Testing
  • SQL Tutorial For Beginners
  • Difference
  • Tools
  • Contact Us
  • Search
SoftwareTestingo Â» Java Â» Java Tutorial Â» Map In Java

Map In Java

Last Updated on: May 1, 2022 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • What is the Java Map Interface?
  • Why and when to use Maps?
  • Working of Map
  • Classes that implement Map
  • Methods of Map

The Map interface in Java is a way of representing a mapping between a key and value. It is often misunderstood to be part of the Collection interface. This article will help you understand how maps work in Java.

Post On:Map In Java
Post Type:Java Tutorials
Published On:www.softwaretestingo.com
Applicable For:Freshers & Experience
Get Updates:SoftwareTestingo Telegram Group

What is the Java Map Interface?

The Map interface in Java is a part of java.util package and it stores data in key-value pairs. It does not allow duplicate keys. Some people might misunderstand the Map interface as being a subtype of the Collections interface, but that is not true. The Map interface functions differently from the Collection interface.

What Is Map
What Is Map

Why and when to use Maps?

There are a number of classes in the Collections Framework that implement the Map interface. Each class has different functionality and level of thread safety. The most common implementation is HashMap, so we’ll be using this for most of our examples.

Collections Framework
Collections Framework

Working of Map

In Java, elements of Map are stored in key/value pairs. Each key corresponds to a unique value. A map cannot contain duplicate keys, and each key can only be associated with one value.

Map Key Value
Map Key Value

In the above diagram we have two columns called Keys and Values. Now if you want to access any value then you need the help of key.

Java Tutorial
  • Queue In Java
  • Java Set
  • Java ArrayList
  • SortedSet In Java
  • Java HashSet
  • LinkedHashSet In Java
  • Java TreeSet

Classes that implement Map

As we know Map is an interface, so we cannot create objects from it directly. To use the functionality of the map we have to use those classes which have implement the map interface. Such as:

  • HashMap
  • EnumMap
  • LinkedHashMap
  • WeakHashMap
  • TreeMap

Interfaces that extend Map

The Map interface has several subinterfaces, including the following:

  • SortedMap
  • NavigableMap
  • ConcurrentMap
Java Map SubInterface
Java Map SubInterface

Methods of Map

To use Map in Java, we must first import the java.util.Map package. Once imported, we can create a map by following these steps:

// Map implementation
Map<Key, Value> numbers = new HashMap<>();
Map<Key, Value> numbers = new LinkedHashMap<>();
Map<Key, Value> numbers = new TreeMap<>();

The Map interface in Java can be used with the classes that implement it to perform various operations. Some of these primary operations include:

  • Adding elements
  • Removing elements
  • Changing elements
  • Iterating through the map

Adding Into Map

The example below shows how to use the put() method to add elements to a map. The HashMap class is used here to implement Java maps.

package com.SoftwareTestingO.collections;
import java.util.HashMap;
import java.util.Map;

public class MapAdding 
{
	public static void main(String[] args) 
	{
		// Default Initialization of a Map
		Map<Integer, String> hm1 = new HashMap<>();

		// Initialization of a Map using Generics
		Map<Integer, String> hm2= new HashMap<Integer, String>();

		// Inserting the Elements
		hm1.put(1, "Software");
		hm1.put(2, "Testingo");
		hm1.put(3, "Blog");

		hm2.put(new Integer(1), "Java");
		hm2.put(new Integer(2), "Linux");
		hm2.put(new Integer(3), "Data Structures");

		System.out.println(hm1);
		System.out.println(hm2);	
	}
}

Changing Elements:

If we want to change an element after adding it, we can use the put() method again. Since elements in a map are indexed using keys, we can change the value of a key by simply inserting the updated value for that key.

package com.SoftwareTestingO.collections;
import java.util.HashMap;
import java.util.Map;

public class MapChanging 
{
	public static void main(String[] args) 
	{
		// Default Initialization of a Map
		Map<Integer, String> hm1 = new HashMap<>();

		// Inserting the Elements
		hm1.put(1, "Software");
		hm1.put(2, "Testingo");
		hm1.put(3, "Blog");

		// Before Change
		System.out.println("Before Chganging Elements: "+hm1);
		hm1.put(3, "Software");
		
		// After Change
		System.out.println("After Chganging Elements: "+hm1);
	}
}

Removing Elements

To remove an element from a Map, you can use the remove() method. This method takes in a key value and removes the mapping for that key from the Map if it is present.

package com.SoftwareTestingO.collections;

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

public class MapRemoving 
{
	public static void main(String[] args) 
	{
		// Default Initialization of a Map
		Map<Integer, String> hm1 = new HashMap<>();

		// Inserting the Elements
		hm1.put(1, "Software");
		hm1.put(2, "Testingo");
		hm1.put(3, "Blog");

		// Before Change
		System.out.println("Before Chganging Elements: "+hm1);
		
		//Removing the Element
		hm1.remove(3);
		
		// After Change
		System.out.println("After Chganging Elements: "+hm1);
	}
}

Iterating through the Map

There are several ways to iterate through a Map. The most common way is to use a for-each loop and get the keys. The value of each key can be found by using the getValue() method.

package com.SoftwareTestingO.collections;

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

public class MapIterating 
{
	public static void main(String[] args) 
	{
		// Default Initialization of a Map
		Map<Integer, String> hm2 = new HashMap<>();

		// Inserting the Elements
		hm2.put(new Integer(1), "Java");
		hm2.put(new Integer(2), "Linux");
		hm2.put(new Integer(3), "Data Structures");

		for(Map.Entry element: hm2.entrySet())
		{
			int key=(int) element.getKey();
			String value=(String) element.getValue();
			System.out.println(key +"--> "+value);
		}
	}
}

    String Pool in Java SoftwareTestingo
    String Pool in Java
    Wrapper Classes In Java
    Wrapper Class In Java
    Convert String to Char and Char Array in Java
    Convert String to Char and Char Array in Java
    Ternary Operator In Java
    Ternary Operator In Java
    Abstract Class In Java
    Abstract Class In Java
    Scanner Class in Java
    Scanner class in Java
    Exception Handling In Java
    Exception Handling In Java
    Polymorphism In Java
    Polymorphism In Java
    Keywords In Java Programming Language
    Keywords In Java Programming Language
    Static Binding and Dynamic Binding In Java
    Static Binding and Dynamic Binding in Java

    Filed Under: Java Tutorial

    Reader Interactions

    Leave a Reply Cancel reply

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

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Footer

    Java Tutorial for Beginners | Selenium Tutorial | Manual Testing Tutorial | SQL Tutorial For Beginners | GitHub Tutorial For Beginners | Maven Tutorial

    Copyright © 2023 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers