The forEach(BiConsumer) method of HashMap class perform the BiConsumer operation on each entry of hashmap until all entries have been processed or the action throws an exception. The BiConsumer operation is a function operation of the key-value pair of hashtable performed in the order of iteration. Method traverses each element of Hashtable until all elements have been processed by the method or an exception occurs. Exceptions thrown by the Operation are passed to the caller.
Syntax:
public void forEach(BiConsumer action)
Parameters: This method accepts a parameter action of BiConsumer type that represents what action are to be performed on the HashMap elements.
Returns: This method do not return anything.
Exceptions: This method throws NullPointerException if the action is null.
Below programs illustrate the forEach(BiConsumer) method:
Program 1: to traverse a hashmap using action
| // Java program to demonstrate// forEach(BiConsumer) method. Âimportjava.util.HashMap;importjava.util.Map;importjava.util.function.BiConsumer; ÂpublicclassGFG { Â    // Main method    publicstaticvoidmain(String[] args)    { Â        // create a HashMap and add some values        Map<String, Integer> map            = newHashMap<>(); Â        map.put("geeks", 55);        map.put("for", 13);        map.put("geeks", 22);        map.put("is", 11);        map.put("heaven", 90);        map.put("for", 100);        map.put("geekies like us", 96); Â        // creating a custom action        BiConsumer<String, Integer> action            = newMyBiConsumer(); Â        // calling forEach method        map.forEach(action);    }} Â// Defining Our Action in MyBiConsumer classclassMyBiConsumer implementsBiConsumer<String, Integer> { Â    publicvoidaccept(String k, Integer v)    {        System.out.print("Key = "+ k);        System.out.print("\t Value = "+ v);        System.out.println();    }} | 
Key = geeks Value = 22 Key = for Value = 100 Key = is Value = 11 Key = heaven Value = 90 Key = geekies like us Value = 96
Program 2:
| // Java program to demonstrate// forEach(BiConsumer) method. Âimportjava.util.HashMap;importjava.util.Map;importjava.util.function.BiConsumer; ÂpublicclassGFG { Â    // Main method    publicstaticvoidmain(String[] args)    { Â        // Create a HashMap        // and add some values        Map<String, Integer> map            = newHashMap<>(); Â        map.put("geeks", 55);        map.put("for", 13);        map.put("geeks", 22);        map.put("is", 11);        map.put("heaven", 90);        map.put("for", 100);        map.put("geekies like us", 96); Â        // creating an action        BiConsumer<String, Integer> action            = newMyBiConsumer(); Â        // calling forEach method        map.forEach(action);    }} Â// Defining Our Action in MyBiConsumer classclassMyBiConsumer implementsBiConsumer<String, Integer> { Â    publicvoidaccept(String k, Integer v)    {        System.out.println("Key: "+ k                           + "\tValue: "+ v); Â        if("for".equals(k)) {            System.out.println("Its the "                               + "highest value\n");        }    }} | 
Key: geeks Value: 22 Key: for Value: 100 Its the highest value Key: is Value: 11 Key: heaven Value: 90 Key: geekies like us Value: 96

 
                                    







