Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stream groupingBy: reducing to first element of list [duplicate]

I have a List<Valuta> which can be represented (simplified) JSON-style:

[ { codice=EUR, description=Euro, ratio=1 }, { codice=USD, description=Dollars, ratio=1.1 } ]

I want to transform that in a Map<String, Valuta> like this:

{ EUR={ codice=EUR, description=Euro, ratio=1 }, USD={ codice=USD, description=Dollars, ratio=1.1 }}

I wrote this one-liner:

getValute().stream().collect(Collectors.groupingBy(Valuta::getCodice)); 

but this returns a Map<String, List<Valuta>> instead of what I need.

I suppose mapping() function would work for me, but don't know how.

like image 573
Fabio B. Avatar asked Dec 28 '15 15:12

Fabio B.


People also ask

How does Collector groupingBy work?

The groupingBy() method of Collectors class in Java are used for grouping objects by some property and storing results in a Map instance. In order to use it, we always need to specify a property by which the grouping would be performed. This method provides similar functionality to SQL's GROUP BY clause.

How do I skip the first element in a stream?

Stream skip(n) method is used to skip the first 'n' elements from the given Stream. The skip() method returns a new Stream consisting of the remaining elements of the original Stream, after the specified n elements have been discarded in the encounter order.

What is flatMap in Java stream?

Stream flatMap(Function mapper) returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. Stream flatMap(Function mapper) is an intermediate operation. These operations are always lazy.


2 Answers

Actually, you need to use Collectors.toMap here instead of Collectors.groupingBy:

Map<String, Valuta> map =      getValute().stream()                .collect(Collectors.toMap(Valuta::getCodice, Function.identity())); 

groupingBy is used to group elements of a Stream based on a grouping function. 2 Stream elements that will have the same result with the grouping function will be collected into a List by default.

toMap will collect the elements into a Map where the key is the result of applying a given key mapper and the value is the result of applying a value mapper. Note that toMap, by default, will throw an exception if a duplicate is encountered.

like image 198
Tunaki Avatar answered Oct 14 '22 16:10

Tunaki


It's a bit late in the game, but try this:

Map<String, Valuta> map =      getValute().stream()                .collect(Collectors.groupingBy(Valuta::getCodice,                             Collectors.collectingAndThen(                                 Collectors.toList(),                                  values -> values.get(0)))); 
like image 21
Philippe Avatar answered Oct 14 '22 15:10

Philippe