A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/hashmap-replaceallbifunction-method-in-java-with-examples/ below:

HashMap replaceAll(BiFunction) method in Java with Examples

HashMap replaceAll(BiFunction) method in Java with Examples

Last Updated : 12 Jul, 2025

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 function. It rethrows exceptions thrown by the replacement function.

Syntax:
default void replaceAll(BiFunction<K, V> function)
Parameter: Return value:

replaces calculated values in place and the method returns nothing

Exceptions:

Below programs illustrate the merge(Key, Value, BiFunctional) method:

Program 1: Java
// Java program to demonstrate
// replaceAll(BiFunction) method.

import java.util.*;

public class GFG {

    // Main method
    public static void main(String[] args)
    {

        // create a HashMap having some entries
        HashMap<String, Integer>
            map1 = new HashMap<>();
        map1.put("key1", 1);
        map1.put("key2", 2);
        map1.put("key3", 3);
        map1.put("key4", 4);

        // print map details
        System.out.println("HashMap1: "
                           + map1.toString());

        // replace oldValue with square of oldValue
        // using replaceAll method
        map1.replaceAll((key, oldValue)
                            -> oldValue * oldValue);

        // print new mapping
        System.out.println("New HashMap: "
                           + map1);
    }
}
Output:
HashMap1: {key1=1, key2=2, key3=3, key4=4}
New HashMap: {key1=1, key2=4, key3=9, key4=16}
Program 2: Java
// Java program to demonstrate
// replaceAll(BiFunction) method.

import java.util.*;

public class GFG {

    // Main method
    public static void main(String[] args)
    {

        // create a HashMap having
        // record of the year of birth
        HashMap<String, Integer>
            map1 = new HashMap<>();
        map1.put("Tushar", 2000);
        map1.put("Anushka", 2001);
        map1.put("Sanskriti", 2003);
        map1.put("Anuj", 2002);

        // print map details
        System.out.println("HashMap1: "
                           + map1.toString());

        // replace yearOfBirth with current age
        // using replaceAll method
        map1.replaceAll((key, yearOfBirth)
                            -> 2019 - yearOfBirth);

        // print new mapping
        System.out.println("New HashMap: "
                           + map1);
    }
}
Output:
HashMap1: {Sanskriti=2003, Anushka=2001, Tushar=2000, Anuj=2002}
New HashMap: {Sanskriti=16, Anushka=18, Tushar=19, Anuj=17}
Reference: https://docs.oracle.com/javase/10/docs/api/java/util/Map.html#replaceAll(java.util.function.BiFunction)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4