Here we are Combine multiple lists in Java 8 using Stream Api and flatMap.
- Create the three Integer type list data.
- Now create a stream with all lists using Stream.of() method.
- Using flatMap() to transform each element of all streams into a single stream.
- From collect() method, we collect all the list of integer data.
- Print the whole list of data into single list.
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class CombineList {
public static void main(String[] args) {
List<Integer> list1 = Arrays.asList(12, 13, 14, 15);
List<Integer> list2 = Arrays.asList(23, 45, 67, 89);
List<Integer> list3 = Arrays.asList(1, 2, 4, 5);
List<Integer> ListData = Stream.of(list1, list2,
list3).flatMap(a -> a.stream()).collect(Collectors.toList());
System.out.println("All list of data :- " + ListData);
}
}
Output :-
All list of data :- [12, 13, 14, 15, 23, 45, 67, 89, 1, 2, 4, 5]