简体   繁体   中英

java8 lambda grouping with condition

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

foo.stream()
   .filter(q -> q.weight != null && q.weight != 0.0)
   .<continue what you were doing>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM