Merge lists with stream API
Solution 1
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());
Solution 2
Alternative: Stream.concat()
Stream.concat(map.values().stream(), listContainer.lst.stream())
.collect(Collectors.toList()
Solution 3
Already answered above, but here's another approach you could take. I can't find the original post I adapted this from, but here's the code for the sake of your question. As noted above, the flatMap() function is what you'd be looking to utilize with Java 8. You can throw it in a utility class and just call "RandomUtils.combine(list1, list2, ...);" and you'd get a single List with all values. Just be careful with the wildcard - you could change this if you want a less generic method. You can also modify it for Sets - you just have to take care when using flatMap() on Sets to avoid data loss from equals/hashCode methods due to the nature of the Set interface.
Edit - If you use a generic method like this for the Set interface, and you happen to use Lombok, make sure you understand how Lombok handles equals/hashCode generation.
/**
* Combines multiple lists into a single list containing all elements of
* every list.
*
* @param <T> - The type of the lists.
* @param lists - The group of List implementations to combine
* @return a single List<?> containing all elements of the passed in lists.
*/
public static <T> List<?> combine(final List<?>... lists) {
return Stream.of(lists).flatMap(List::stream).collect(Collectors.toList());
}
Solution 4
In Java 8 we can use stream List1.stream().collect(Collectors.toList()).addAll(List2); Another option List1.addAll(List2)
Solution 5
To merge several lists or other types of collections into a single one you can use this approach:
Stream.of(list1.stream(), list2.stream(), someSet.stream(), otherCollection.stream())
.flatMap(Function.identity())
.collect(Collectors.toList());
mat_boy
Updated on September 02, 2021Comments
-
mat_boy over 2 years
I have the following situation
Map<Key, ListContainer> map; public class ListContainer { List<AClass> lst; }
I have to merge all the lists
lst
from theListContainer
objects from aMap
map.public static void main(String[] args) { List<AClass> alltheObjectsAClass = map.values().stream(). // continue.... }
Any idea how, using Java 8 stream API?
-
Puce over 7 years? Why were the comments deleted that explained why
.flatMap(Collection::stream)
is not possible here? -
TWiStErRob over 7 years@Puce good question, however it's kind-of possible: if
ListContainer
is encapsulated (i.e. has a getter forlst
), you can decompose.flatMap(->)
into.map(ListContainer::getLst)
.flatMap(Collection::stream)
-
Puce over 7 years@TWiStErRob Yes, that is what I wrote in my original comment. Why was it deleted?
-
Tony BenBrahim almost 7 yearsplus 1 even though it does not answer the question, it answers the title of the question
-
SQB over 5 years@Puce a note in your answer explaining why
.flatMap(Collection::stream)
is no possible would be better, I think. Should be more permanent. -
Alex over 4 yearshow we can make
flatMap(listContainer -> listContainer.lst.stream())
null pointer safe? In case iflistContainer.lst
equalsnull
. Ok I got it. Sorry for that - we can do simple.filter(listContainer -> listContainer.lst != null)
before flatMap -
Puce over 4 years@Alex yes, this is one way resp.
.map(listContainer -> listContainer.lst).filter(Objects::nonNull).flatMap(Collection::stream)