LinkedHashMap replaceAll(BiFunction) in Java

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

Syntax:

void java.util.LinkedHashMap.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.

Exceptions: NA

Approach

Java

import java.util.LinkedHashMap;
import java.util.function.BiFunction;

public class LinkedHashMapreplaceAll {

    public static void main(String args[]) {

        LinkedHashMap<String, Integer> linkedHashMap =
new LinkedHashMap<String, Integer>();

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

        BiFunction<? super String, ? super Integer,
? extends Integer> function = (a, b) ->
(Integer.parseInt(a) + b);
        linkedHashMap.replaceAll(function);

        System.out.println(linkedHashMap);

    }

}

Output:

{1=2, 2=4}


No comments:

Post a Comment