MyJavaCode.com

Tutorial and How to Guide on Java Programming. Topics covered include Java Programming Language - Core Java, Spring, Webservices, REST, Hibernate, Maven and Microservices.

  • Core Java
  • REST API’s
  • Spring boot
  • Hibernate
  • Interview Questions
  • Contact Us
  • Projects
  • Offerings

How to iterate Java HashMap

January 5, 2025 By Prasanna Manjunatha

HashMap is one of the data structure in Java which implements the Map Interface. It makes use of the key value pair to store the data. Following are the four ways to iterate Java HashMap. The same approach can be used for iterating over other Map implementations like HashTable, TreeMap, etc.

  • JaCoCo Code Coverage in Springboot Applications : A Comprehensive Guide
  • How to Password Protect a PDF File using Java : A Step-by-Step Guide

1.  Using Lambda Expression

Lambda Expressions were introduced as part of Java 8 and it is the easiest option to iterate the HashMap.

Map<String,Integer> map = new HashMap<String,Integer>(); 
map.put("Robert", 34); map.put("Kiran", 29); 
map.put("Andy", 44); 
map.forEach((key,value) -> { 
  System.out.println(key); 
  System.out.println(value);
});

 

2. Using entrySet & advanced for loop

Map<String,Integer> map = new HashMap<String,Integer>(); 
map.put("Robert", 34); 
map.put("Kiran", 29); 
map.put("Andy", 44); 
for(Map.Entry<String,Integer> entry : map.entrySet()) { 
  System.out.println(entry.getKey()); 
  System.out.println(entry.getValue()); 
}

 

Also Read: How to Generate MD5 Hash in Java

3. Using keySet & advanced for loop

Map<String,Integer> map = new HashMap<String,Integer>();
        
map.put("Robert", 34);
map.put("Kiran", 29);
map.put("Andy", 44);

for(String key : map.keySet())
{
    System.out.println(key);
    System.out.println(map.get(key));
}

 

4. Using Iterator interface

Map<String,Integer> map = new HashMap<String,Integer>();
        
map.put("Robert", 34);
map.put("Kiran", 29);
map.put("Andy", 44);

for(String key : map.keySet())
{
    System.out.println(key);
    System.out.println(map.get(key));
}
email
print

About Prasanna Manjunatha

Prasanna is a Toronto based Java Consultant with more than 15 years of Software Development experience. His areas of expertise include Core Java, Spring boot, REST, Microservices, Hibernate, NoSQL, Docker, Kubernetes and AWS.

Currently Trending

  • How to Password Protect a PDF File using Java
  • How to Map Composite Key in Hibernate
  • How to configure JaCoCo for Code Coverage in Spring boot Applications
  • Wildcard SSL Certificate Configuration in JBoss
  • What Are REST APIs? A Beginner’s Guide for Java Developers
  • How to Update MySQL Collation for all tables in the schema
  • How to Extract Private Key from Java Keystore (.JKS) or .P12 File
  • How to Send Email In Java
  • Externalizing MyBatis SQL Queries in a Spring Boot Application
  • How to Deploy Exploded WAR file in JBoss EAP Server

Connect with us

  • Facebook
  • GitHub
  • LinkedIn
  • Twitter

TAG CLOUD

Axis2 Code Coverage CodeCoverage cron expression Data Structure Frameworks Hashing HashMap Hibernate IntelliJ java Java Mail jboss jpa 2.1 Maven MyBatis MySQL PDF Quartz REST REST API SOAP Springboot Spring boot SQL Tools Tutorial web service

All time Popular Posts

  • How to Extract Private Key from Java Keystore (.JKS) or .P12 File
  • How to check whether a Collection is Null or Empty in Java
  • Automate Audit Fields in Spring Boot with MyBatis: A Generic Solution for CRUD Operations
  • How to Generate MD5 Hash in Java
  • How to Solve Maven invalid CEN header error
  • How to Deploy Exploded WAR file in JBoss EAP Server
  • How to configure JaCoCo for Code Coverage in Spring boot Applications
  • How to Password Protect a PDF File using Java
  • How to Create Cron Expression for Quartz Scheduler
  • How to Send Email In Java

Copyright © 2026 MyJavaCode.com