Reputation: 952
Java8 and Lambdas - that's my play for now. And another problem/question. I got grouping made with lamda that look like this:
Map<Question, List<Answer>> temp = foo.stream().flatMap(x -> x.getValue().stream()).flatMap(
x -> x.getAnswers().stream()).collect(
Collectors.groupingBy(
zz -> zz.getQuestion(),
Collectors.mapping(z -> z, Collectors.toList())
)
);
I got myself here from a list of Foo, a map of Question with aggregated List of Answers that users made.
QUESTION
is it possible to add condition while grouping?
In this example, my Question.class has a Double field called Weight, and some Questions got this field null or with a value of 0.0.
I don't need them in my aggregated Map, so I was wondering can I add condition here, or do I need to iterate through resulting Map?
EDIT
foo is a list of Result.class, x.getValue() returns a List of AnswerSet.class, and x.getAnswers() return a list of Answer.class. Answer.class has a Question.class as a field
Upvotes: 1
Views: 2811
Reputation: 8128
foo.stream()
.filter(q -> q.weight != null && q.weight != 0.0)
.<continue what you were doing>
Upvotes: 2