HashMap forEach(BiConsumer) method in Java with Examples Last Updated : 11 Oct, 2019 Comments Improve Suggest changes Like Article Like Report 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 // Java program to demonstrate // forEach(BiConsumer) method. import java.util.HashMap; import java.util.Map; import java.util.function.BiConsumer; public class GFG { // Main method public static void main(String[] args) { // create a HashMap and add some values Map<String, Integer> map = new HashMap<>(); 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 = new MyBiConsumer(); // calling forEach method map.forEach(action); } } // Defining Our Action in MyBiConsumer class class MyBiConsumer implements BiConsumer<String, Integer> { public void accept(String k, Integer v) { System.out.print("Key = " + k); System.out.print("\t Value = " + v); System.out.println(); } } Output: 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 // Java program to demonstrate // forEach(BiConsumer) method. import java.util.HashMap; import java.util.Map; import java.util.function.BiConsumer; public class GFG { // Main method public static void main(String[] args) { // Create a HashMap // and add some values Map<String, Integer> map = new HashMap<>(); 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 = new MyBiConsumer(); // calling forEach method map.forEach(action); } } // Defining Our Action in MyBiConsumer class class MyBiConsumer implements BiConsumer<String, Integer> { public void accept(String k, Integer v) { System.out.println("Key: " + k + "\tValue: " + v); if ("for".equals(k)) { System.out.println("Its the " + "highest value\n"); } } } Output: 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 Reference: https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#forEach-java.util.function.BiConsumer- Comment More infoAdvertise with us Next Article HashMap forEach(BiConsumer) method in Java with Examples I ia05030112 Follow Improve Article Tags : Java Java - util package Java-Functions Java-HashMap Practice Tags : Java Similar Reads HashTable forEach() method in Java with Examples The forEach(BiConsumer) method of Hashtable class perform the BiConsumer operation on each entry of hashtable until all entries have been processed or the action throws an exception. The BiConsumer operation is a function operation of key-value pair of hashtable performed in the order of iteration. 2 min read Properties forEach(BiConsumer) method in Java with Examples The forEach(BiConsumer) method of Properties class perform the BiConsumer operation on each entry of hashtable until all entries have been processed or the action throws an exception. The BiConsumer operation is a function operation of key-value pair of hashtable performed in the order of iteration. 2 min read HashTable compute() method in Java with Examples The compute(Key, BiFunction) method of Hashtable class allows to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping is found). If the remapping function passed in compute() of Hashtable returns null as a return value then the mapping is remov 3 min read HashMap replaceAll(BiFunction) method in Java with Examples The replaceAll(BiFunction) method of HashMap class replaces each value with the result of applying the given function(performs a certain operation) on the corresponding value. This process continues in the same manner until all the entries have been processed or until an exception is thrown by the f 3 min read Stream forEach() method in Java with examples Stream forEach(Consumer action) performs an action for each element of the stream. Stream forEach(Consumer action) is a terminal operation i.e, it may traverse the stream to produce a result or a side-effect. Syntax : void forEach(Consumer<? super T> action) Where, Consumer is a functional int 2 min read Like