MaporiginalMap = ImmutableMap.of("one", 1, "two", 2, "three", 3); Function incrementByOne = n -> n + 1; Map transformedMap = Maps.transformValues(originalMap, incrementByOne); // transformedMap contains {"one": 2, "two": 3, "three": 4}
MapIn this example, we create a map with some key-value pairs and then define a method reference that converts a string to uppercase. We then use transformValues to apply this function to each value in the original map and create a new map with the transformed values. Both of these examples use the ImmutableMap class from the com.google.common.collect package, which provides an immutable implementation of the Map interface. This package library is commonly used in Java projects for working with collections and providing utility functions.originalMap = ImmutableMap.of("a", "apple", "b", "banana", "c", "cherry"); Function toUpper = String::toUpperCase; Map transformedMap = Maps.transformValues(originalMap, toUpper); // transformedMap contains {"a": "APPLE", "b": "BANANA", "c": "CHERRY"}