Count number of content using 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;
}
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 -> topics != null)
.mapToInt(List::size)
.sum();
Listbeingnull? when you wrote.filter(topics -> topics != null)? I mean I can read it, but you should avoid that. (assigningnullto aList)