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 Stream
s.
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?
You can extract both by using flatMap
:
list.stream().flatMap(a -> Stream.of(a.getNumber1(),a.getNumber2())).distinct()...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With