public class StreamsInMaps extends Object
| Constructor and Description |
|---|
StreamsInMaps() |
| Modifier and Type | Method and Description |
|---|---|
static <K,V1,V2> Collector<Map.Entry<K,V1>,?,Map<K,V2>> |
mapWithValues(Function<Map.Entry<K,V1>,V2> valueMapper)
Function to be used when streaming maps just to convert the value of the entries.
|
public static <K,V1,V2> Collector<Map.Entry<K,V1>,?,Map<K,V2>> mapWithValues(Function<Map.Entry<K,V1>,V2> valueMapper)
Example: "I would like to have a new map with the age of every person, but preserving the same keys". Lets suppose the key is
a String, the conversion would be: Map<String, Person> --> Map<String, Integer>.
The code would look something like this (let 'persons' be the map):
Map<String, Integer> ages = persons.entrySet().stream().collect(mapWithValues(e -> e.getValue().age()));
Copyright © 2014–2023 MuleSoft, Inc.. All rights reserved.