Streams Java8: Merging two lists
public static void mergeTwoLists(){
//oneway
List<String> listOne = new ArrayList<>(Arrays.asList("a", "b", "c"));
List<String> listTwo = new ArrayList<>(Arrays.asList("c", "d", "e"));
List<String> combinedList = Stream.of(listOne, listTwo)
.flatMap(x -> x.stream())
.collect(Collectors.toList());
System.out.println(combinedList);
//second way
List<String> mergeLists =
Stream.concat(listOne.stream(),listTwo.stream()).collect(Collectors.toList());
System.out.println(mergeLists);
}
Comments
Post a Comment