Java8 Extract multiple fields from a class object using streams

Pankaj Singhal :

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?

Eran :

You can extract both by using flatMap:

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=460133&siteId=1