I think flatMap() is what you're looking for.
For example:
List<AClass> allTheObjects = map.values()
.stream()
.flatMap(listContainer -> listContainer.lst.stream())
.collect(Collectors.toList());
Answer from Puce on Stack OverflowDavidvlijmincx
davidvlijmincx.com › home › java › java stream join lists
Java Stream join lists
March 10, 2022 - The easiest way to join two lists with Streams is by using the Stream.concat(Stream a, Stream b) method. The method takes two Stream instances as parameters and puts the second Stream behind the first Stream.
Top answer 1 of 5
219
I think flatMap() is what you're looking for.
For example:
List<AClass> allTheObjects = map.values()
.stream()
.flatMap(listContainer -> listContainer.lst.stream())
.collect(Collectors.toList());
2 of 5
80
Alternative: Stream.concat()
Stream.concat(map.values().stream(), listContainer.lst.stream())
.collect(Collectors.toList()
Techie Delight
techiedelight.com › home › java › join two lists in java
Join two lists in Java | Techie Delight
July 7, 2026 - This method is similar to List.addAll() but likely to run significantly faster. We can also use Java 8 Stream to join two lists. ⮚ Using Stream.of() with flatMap() + Collector · Here we’re obtaining a stream consisting of elements from both the list using the static factory method Stream.of() and accumulating all elements into a new list using a Collector.
Benchresources
benchresources.net › home › java › java 8 – how to merge/concatenate/join two lists into single list ?
Java 8 - How to merge/concatenate/join two lists into single list ? - BenchResources.Net
July 19, 2022 - Previously we have seen an example to join or concatenate two or more Arrays into single Arrays ... package in.bench.resources.java8.merge.list; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class MergeListUsingStreamConcat { public static void main(String[] args) { // list 1 - fruits List<String> fruits = Arrays.asList( "Apple", "Mango", "Grape", "Cherry", "Melon" ); System.out.println("Fruit List :- \n" + fruits); // list 2 - vegetables List<String> veggies = Arrays.asList( "Carrot", "Radish", "Lettuce", "Bean", "Gourd" ); System.out.println("\nVeggies List :- \n" + veggies); // Merging 2 lists List<String> mergedList = Stream .concat(fruits.stream(), veggies.stream()) .collect(Collectors.toList()); // print merged list to the console System.out.println("\nMerged List :- \n" + mergedList); } }
How to do in Java
howtodoinjava.com › home › collections framework › java arraylist › how to merge two arraylists in java
How to Merge Two ArrayLists in Java
January 19, 2023 - ArrayList<String> listOne = new ArrayList<>(Arrays.asList("a", "b", "c")); ArrayList<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); Program output. ... In these examples, we combined the lists, but in the final list, we had duplicate elements. This may not be the desired output in many cases. Next, we will learn to merge the lists, excluding duplicates. To get a merged list minus duplicate elements, we have two approaches: The Java Sets allow only unique elements.
Coderwall
coderwall.com › p › im4lja › joining-objects-into-a-string-with-java-8-stream-api
Joining Objects into a String with Java 8 Stream API (Example)
July 17, 2023 - #join · #stream · You can leverage Java 8 Collectors to concatenate some objects into a string separated by a delimiter: For example: List<Integer> numbers = Arrays.asList( 4, 8, 15, 16, 23, 42 ); return numbers.stream() .map( n -> n.toString() ) .collect( Collectors.joining( "," ) ); will return "4,8,15,16,23,42".
Top answer 1 of 16
1155
In Java 8:
List<String> newList = Stream.concat(listOne.stream(), listTwo.stream())
.collect(Collectors.toList());
Java 16+:
List<String> newList = Stream.concat(listOne.stream(), listTwo.stream()).toList();
2 of 16
702
Off the top of my head, I can shorten it by one line:
List<String> newList = new ArrayList<String>(listOne);
newList.addAll(listTwo);
Educative
educative.io › answers › how-to-merge-two-lists-in-java
How to merge two lists in Java
In this shot, we have seen how to merge two lists in Java by using the addAll() method and Java Stream.
Scaler
scaler.com › home › topics › java program to merge two lists
Java Program to Merge Two Lists | Scaler Topics
June 9, 2024 - stream class in java can be used for merging two lists, it accepts two streams as arguments and joins them together to form a new stream.
verytools
verytools.net › xtools-guide › en › posts › collections-utils
How to join two lists by property in java
October 23, 2023 - One to one join is easy, one two many join is no much harder, let's implement it. // if the relationship between user and order is one to many, we can map user id to orders using java stream Map<Integer, List<Order>> userId2OrdersMap = orders.stream().collect(Collectors.groupingBy(Order::getUserId)); for (User user : users) { int userId = user.getId(); // for each user, we can get the related orders by user id List<Order> orders = userId2OrdersMap.get(userId); }