使用流从List<HashMap>中收集键

huangapple go评论70阅读模式
英文:

Collect keys from List<HashMap> using streams

问题

我有一个名为 dataBeanListList<ExcelData>ExcelData 类拥有变量 HashMap<String, Integer> cardNumber

我想从 List<ExcelData> 中获取所有的键(keys)。我尝试了下面的方法,但是我得到了 List<List<String>> 类型的值。然而,我希望得到 List<String> 类型的值。你能帮我解决这个问题吗?

List<List<String>> collect = dataBeanList
                .stream()
                .map(excelData ->
                        excelData.getCardNumber().keySet()
                                .stream()
                                .collect(Collectors.toList()))
                .collect(Collectors.toList());
英文:

I have a List&lt;ExcelData&gt; called dataBeanList. ExcelData class has variable HashMap&lt;String, Integer&gt; cardNumber.

I want to get all keys from List&lt;ExcelData&gt;. I tried below approach but, I obtained List&lt;List&lt;String&gt;&gt; values. However I want to get List&lt;String&gt; values. Can you help me with that?

List&lt;List&lt;String&gt;&gt; collect = dataBeanList
                .stream()
                .map(excelData -&gt;
                        excelData.getCardNumber().keySet()
                                .stream()
                                .collect(Collectors.toList()))
                .collect(Collectors.toList());

答案1

得分: 3

基于 @ernest_k 在评论中提供的答案(他还谈到如果键重复并且您只需要不同的键,则可以将其转换为集合):

List<String> collect = dataBeanList
                .stream()
                .map(excelData ->
                        excelData.getCardNumber().keySet()
                                .stream()
                                .collect(Collectors.toList()))
                .flatMap(List::stream)
                .collect(Collectors.toList());
英文:

Building on what @ernest_k provided as answer in comment (he also talked about using converting it to a set if the keys are duplicating and you need only distinct ones) :

List&lt;String&gt; collect = dataBeanList
                .stream()
                .map(excelData -&gt;
                        excelData.getCardNumber().keySet()
                                .stream()
                                .collect(Collectors.toList()))
                .flatMap(List::stream)
                .collect(Collectors.toList());

答案2

得分: 3

使用flatMap,每当你需要从一个流创建另一种类型的流时,就可以使用它。
根据文档说明:

返回一个流,该流由将此流的每个元素替换为映射流的内容所组成,映射流是通过将提供的映射函数应用于每个元素而生成的。

将你的代码更改为:

dataBeanList.stream()
    .flatMap(excelData -> excelData.getCardNumber().keySet().stream())
    .collect(Collectors.toList());
英文:

Use flatMap whenever you need to create another type of Stream from one Stream.
From documentation -

Returns a stream consisting of the results of replacing 
each element of his stream with the contents of a mapped stream 
produced by applying the provided mapping function to each element

Change your code to -

dataBeanList.stream().
                flatMap(excelData -&gt; excelData.getCardNumber().keySet().stream()).
                collect(Collectors.toList());

huangapple
  • 本文由 发表于 2020年7月27日 16:47:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63111748.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定