TreeMap replaceAll(BiFunction) in Java

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

Syntax:

void java.util.TreeMap.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.TreeMap;
import java.util.function.BiFunction;

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

        TreeMap<Integer, String> treeMap =
new TreeMap<Integer, String>();

        treeMap.put(2, "Hello");
        treeMap.put(11, "Java");
        treeMap.put(23, "Program");
        treeMap.put(6, "C++");
        treeMap.put(25, "Program");

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

        treeMap.replaceAll(function);

        System.out.println(treeMap);

    }
}

Output:

{2=2Hello, 6=6C++, 11=11Java, 23=23Program, 25=25Program}


No comments:

Post a Comment