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.
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
Key = geeks Value = 22 Key = for Value = 100 Key = is Value = 11 Key = heaven Value = 90 Key = geekies like us Value = 96
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
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- HashMap replace(key, value) method in Java with Examples
- HashMap computeIfAbsent() method in Java with Examples
- HashMap putIfAbsent(key, value) method in Java with Examples
- HashMap compute() method in Java with Examples
- HashMap computeIfPresent(key, BiFunction) method in Java with Examples
- HashMap replaceAll(BiFunction) method in Java with Examples
- HashMap merge(key, value, BiFunction) method in Java with Examples
- HashMap getOrDefault(key, defaultValue) method in Java with Examples
- HashMap replace(key, oldValue, newValue) method in Java with Examples
- HashMap in Java with Examples
- Hashmap methods in Java with Examples | Set 2 (keySet(), values(), containsKey()..)
- HashMap Class Methods in Java with Examples | Set 1 (put(), get(), isEmpty() and size())
- HashMap put() Method in Java
- HashMap get() Method in Java
- HashMap entrySet() Method in Java
- HashMap values() Method in Java
- HashMap remove() Method in Java
- HashMap putAll() Method in Java
- HashMap clear() Method in Java
- HashMap containsKey() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.