Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java8: Stream merge two lists [duplicate]

I have two lists

List1 - (1, 2, 3)
List2 - (4, 5, 6,7, 8)

Want to merge both the list as (1, 4, 2, 5, 3, 6, 7, 8) using java streams

First element from List1, List2 and, Second element from list1, list2 ...so on..if any extra elements remain then place at the end.

like image 570
user3125890 Avatar asked Mar 17 '26 07:03

user3125890


1 Answers

Stream.concat(list1.stream(), list2.stream())
   .collect(Collectors.toList())
like image 195
Maxim Popov Avatar answered Mar 19 '26 19:03

Maxim Popov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!