Properties forEach(BiConsumer) method in Java with Examples Last Updated : 23 May, 2019 Comments Improve Suggest changes Like Article Like Report 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. 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 properties 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: Java // Java program to demonstrate // forEach(BiConsumer) method. import java.util.*; public class GFG { // Main method public static void main(String[] args) { // Create a properties and add some values Properties properties = new Properties(); properties.put("Pen", 10); properties.put("Book", 500); properties.put("Clothes", 400); properties.put("Mobile", 5000); // Print Properties details System.out.println("Properties 1: " + properties.toString()); // Add 100 in each value using forEach() properties.forEach((k, v) -> { v = (int)v + 100; properties.replace(k, v); }); // Print new mapping using forEcah() properties.forEach( (k, v) -> System.out.println("Key : " + k + ", Value : " + v)); } } Output: Properties 1: {Book=500, Mobile=5000, Pen=10, Clothes=400} Key : Clothes, Value : 500 Key : Mobile, Value : 5100 Key : Pen, Value : 110 Key : Book, Value : 600 Program 2: To show NullPointerException Java // Java program to demonstrate // forEach(BiConsumer) method. import java.util.*; public class GFG { // Main method public static void main(String[] args) { // Create a properties and add some values Properties properties = new Properties(); properties.put(1, "100RS"); properties.put(2, "500RS"); properties.put(3, "1000RS"); // Print Properties details System.out.println("Current Properties: " + properties.toString()); try { // add 100 in each value using forEach() properties.forEach((k, v) -> { v = (String)v + "100"; properties.put(null, v); }); } catch (Exception e) { System.out.println("Exception: " + e); } } } Output: Current Properties: {3=1000RS, 2=500RS, 1=100RS} Exception: java.lang.NullPointerException References: https://docs.oracle.com/javase/9/docs/api/java/util/Properties.html#forEach-java.util.function.BiConsumer- Comment More infoAdvertise with us Next Article Properties forEach(BiConsumer) method in Java with Examples K Kirti_Mangal Follow Improve Article Tags : Java Java - util package Java-Functions Java-Properties Practice Tags : Java Similar Reads HashMap forEach(BiConsumer) method in Java with Examples 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. 3 min read Properties entrySet() method in Java with Examples The entrySet() method of Properties class is used to get the Set view of this Properties mappings. Syntax: public Set<Map.Entry> entrySet() Parameters: This method do not accepts any parameters. Returns: This method returns a Set view of the mappings of this Properties object. Below programs i 2 min read Properties elements() method in Java with Examples The elements() method of Properties class is used to get the enumeration of this Properties object. It can be further used to retrieve the elements sequentially using this Enumeration received. Syntax: public Enumeration elements() Parameters: This method do not accepts any parameters.Returns: This 2 min read Properties compute(Key, BiFunction) method in Java with Examples The compute(Key, BiFunction) method of Properties 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 Properties returns null as a return value then the mapping is rem 2 min read Properties get(key) method in Java with Examples The get(key) method of Properties class is used to get the value mapped to this key, passed as the parameter, in this Properties object. This method will fetch the corresponding value to this key, if present, and return it. If there is no such mapping, then it returns null. Syntax: public Object get 2 min read Like