How does HashMap forEach() Method Work in Java 8? Last Updated : 14 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report The HashMap is a part of collections in the java.util package. The HashMap in Java stores the data in the form of a key-value pair. The HashMap takes the generic parameters for the key along with the value. Similarly, like we instantiate the classes in Java we also instantiate the hash map like we create objects in Java. In this article, we will be using the forEach() method. The forEach() method in Java iterates over the hashMap and we can perform any action on the key and values in the method. Syntax:hashMap.forEach( (key , value) -> { //action to be performed } ); The forEach() method takes the parameters a key and value variable which represents the key and value pair.After that, we used -> which specifies that it is a lambda expression. The lambda expression specifies the action to be performed in the forEach () method.Note: The forEach() method doesn't have any return value. Program to Understand forEach() Method in HashMap in Java 8Below is the Program to Understand forEach() method in HashMap in Java 8: Java // Java program to understand forEach() Method of HashMap import java.util.*; // Driver Class public class GFG { // Main Function public static void main(String args[]) { // Initializing the HashMap in the java HashMap<Integer, String> hm = new HashMap<>(); // storing some of the key value pairs in java hm.put(1, "Krishna"); hm.put(2, "Harsha"); hm.put(3, "Rama"); hm.put(4, "Arfan khan"); hm.put(5, "Joseph"); // Variables are taken as key and value for // Respective key-value pair in HashMap hm.forEach((key, value) -> { // action to performed is given here String str = value.toUpperCase(); int keyvalue = key * 10; System.out.println("Key : " + keyvalue + " Value : " + str); }); } } OutputKey : 10 Value : KRISHNA Key : 20 Value : HARSHA Key : 30 Value : RAMA Key : 40 Value : ARFAN KHAN Key : 50 Value : JOSEPH Explanation of the Program:In the above program, we have created a HashMap with the help of the java.util package.The HashMap is instantiated with the object hm.We have used the HashMap object to use the forEach() method in the class HashMap class.The forEach method in the hashmap takes Biconsumer as parameters and uses lambda expression to perform some action with it.The HashMap created in the above program consists of key which is of string type and value which is of integer type.So, when we use the forEach methosd we have specified in the lambda expression to convert the key into uppercase and the value multiplied by 10.In this way, we can use the forEach method in the Java 8. Comment More infoAdvertise with us Next Article How does HashMap forEach() Method Work in Java 8? B boora_harsha_vardhan Follow Improve Article Tags : Java Java Programs Java-HashMap Java 8 Java Examples +1 More Practice Tags : Java Similar Reads How to Convert ArrayList to HashMap Before Java 8? ArrayList is a resizable array. It is under the java package java.util. It gives dynamic arrays in Java. ArrayList is useful in programs where a lot of changes in the array are required but these are also slower than the standard arrays. The elements in an ArrayList can be added and removed whenever 4 min read Load Factor in HashMap in Java with Examples HashMap is a class that implements the Map interface of Java Collections Framework. The most important feature of a HashMap is that it has a constant time performance for retrieval and insertion. The two factors that dictate the performance of a HashMap are: Initial CapacityLoad Factor Before we exp 5 min read How to Iterate HashMap in Java? HashMap is a part of Javaâs collection providing the basic implementation of the Map interface of Java by storing the data in (Key, Value) pairs to access them by an index of another type. One object is listed as a key (index) to another object (value). If you try to insert the duplicate key, it wil 5 min read Creating HashMap from Other Maps in Java Map interface present in java.util package represents a mapping between a key and a value. The Map interface is not a subtype of the Collection interface. Therefore, it behaves a bit differently from the rest of the collection types. A map contains unique keys. There are three main types of maps in 3 min read Convert HashMap to LinkedList in Java HashMap is similar to the HashTable, but it is unsynchronized. It allows to store the null keys as well, but there should be only one null key object and there can be any number of null values. LinkedList is a part of the Collection framework present in java.util package. This class is an implementa 2 min read Like