9

I have a List of class A objects with multiple fields including number1 & number2 apart from various others.

I want to extract all the unique number1 & number2 values from the List<A> via java 8 Streams.

The map function helps me get only 1 field like below:

list.stream().map(A::getNumber1);

And after the above code gets executed, there is no way to extract number2. How can I do this?

1 Answer 1

16

You can extract both by using flatMap:

list.stream().flatMap(a -> Stream.of(a.getNumber1(),a.getNumber2())).distinct()...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.