Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 stream map on entry set

I'm trying to perform a map operation on each entry in a Map object.

I need to take a prefix off the key and convert the value from one type to another. My code is taking configuration entries from a Map<String, String> and converting to a Map<String, AttributeType> (AttributeType is just a class holding some information. Further explanation is not relevant for this question.)

The best I have been able to come up with using the Java 8 Streams is the following:

private Map<String, AttributeType> mapConfig(Map<String, String> input, String prefix) {    int subLength = prefix.length();    return input.entrySet().stream().flatMap((Map.Entry<String, Object> e) -> {       HashMap<String, AttributeType> r = new HashMap<>();       r.put(e.getKey().substring(subLength), AttributeType.GetByName(e.getValue()));       return r.entrySet().stream();    }).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); } 

Being unable to construct an Map.Entry due to it being an interface causes the creation of the single entry Map instance and the use of flatMap(), which seems ugly.

Is there a better alternative? It seems nicer to do this using a for loop:

private Map<String, AttributeType> mapConfig(Map<String, String> input, String prefix) {    Map<String, AttributeType> result = new HashMap<>();     int subLength = prefix.length();     for(Map.Entry<String, String> entry : input.entrySet()) {       result.put(entry.getKey().substring(subLength), AttributeType.GetByName( entry.getValue()));    }    return result; } 

Should I avoid the Stream API for this? Or is there a nicer way I have missed?

like image 586
Wil Selwood Avatar asked Dec 01 '14 13:12

Wil Selwood


People also ask

How do you convert an entry to a map?

There is no inbuilt API in java for direct conversion between HashSet and HashMap , you need to iterate through set and using Entry fill in map. Returns a Set view of the mappings contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa.

Can we use stream with map in Java?

Yes, you can map each entry to another temporary entry that will hold the key and the parsed integer value. Then you can filter each entry based on their value. Map<String, Integer> output = input.

Can we use map stream in Java 8?

Java 8 Stream's map method is intermediate operation and consumes single element forom input Stream and produces single element to output Stream. It simply used to convert Stream of one type to another. Let's see method signature of Stream's map method.

How do I collect a stream map?

Method 1: Using Collectors.toMap() Function The Collectors. toMap() method takes two parameters as the input: KeyMapper: This function is used for extracting keys of the Map from stream value. ValueMapper: This function used for extracting the values of the map for the given key.


2 Answers

Simply translating the "old for loop way" into streams:

private Map<String, String> mapConfig(Map<String, Integer> input, String prefix) {     int subLength = prefix.length();     return input.entrySet().stream()             .collect(Collectors.toMap(                    entry -> entry.getKey().substring(subLength),                     entry -> AttributeType.GetByName(entry.getValue()))); } 
like image 121
Smutje Avatar answered Sep 28 '22 05:09

Smutje


Please make the following part of the Collectors API:

<K, V> Collector<? super Map.Entry<K, V>, ?, Map<K, V>> toMap() {   return Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue); } 
like image 34
Lóránt Miglécz Avatar answered Sep 28 '22 05:09

Lóránt Miglécz