Skip to main content

Posts

Showing posts from December, 2019

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…