Merge Map Java 8 Stream

前端 未结 4 719
灰色年华
灰色年华 2020-12-28 17:56

I would like to merge two Map with JAVA 8 Stream:

Map> mapGlobal = new HashMap>();
Map

        
4条回答
  •  不思量自难忘°
    2020-12-28 18:35

    Using StreamEx

    Map> mergedMap =
            EntryStream.of(mapGlobal)
                    .append(EntryStream.of(mapAdded))
                    .toMap((v1, v2) -> {
                        List combined = new ArrayList<>();
                        combined.addAll(v1);
                        combined.addAll(v2);
                        return combined;
                    });
    

    If you have even more maps to merge just append to the stream

                    .append(EntryStream.of(mapAdded2))
                    .append(EntryStream.of(mapAdded3))
    

提交回复
热议问题