How to get a List from a HashMap>

后端 未结 7 646
没有蜡笔的小新
没有蜡笔的小新 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<String> 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<String> 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.

    0 讨论(0)
提交回复
热议问题