replaceAll(BiFunction): This method is available in java.util.EnumMap class of Java.
Syntax:
void java.util.Map.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.
Throws:
1. UnsupportedOperationException - if the set operation is not supported by this map's entry set iterator.
2. ClassCastException - if the class of a replacement value prevents it from being stored in this map
3. NullPointerException -
1. If the specified function is null, or the specified replacement value is null, and this map does not permit null values.
2. If function or a replacement value is null,and this map does not permit null keys or values
4. ClassCastException - if a replacement value is of an inappropriate type for this map.
5. IllegalArgumentException - if some property of a replacement value prevents it from being stored in this map.
6. ConcurrentModificationException - if an entry is found to be removed during iteration.
Approach
Java
import java.util.EnumMap;import java.util.function.BiFunction;public class EnumMapreplaceAll {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 Colour, ? super String,? extends String> add = (a, b) -> a + b;enumMap.replaceAll(add);System.out.println(enumMap);}}
Output:
{RED=REDRed, GREEN=GREENGreen, YELLOW=YELLOWYellow, ORANGE=ORANGEOrange}
No comments:
Post a Comment