使用Java的流(Stream) API计算嵌套元素的数量。

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

Count number of nested elements using stream api java

问题

统计使用流(Stream)的内容数量

class Subject {
  private String id;
  private String name;
  private List<Unit> units;
}

class Unit {
  private String id;
  private String name;
  private List<Topic> topics;
}

class Topic {
  private String id;
  private String name;
  private List<Content> contents;
}

class Content {
  private String id;
  private String contentType;
  private SubTopic subtopic;
}

使用Java 8和Streams我想要统计contentType为video的Content元素的数量

要统计topic数量我尝试了以下代码

```java
int topicCount = subject.getUnits().stream()
    .map(Unit::getTopics)
    .filter(topics -> topics != null)
    .mapToInt(List::size)
    .sum();
英文:

Count number of content using stream

class Subject {
  private String id;
  private String name;
  private List&lt;Unit&gt; units;
}

class Unit {
  private String id;
  private String name;
  private List&lt;Topic&gt; topics;
}

class Topic {
  private String id;
  private String name;
  private List&lt;Content&gt; contents;
}

class Content {
  private String id;
  private String contentType;
  private SubTopic subtopic;
}

With Java 8 and Streams I want the count of the Content elements which is of contentType equal to the video.

To count topic I tried this:

int topicCount = subject.getUnits().stream()
    .map(Unit::getTopics)
    .filter(topics -&gt; topics != null)
    .mapToInt(List::size)
    .sum();

答案1

得分: 4

你可以对嵌套的元素进行扁平映射并进行计数:

long videoContentCount = 
    subject.getUnits()
           .stream()
           .flatMap(u -> u.getTopics().stream())
           .flatMap(t -> t.getContents().stream())
           .filter(c -> c.getCountetType().equals("video"))
           .count();
英文:

You could flat map the nested elements and count them:

long videoContentCount = 
    subject.getUnits()
           .stream()
           .flatMap(u -&gt; u.getTopics().stream())
           .flatMap(t -&gt; t.getContents().stream())
           .filter(c -&gt; c.getCountetType().equals(&quot;video&quot;))
           .count();

答案2

得分: 2

您可以按如下方式使用流:

subject.getUnits()
        .stream()
        .map(Unit::getTopics)
        .flatMap(List::stream)
        .map(Topic::getContents)
        .flatMap(List::stream)
        .map(Content::getContentType)
        .filter("video"::equals)
        .count();

您也可以避免使用 map,像这样:

subject.getUnits()
        .stream()
        .flatMap(e->e.getTopics().stream())
        .flatMap(e->e.getContents().stream())
        .map(Content::getContentType)
        .filter("video"::equals)
        .count();
英文:

You use streams as below,

subject.getUnits()
        .stream()
        .map(Unit::getTopics)
        .flatMap(List::stream)
        .map(Topic::getContents)
        .flatMap(List::stream)
        .map(Content::getContentType)
        .filter(&quot;video&quot;::equals)
        .count();

You can avoid map,

subject.getUnits()
        .stream()
        .flatMap(e-&gt;e.getTopics().stream())
        .flatMap(e-&gt;e.getContents().stream())
        .map(Content::getContentType)
        .filter(&quot;video&quot;::equals)
        .count();

huangapple
  • 本文由 发表于 2020年4月9日 19:54:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/61120568.html
匿名

发表评论

匿名网友

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

确定