C
C
Che_Bu_Rashka2018-02-22 17:11:34
Java
Che_Bu_Rashka, 2018-02-22 17:11:34

How to merge 2 TreeMap using Stream API (Java 8)?

Hello. Question about Stream API
there are 2 TreeMap

TreeMap<String, String> bar = ...
 TreeMap<String, String> foo = ...

How to combine them?
At what everyone has the same keys, but the values ​​are different. TreeMap after all can store duplicates. After that, I'm going to group by using groupBy( ) from the Stream API.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2018-02-22
@Che_Bu_Rashka

And, in fact, why are streams here? Map::putAll did not please?
Of course not. Mar is a container of key-value pairs. The key to that and the key is that it is unique.

D
Dmitry Eremin, 2018-02-22
@EreminD

Map<String, String> myMap = Stream.of(bar, foo)
                                   .map(Map::entrySet)
                                   .flatMap(Collection::stream)
                                   //тут дальше группировка

Tuta

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question