WeakHashMap replaceAll(BiFunction) in Java

replaceAll(BiFunction): This method is available in java.util.WeakHashMap class of Java.

Syntax:

void java.util.WeakHashMap.replaceAll(BiFunction<? super K, ? super V, ? extends V> function)

This method takes one argument. This method replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.

Parameters: One parameter is required for this method.

function: the function to apply to each entry.

Returns: NA

Exceptions: NA

Approach

Java

import java.util.WeakHashMap;
import java.util.function.BiFunction;

public class WeakHashMapreplaceAll {
    public static void main(String[] args) {

        WeakHashMap<String, Integer> weakHashMap =
new WeakHashMap<String, Integer>();

        weakHashMap.put("1", 1);
        weakHashMap.put("2", 2);

        BiFunction<? super String, ? super Integer,
? extends Integer> function = (k, v) -> v +
Integer.parseInt(k);
        weakHashMap.replaceAll(function);
        System.out.println(weakHashMap);

    }
}

Output:

{1=2, 2=4}


No comments:

Post a Comment