How to get a List from a HashMap>

后端 未结 7 670
没有蜡笔的小新
没有蜡笔的小新 2020-12-15 04:28

I want to extract a List from a Map> (E is a random Class) using stream().

I

7条回答
  •  南笙
    南笙 (楼主)
    2020-12-15 04:51

    You can use Collectors2.flatCollect from Eclipse Collections

    List list = 
        map.values().stream().collect(Collectors2.flatCollect(l -> l, ArrayList::new));
    

    You can also adapt the Map and use the Eclipse Collections MutableMap API.

    List list = 
        Maps.adapt(map).asLazy().flatCollect(l -> l).toList();
    

    The method flatCollect is equivalent to the method flatMap, except flatCollect takes an Iterable instead of a Stream.

    Note: I am a committer for Eclipse Collections.

提交回复
热议问题