user2624119 user2624119 -4 years ago 179
Java Question

Elegantly create map with object fields as key/value from object stream in Java 8

I have the following class

class Person {
public String name;
public int age;
public List<String> hobbies;

Person(String name, int age, List<String> hobbies)
{this.name = name; this.age = age; this.hobbies = hobbies;}
}


How do I create a Map of age to hobbies like
Map<Integer, Set<String>>
?

The Java 8 way I cooked up is:

Map<Integer, Set<String>> collect8 = persons.stream()
.collect(
toMap(
p -> p.age,
p -> p.hobbies.stream().collect(toSet()),
(hobbies1, hobbies2) ->
Stream.concat(hobbies1.stream(), hobbies2.stream()).collect(toSet())
)
);


Is there a more idiomatic way of doing this with
Collectors.groupingBy()
perhaps?

As a related question, I find the version without Java streams to be more readable.

Map<Integer, Set<String>> collect7 = new HashMap<>();

for(Person p: persons) {
Set<String> hobbies = collect7.getOrDefault(p.age, new HashSet<>());
hobbies.addAll(p.hobbies);
collect7.put(p.age, hobbies);
}


Should we go with non streams code if it is easier to read; specially when the streamed version, as seen here, has no intermediate streams with transformations of the data but quickly end in a terminal operation?

Answer Source

As you noted yourself: the Stream solution might not be as readable as your current non-Stream-solution. Solving your problem with groupingBy might not look as good as you might expect as you want to transform your List into a Set.

I constructed a solution with groupingBy, mapping and reducing, but that solution is not that easy to read and did even contain an error. You can read more about that in: Java 8 stream.collect( ... groupingBy ( ... mapping( ... reducing ))) reducing BinaryOperator-usage I really suggest to look up the answer Holger gave as it also contains a simpler solution using a custom Collector and a little Outlook to Java 9's flatMapping, which for me comes close to your non-Stream-solution.

But another solution using groupingBy I came up with and that actually works is the following:

Map<Integer, Set<String>> yourmap;
yourmap = personList.stream()
                    .flatMap(p -> p.hobbies.stream()
                                           .flatMap(hobby -> Stream.of(new SimpleEntry<>(p.age, hobby)))
                            )
                    .collect(Collectors.groupingBy(Entry::getKey,
                             Collectors.mapping(Entry::getValue, Collectors.toSet())));

for that you need the following imports:

import java.util.AbstractMap.SimpleEntry;
import java.util.Map.Entry;

Of course you can take also a Tuple or Pair or what you like the most.

But then again not better in any ways.

I would stay with your current non-Stream-solution. It is more readable and does what it should do.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download