BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

How to loop HashMap in java

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

In this tutorial we will learn how to loop HashMap using following methods:

  1. For loop
  2. While loop + Iterator

Example:
In the below example we are iterating the HashMap using both the methods (for loop and while loop). In while loop we have used the iterator.

package beginnersbook.com;
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
public class Details
{
    public static void main(String [] args)
    {
        HashMap<Integer, String> hmap = new HashMap<Integer, String>();
        //Adding elements to HashMap
        hmap.put(11, "AB");
        hmap.put(2, "CD");
        hmap.put(33, "EF");
        hmap.put(9, "GH");
        hmap.put(3, "IJ");

        //FOR LOOP
        System.out.println("For Loop:");
        for (Map.Entry me : hmap.entrySet()) {
          System.out.println("Key: "+me.getKey() + " & Value: " + me.getValue());
        }

        //WHILE LOOP & ITERATOR
        System.out.println("While Loop:");
        Iterator iterator = hmap.entrySet().iterator();
        while (iterator.hasNext()) {
             Map.Entry me2 = (Map.Entry) iterator.next();
          System.out.println("Key: "+me2.getKey() + " & Value: " + me2.getValue());
        } 
    }
}

Output:

For Loop:
Key: 2 & Value: CD
Key: 3 & Value: IJ
Key: 33 & Value: EF
Key: 9 & Value: GH
Key: 11 & Value: AB
While Loop:
Key: 2 & Value: CD
Key: 3 & Value: IJ
Key: 33 & Value: EF
Key: 9 & Value: GH
Key: 11 & Value: AB

References:

  • Iterator javadoc
  • HashMap documentation

Top Related Articles:

  1. How to loop LinkedList in Java
  2. How to synchronize HashMap in Java with example
  3. How to sort Hashtable in java
  4. Java 8 – Filter null values from a Stream
  5. How to sort HashMap in Java by Keys and Values

Tags: Collections, Java-HashMap

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Mathan says

    November 22, 2014 at 8:13 AM

    Hi Chaitanya, Thank you very much for your wonderful and Helpful Articles. As a beginner, your website helps me a lot. thanks so much.

    for this article, could you please advice me which loop will be efficient and can be best one for any appliance? since i am a beginner I could not give a use case for this.

    Reply
  2. Aditya says

    February 6, 2015 at 9:10 AM

    Thanks you for the article could you please also let us know how the loop can be done to add data into the hashmap.

    Reply
  3. Saurabh says

    May 17, 2015 at 4:31 PM

    Appreciate your efforts Chaitanya…Thanks for helping in learning collections specially

    Reply

Leave a Reply Cancel reply

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

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap