Java流 – 结合filter和collect

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

Java Stream - Combine filter and collect

问题

ColumnFamily column = tokens.getColumnFamilies().stream()
    .filter(family -> family.getName().equals("this_family"))
    .findAny()
    .get();

Map<String, String> tokenized = column.getColumns().stream()
    .collect(Collectors.toMap(
        Column::getQualifier,
        Column::getValue
    ));

Is there a way I can combine these two streams into one? I am using the first stream to filter and find on my nested list, and using the second stream to create a map based on the result of the stream. I am wondering if there's a way I could have done this using a single stream.

Something like this

Map<String, String> tokenized = tokens.getColumnFamilies().stream()
    .filter(family -> family.getName().equals("this_family"))
    .flatMap(family -> family.getColumns().stream())
    .collect(Collectors.toMap(
        Column::getQualifier,
        Column::getValue
    ));
英文:
ColumnFamily column = tokens.getColumnFamilies().stream()
    .filter(family -&gt; family.getName().equals(&quot;this_family&quot;))
    .findAny()
    .get();

Map&lt;String, String&gt; tokenized = column.getColumns().stream()
    .collect(Collectors.toMap(
        Column::getQualifier,
        Column::getValue
    ));

Is there a way I can combine these two streams into one? I am using the first stream to filter and find on my nested list, and using the second stream to create a map based on the result of the stream. I am wondering if there's a way I could have done this using a single stream.

Something like this

Map&lt;String, String&gt; tokenized = tokens.getColumnFamilies().stream()
    .filter(family -&gt; family.getName().equals(&quot;this_family&quot;))
    .collect(Collectors.toMap(
        //
    ));

答案1

得分: 2

你可以使用 flatMap 来获取嵌套的 Stream 并展平结构:

Map<String, String> tokenized = tokens.getColumnFamilies().stream()
    .filter(family -> family.getName().equals("this_family"))
    .limit(1)  // 相当于 findAny
    .flatMap(cf -> cf.getColumns().stream())
    .collect(Collectors.toMap(
        Column::getQualifier,
        Column::getValue
    ));
英文:

You can use flatMap to get nested Stream and flatten the structure:

Map&lt;String, String&gt; tokenized = tokens.getColumnFamilies().stream()
    .filter(family -&gt; family.getName().equals(&quot;this_family&quot;))
    .limit(1)  // equivalent of findAny
    .flatMap(cf -&gt; cf.getColumns().stream())
    .collect(Collectors.toMap(
        Column::getQualifier,
        Column::getValue
    ));

答案2

得分: 0

使用 Optional#map() 并结合:

Map<String, String> tokenized = tokens.getColumnFamilies().stream()
    .filter(family -> family.getName().equals("this_family"))
    .findAny()
    .map(cf -> cf.getColumns().stream()).get()
    .collect(Collectors.toMap(Column::getQualifier, Column::getValue));
英文:

Use Optional#map() and combine

Map&lt;String, String&gt; tokenized = tokens.getColumnFamilies().stream()
                    .filter(family -&gt; family.getName().equals(&quot;this_family&quot;))
                    .findAny()
                    .map(cf -&gt; cf.getColumns().stream()).get()
                    .collect(Collectors.toMap(Column::getQualifier, Column::getValue));

huangapple
  • 本文由 发表于 2020年8月27日 11:13:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63608614.html
匿名

发表评论

匿名网友

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

确定