How to get a List from a HashMap>

后端 未结 7 657
没有蜡笔的小新
没有蜡笔的小新 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:32

    You can use Collection.stream with flatMap as:

    Map> map = new HashMap<>(); // program to interface
    List list = map.values()
                      .stream()
                      .flatMap(Collection::stream)
                      .collect(Collectors.toList());
    

    or use a non-stream version as:

    List list = new ArrayList<>();
    map.values().forEach(list::addAll)
    

提交回复
热议问题