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. 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<? super K, ? super V> action)
Parameters: This method takes a parameter name BiConsumer which represents the action to be performed for each element.
Returns: This method returns nothing.
Exception: This method throws:
- NullPointerException: if the specified action is null.
Below programs illustrate the forEach(BiConsumer) method:
Program 1:
// Java program to demonstrate // forEach(BiConsumer) method. import java.util.*; public class GFG { // Main method public static void main(String[] args) { // create a table and add some values Map<String, Integer> table = new Hashtable<>(); table.put("Pen", 10); table.put("Book", 500); table.put("Clothes", 400); table.put("Mobile", 5000); table.put("Booklet", 2500); // add 100 in each value using forEach() table.forEach((k, v) -> { v = v + 100; table.replace(k, v); }); // print new mapping using forEcah() table.forEach( (k, v) -> System.out.println("Key : " + k + ", Value : " + v)); } } |
Key : Booklet, Value : 2600 Key : Clothes, Value : 500 Key : Mobile, Value : 5100 Key : Pen, Value : 110 Key : Book, Value : 600
Program 2: To Show NullPointerException
// Java program to demonstrate // forEach(BiConsumer) method. import java.util.*; public class GFG { // Main method public static void main(String[] args) { // create a table and add some values Map<Integer, String> table = new Hashtable<>(); table.put(1, "100RS"); table.put(2, "500RS"); table.put(3, "1000RS"); try { // add 100 in each value using forEach() table.forEach((k, v) -> { v = v + 100; table.put(null, v); }); } catch (Exception e) { System.out.println("Exception: " + e); } } } |
Exception: java.lang.NullPointerException
References: https://docs.oracle.com/javase/8/docs/api/java/util/Hashtable.html#forEach-java.util.function.BiConsumer-
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- foreach() loop vs Stream foreach() vs Parallel Stream foreach()
- HashTable compute() method in Java with Examples
- Hashtable computeIfAbsent() method in Java with Examples
- HashTable putIfAbsent() method in Java with Examples
- Hashtable keySet() Method in Java with Examples
- Stream forEach() method in Java with examples
- Iterable forEach() method in Java with Examples
- LinkedTransferQueue forEach() method in Java with Examples
- LinkedBlockingDeque forEach() method in Java with Examples
- CopyOnWriteArraySet forEach() method in Java with Examples
- CopyOnWriteArrayList forEach() method in Java with Examples
- Properties forEach(BiConsumer) method in Java with Examples
- HashMap forEach(BiConsumer) method in Java with Examples
- Hashtable toString() Method in Java
- Hashtable elements() Method in Java
- Hashtable keys() Method in Java
- Hashtable containsValue() Method in Java
- Hashtable containsKey() Method in Java
- Hashtable put() Method in Java
- Hashtable get() 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 contribute@geeksforgeeks.org. 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.
Improved By : nidhi_biet

