How does HashMap forEach() Method Work in Java 8? Last Updated : 23 Jul, 2025 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. B boora_harsha_vardhan Follow Article Tags : Java Java Programs Java-HashMap Java 8 Java Examples +1 More Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings8 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java9 min readAccess Modifiers in Java4 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like