Separating java threads in categories

I have stream<A> where

 class A { String category(); // ... } 

I would like to get map<String, list<A>> , where the original stream is broken into signatures based on the value of category (). This is pretty trivial if implemented using a for loop, but is it possible to get a more elegant solution using java threads?

Example:

Given {[a, xyz], [a, zyx], [b, abc]} , I would like to get a map:

 a -> {[a, xyz], [a, zyx]} b -> {[b, abc]} 
+5
source share
1 answer

Use groupingBy collector .

stream.collect(Collectors.groupingBy(A::category));

+11
source

Source: https://habr.com/ru/post/1267129/


All Articles