Grzenio :
I have a stream<A>
, where
class A {
String category();
// ...
}
I would like to get a map<String, list<A>>
, where the original stream is partitioned into sublists based on the value of category(). It is pretty trivial to have it implemented using a for loop, but is it possible to get a more elegant solution harnessing java streams?
EXAMPLE:
Given {[a, xyz], [a, zyx], [b, abc]}
, I would like to get a map:
a -> {[a, xyz], [a, zyx]}
b -> {[b, abc]}
Jure Kolenko :
Use the groupingBy collector.
stream.collect(Collectors.groupingBy(A::category));
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=464897&siteId=1