Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » Java » Iterate Map in Java

  • Python Tuple and Basic Tuple Operations with Examples
    Python Tuple and Basic Tuple Operations with Examples Python
  • World Earth Day in Hindi | पृथ्वी दिवस कब और क्यों मनाया जाता है?
    Earth Day in Hindi, Theme | पृथ्वी दिवस कब और क्यों मनाया जाता है? General Knowledge
  • Importance of Thread Synchronization in Java
    Importance of Thread Synchronization in Java Java
  • Java Keywords
    Java Keywords Java
  • Famous Slogans given by famous Personalities Indian leaders
    Famous Slogans given by famous Personalities Indian leaders General Knowledge
  • Java LinkedList
    Java LinkedList Java
  • Python List
    Python List Python
  • Maharishi Valmiki in Hindi, Biography, Dacoit Ratnakar, Jayanti
    Maharishi Valmiki in Hindi, Biography, Dacoit Ratnakar, Jayanti Biography

Iterate Map in Java

Posted on August 14, 2021October 4, 2021 By admin No Comments on Iterate Map in Java
Iterate Map in Java

There are many ways to iterate a Map in Java. First let’s understand about Map in Java.

Map in Java stores elements in the form of key and value pairs. If the key is provided then map returns corresponding value.

There are following ways to iterate map in Java:

  • Map.entrySet() with For-Each loop
  • keySet() and values()
  • forEach(action) method
  • Searching of keys in map
  • By using iterators

Table of Contents

  • 1. Using Map.entrySet() with For-Each loop
  • 2. Using keySet() and values()
  • 3. Iterate map in java Using forEach(action) method
  • 4. Searching of keys in map
  • 5. Iterate map in java using iterators
    • More about collection
  • Can you store a primitive data type into a collection?

1. Using Map.entrySet() with For-Each loop

In this method of iteration, we use Map.Entry<K,V> in which Entry means key value pair. We get a collection-view of the map from Map.entrySet(), whose elements are in this class.

                  import java.util.Map; 
import java.util.HashMap; 
 
public class GeekCer { 
  public static void main(String[] arg) { 
    Map<String,String> map = new HashMap<String,String>(); 
      
    // Add data as key-key value pair 
    map.put("ADAMS", "CLERK"); 
    map.put("BLAKE", "MANAGER"); 
    map.put("FORD", "ANALYST"); 
    map.put("MILLER", "CLERK"); 
    map.put("TURNER", "SALESMAN");
          
    for (Map.Entry entry : map.entrySet()) {  
        System.out.println("Key = " + entry.getKey() + 
                             ", Value = " + entry.getValue());
    }
  } 
} 

Output:
Key = TURNER, Value = SALESMAN
Key = BLAKE, Value = MANAGER
Key = MILLER, Value = CLERK
Key = ADAMS, Value = CLERK
Key = FORD, Value = ANALYST

2. Using keySet() and values()

In this program, we have iterated over keys using keySet() method and iterated over values using values() method. The keySet() method returns a set view of the keys contained in this map and values() method returns of the values contained in this map.

                  import java.util.Map; 
import java.util.HashMap; 
 
public class GeekCer { 
  public static void main(String[] arg) { 
    Map<String,String> map = new HashMap<String,String>(); 
      
    // Add data as key-key value pair 
    map.put("ADAMS", "CLERK"); 
    map.put("BLAKE", "MANAGER"); 
    map.put("FORD", "ANALYST"); 
    map.put("MILLER", "CLERK"); 
    map.put("TURNER", "SALESMAN");
          
    // Iteration over keys using keySet()
    for (String name : map.keySet()) {
      System.out.println("Key: " + name);
    }

    // Iteration over values using  values()
    for (String job : map.values()) {
      System.out.println("Value: " + job);
    }
  } 
} 

Output:
Key: TURNER
Key: BLAKE
Key: MILLER
Key: ADAMS
Key: FORD
Value: SALESMAN
Value: MANAGER
Value: CLERK
Value: CLERK
Value: ANALYST

3. Iterate map in java Using forEach(action) method

This technique available in Java 8, here we iterate a map using lambda expression and forEach(action) method.

                  import java.util.Map; 
import java.util.HashMap; 
 
public class GeekCer { 
  public static void main(String[] arg) { 
    Map<String,String> map = new HashMap<String,String>(); 
      
    // Add data as key-key value pair 
    map.put("ADAMS", "CLERK"); 
    map.put("BLAKE", "MANAGER"); 
    map.put("FORD", "ANALYST"); 
    map.put("MILLER", "CLERK"); 
    map.put("TURNER", "SALESMAN");
          
     map.forEach((key ,value) -> System.out.println("Key = "
            + key + ", Value = " + value)); 
  } 
} 

Output:
Key = TURNER, Value = SALESMAN
Key = BLAKE, Value = MANAGER
Key = MILLER, Value = CLERK
Key = ADAMS, Value = CLERK
Key = FORD, Value = ANALYST

4. Searching of keys in map

It a simple way of iteration, firstly we get the key from the map and by using key we get the value from the map.

                  import java.util.Map; 
import java.util.HashMap; 
 
public class GeekCer { 
  public static void main(String[] arg) { 
    Map<String,String> map = new HashMap<String,String>(); 
      
    // Add data as key-key value pair 
    map.put("ADAMS", "CLERK"); 
    map.put("BLAKE", "MANAGER"); 
    map.put("FORD", "ANALYST"); 
    map.put("MILLER", "CLERK"); 
    map.put("TURNER", "SALESMAN");
          
    for (String name : map.keySet()) {
      String job = map.get(name);
      System.out.println("Key = " + name + ", Value = " + job);
    } 
  } 
} 

Output:
Key = TURNER, Value = SALESMAN
Key = BLAKE, Value = MANAGER
Key = MILLER, Value = CLERK
Key = ADAMS, Value = CLERK
Key = FORD, Value = ANALYST

5. Iterate map in java using iterators

First of all, we create a iterator for the collection-view returned by the entrySet() method and using while loop we iterate the map.

                  import java.util.HashMap;
import java.util.Iterator;
import java.util.Map; 
 
public class GeekCer { 
  public static void main(String[] arg) { 
    Map<String,String> map = new HashMap<String,String>(); 
      
    // Add data as key-key value pair 
    map.put("ADAMS", "CLERK"); 
    map.put("BLAKE", "MANAGER"); 
    map.put("FORD", "ANALYST"); 
    map.put("MILLER", "CLERK"); 
    map.put("TURNER", "SALESMAN");
          
    Iterator<Map.Entry<String, String>> itr = map.entrySet().iterator();    
    while(itr.hasNext()) 
    { 
      Map.Entry<String, String> entry = itr.next(); 
      System.out.println("Key = " + entry.getKey() +  
                             ", Value = " + entry.getValue()); 
    } 
  } 
} 

Output:
Key = TURNER, Value = SALESMAN
Key = BLAKE, Value = MANAGER
Key = MILLER, Value = CLERK
Key = ADAMS, Value = CLERK
Key = FORD, Value = ANALYST

More about collection

We can not store primitive dat types in the collection objects hence map doesn’t store the primitive type of data. The main purpose of collection is to handle object only, but not primitive data types.

Can you store a primitive data type into a collection?

No, Collections store the elements in the form of object only. But you can store a primitive data in the form of object by using wrapper class concept.

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

Java

Post navigation

Previous Post: Difference between GenericServlet and HttpServlet
Next Post: Immutable class in Java | How to create Immutable class in Java

More Related Articles

Lambda Expression in Java 8 | Functional Interface | Example Lambda Expression in Java 8 | Functional Interface | Example Java
Java If-else Statement Java If-else Statement Java
Java Vector Java Vector Java
Java Keywords Java Keywords Java
Exception Handling in Java Exception Handling in Java Java
Java Switch Case Java Switch Case Java

Related Posts

  • Lambda Expression in Java 8 | Functional Interface | Example
    Lambda Expression in Java 8 | Functional Interface | Example Java
  • Java If-else Statement
    Java If-else Statement Java
  • Java Vector
    Java Vector Java
  • Java Keywords
    Java Keywords Java
  • Exception Handling in Java
    Exception Handling in Java Java
  • Java Switch Case
    Java Switch Case Java

Leave a Reply Cancel reply

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

Recent Posts

  • Maharishi Valmiki in Hindi, Biography, Dacoit Ratnakar, Jayanti
  • Structured Vs Unstructured Data in Hindi | Key Difference
  • Jhansi Ki Rani Lakshmi Bai History, Story, Information in Hindi
  • Elon musk Hindi : एलन मस्क हिंदी में, Autobiography,  Net Worth
  • World Environment Day in Hindi : Objective, Importance, Theme
  • Thomas Edison Biography in Hindi – थॉमस एडिसन जीवनी
  • International Nurses Day in Hindi | नर्स दिवस क्यों मनाते हैं?
  • Fork/Join Framework in Java | RecursiveTask, RecursiveAction
  • Python Tuple and Basic Tuple Operations with Examples
    Python Tuple and Basic Tuple Operations with Examples Python
  • World Earth Day in Hindi | पृथ्वी दिवस कब और क्यों मनाया जाता है?
    Earth Day in Hindi, Theme | पृथ्वी दिवस कब और क्यों मनाया जाता है? General Knowledge
  • Importance of Thread Synchronization in Java
    Importance of Thread Synchronization in Java Java
  • Java Keywords
    Java Keywords Java
  • Famous Slogans given by famous Personalities Indian leaders
    Famous Slogans given by famous Personalities Indian leaders General Knowledge
  • Java LinkedList
    Java LinkedList Java
  • Python List
    Python List Python
  • Maharishi Valmiki in Hindi, Biography, Dacoit Ratnakar, Jayanti
    Maharishi Valmiki in Hindi, Biography, Dacoit Ratnakar, Jayanti Biography
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved