EnumMap merge(K, V, BiFunction) in Java

merge(K, V, BiFunction): This method is available in java.util.EnumMap class of Java.

Syntax:

V java.util.Map.merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction)

If the specified key is not already associated with a value or is associated with null, associate it with the given non-null value.

Parameters: Three parameters are required for this method.

key: key with which the resulting value is to be associated.

value: the non-null value to be merged with the existing value associated with the key or if no existing value or a null value is associated with the key, to be associated with the key.

remappingFunction: the remapping function to recompute a value if present.

Returns: the new value associated with the specified key, or null if no value is associated with the key.

Throws:

1. UnsupportedOperationException - if the put operation is not supported by this map.

2. ClassCastException - if the class of the specified key or value prevents it from being stored in this map.

3. IllegalArgumentException - if some property of the specified key or value prevents it from being stored in this map.

4. NullPointerException - if the specified key is null and this map does not support null keys or the value or remappingFunction is null

Approach 1: When no exception

Java

import java.util.EnumMap;
import java.util.function.BiFunction;

public class EnumMapmerge {

    public enum Colour {
        RED, GREEN, YELLOW, ORANGE
    };

    public static void main(String[] args) {

        EnumMap<Colour, String> enumMap =
new EnumMap<Colour, String>(Colour.class);

        enumMap.put(Colour.RED, "Red");
        enumMap.put(Colour.GREEN, "Green");

        enumMap.put(Colour.YELLOW, "Yellow");
        enumMap.put(Colour.ORANGE, "Orange");
        BiFunction<? super String, ? super String,
? extends String> add = (a, b) -> a + b;

        System.out.println(enumMap.merge(Colour.RED,
" Red2", add));
    }
}

Output:

Red Red2


Approach 2: NullPointerException 

Java

import java.util.EnumMap;
import java.util.function.BiFunction;

public class EnumMapmerge {

    public enum Colour {
        RED, GREEN, YELLOW, ORANGE
    };

    public static void main(String[] args) {

        EnumMap<Colour, String> enumMap =
new EnumMap<Colour, String>(Colour.class);

        enumMap.put(Colour.RED, "Red");
        enumMap.put(Colour.GREEN, "Green");

        enumMap.put(Colour.YELLOW, "Yellow");
        enumMap.put(Colour.ORANGE, "Orange");
        BiFunction<? super String, ? super String,
? extends String> add = (a, b) -> a + b;

        System.out.println(enumMap.merge(null,
" Red2", add));
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Object.getClass()" because "key" is null at java.base/java.util.EnumMap.typeCheck(EnumMap.java:743) at java.base/java.util.EnumMap.put(EnumMap.java:264) at java.base/java.util.EnumMap.put(EnumMap.java:78) at java.base/java.util.Map.merge(Map.java:1273)


No comments:

Post a Comment