Java8 Retrieving multiple fields from a class object using streams

I have a List of class A objects with several fields, including number1 and number2 , among others.

I want to extract all unique values โ€‹โ€‹of number1 and number2 from List<A> via java 8 Stream s.

The map function helps me get only one field, as shown below:

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

And after this code is executed, there is no way to extract number2 . How can i do this?

+5
source share
1 answer

You can extract both using flatMap :

 list.stream().flatMap(a -> Stream.of(a.getNumber1(),a.getNumber2())).distinct()... 
+9
source

Source: https://habr.com/ru/post/1274919/


All Articles