Skip to main content

Different ways to iterate HashMap in Java

How to Iterate hashmap in Java

In this article, we are going to see how we can iterate hashmap in java. Let's have a look at different ways of iteration.
The below created HashMap is going to be iterate in this post:
// Map to store Roll no. of students with their name
Map<Integer, String> hashMap = new HashMap<>();
hashMap.put(101, "Shubham");
hashMap.put(102, "Jatin");
hashMap.put(103, "Nikhil");
hashMap.put(104, "Priyanka");
hashMap.put(105, "Kajal");

How to iterate HashMap in Java using forEach() method

In this section, we see forEach() method to iterate hashmap in java, java provides forEach() method which takes an argument of BiConsumer, a BiConsumer is a consumer which takes 2 arguments and consume them.
(key, value) -> System.out.println(key + ":" + value)
When we invoked the forEach() method onto a map, then the BiConsumer provided as the argument will be called and execute for each and every entry or key-value pair stored in the hashmap. For example:
hashMap.forEach(
    (key, value) -> System.out.println(key + ":" + value)
);

Collections within HashMap in Java

To iterate hashmap in java, we can also fetch a collection view from the hashmap.A Java Hashmap provides 3 collections over a hashmap namely Entry Set, Key Set and Value Set. Let see how to iterate hashmap in java using each of the collection views one-by-one.

How to Iterate HashMap in Java using Entry Set

To iterate hashmap in java using Entry Set, we first need to understand Map.Entry<K,V> interface of java.util package. An instance or object of this interface shows a key-value pair stored in a hashmap. By using entrySet() method of hashmap, we can fetch the Entry Set i.e., set of all entries or key-value pairs and iterate using the entry set.
Set<Entry<Integer, String>> entrySet = hashMap.entrySet();
Using forEach() method :
entrySet.forEach(
     entry -> System.out.println(entry.getKey() + ":" + entry.getValue())
);
Using for-Each loop :
for (Entry<Integer, String> entry : entrySet) {
     System.out.println(entry.getKey() + ":" + entry.getValue());
}
Using iterators :
Iterator<Entry<Integer, String>> iterator = entrySet.iterator();
while (iterator.hasNext()) {
     Entry<Integer, String> entry = iterator.next();
     System.out.println(entry.getKey() + ":" + entry.getValue());
}

How to Iterate HashMap in Java using Key Set

In this section, we see the approach of iterate hashmap in java using Key Set. The keySet() method is used to fetch the Key Set i.e, the set of all keys stored in hashmap. Then we can use keys to iterate over the hashmap.
Set<Integer> keySet = hashMap.keySet();
Using forEach() method :
keySet.forEach(
     key -> System.out.println(key +":"+hashMap.get(key))
);
Using for-Each loop :
for(Integer key : keySet) {
     System.out.println(key +":"+hashMap.get(key));   
}
Using iterators :
Iterator<Integer> iterator = keySet.iterator();
while(iterator.hasNext()) {
     Integer key = iterator.next();
     System.out.println(key +":"+hashMap.get(key));
}

How to Iterate HashMap in Java using Value Set

In this section, we see the approach of iterate hashmap in java using Value Set, this approach can be used where we only need to use the values of the hashmap not the entire map with keys and values. The values() method is used to fetch the Value Set i.e, the list of all values stored in the hashmap.
Collection<String> values = hashMap.values();
Using forEach() method :
values.forEach(value -> System.out.println(value));
// Or By Method Reference
values.forEach(System.out::println);
Using for-Each loop :
for (String value : values) {
     System.out.println(value);
}
Using iterators :
Iterator<String> iterator = values.iterator();
while (iterator.hasNext()) {
     String value = iterator.next();
     System.out.println(value);
}

So, these are some methods or approaches represents how we can iterate hashmap in java. Of-course there are also exists a lot of other methods for iteration, but these are the few that are easy to use and comes in usual coding practices.

Related Article

Comments

Popular posts from this blog

How HashMap works internally in java

How Hashmap works Internally in Java In this article, we are going to see how HashMap internally works in java. Also, we will have a look at what Java 8 made changes on the internal working of Hashmap to make it faster. What is Hashmap and Hashing in java? How Hashmap works Internally in Java is majorly dependent upon the Hashing Principle. So, Before going to learn how HashMap works internally in java, lets first understand what is HashMap and hashing. HashMap : A HashMap is a map used to store mappings of key-value pairs. Also, it works on the Principle of Hashing. To know more about the HashMap, visit this article: HashMap in Java Hashing Principle : Simply, Hashing is a method used to produce an integer value from an object and this integer value known as the hash value. In HashMap, the key object is used for Hashing.
Internal Struture of the HashMap in java For internal working of HashMap, HashMap maintains an array of bucket, each bucket is a linked-list and linked list is a li…

HashMap in Java

HashMap in Java In this article, we are going to learn about the most important class of Java known as HashMap. HashMap in java is the most used map interface and it is used in our regular or day-to-day programming practices.
What is HashMap in java? Lets first start with basic definition of HashMap,
HashMap is a map which store key-value pair, where each key is mapped with a value.
HashMap defined in java.util package and implements map interface which provides functionalities to perform basic operation like store, retrieve, size of the map etc. It is a part of Java Collection Framework. Time complexity of HashMap in Java? : Time complexity to store and retrieve data from the HashMap is O(1) in the Best Case. But it can be O(n) in the worst case and after the changes made in Java 8 the worst case time complexity can be O(log n) atmost. Internal working of HashMap in java HashMap maintains an array of the buckets, where each bucket is a linked-list and the linked list is a list of…

ArrayList in Java

ArrayList in Java In this article, we are going to discuss ArrayList in Java. ArrayList is a class from the Java Collection Framework and widely in our programs. We use ArrayList as a substitute for the array. So, it is important to know ArrayList in detail. We are going to see what is ArrayList in Java, how we can create the instance of ArrayList and the methods provided by the ArrayList in Java.

If you already familiar to ArrayList, you may learn common programs related to ArrayList.
Table of ContentsWhat is ArrayList in Java?Hierarchy of the ArrayList in java?How to create objects of ArrayList in Java?How to create an ArrayList of Custom Objects?How to iterate ArrayList in Java?How to get the elements by index from ArrayList in Java?How to check whether an elements is stored in the ArrayList in Java?How to remove elements from the ArrayList in Java?Few more methods of ArrayList in Java?
What is ArrayList in Java? ArrayList in Java is a List in which we can store and retrieve elemen…