25
List<List<String>> superlist = new ArrayList<List<String>>();
List<String> list1 = new ArrayList<String>();
list1.add("a1");
list1.add("a2");
List<String> list2 = new ArrayList<String>();
list2.add("b1");
list2.add("b2");
List<String> list3= new ArrayList<String>();
list3.add("c1");
list3.add("c2");
superlist.add(list1);
superlist.add(list2);
superlist.add(list3);
List<String> result= new ArrayList<>();

Now I want to create a new list which contains all the values in superList. Here result should contain a1,a2,b1,b2,c1,c2

Michael
44.5k12 gold badges98 silver badges143 bronze badges
asked Apr 14, 2015 at 18:50
2
  • have you made an attempt at doing this on your own? a couple of foreach loops should do the job perfectly well. Commented Apr 14, 2015 at 18:53
  • stackoverflow.com/questions/25147094/… Commented Dec 20, 2016 at 3:45

5 Answers 5

61

Try like this using flatMap:

List<List<Object>> list = 
List<Object> lst = list.stream()
 .flatMap(Collection::stream)
 .collect(Collectors.toList());
Gautham
8869 silver badges16 bronze badges
answered Apr 14, 2015 at 18:56
Sign up to request clarification or add additional context in comments.

1 Comment

I will leave this here in case it helps someone, if instead of List<List<Object>> you have List<Object[]>, use Arrays::stream instead of Collection::stream
7

If you're on Java < 8 (and cannot use Streams), you can do this in a one-liner with Guava's Iterables.concat:

List<String> merged = Lists.newArrayList(Iterables.concat(superList));
answered Apr 14, 2015 at 19:37

Comments

3
superlist.forEach(e -> result.addAll(e));

Now after some reasarch, I found this way.

answered Apr 14, 2015 at 19:23

2 Comments

Which is the best method for the question in terms of performance
Actually, a plain for-loop would be even faster since iterators tend to consume quite a bit of memory if you use them all over the place. But most of the time performance is not even an issue, especially nowadays. I just noticed that this answer was posted almost 6 years ago.
2

You would have to loop through every List in your superlist object in order to get all of the contents. You can use the addAll() method to copy each list's contents to your new List:

List<String> result = new ArrayList<String>();
for (List<String> list : superlist) {
 result.addAll(list);
}
answered Apr 14, 2015 at 18:53

Comments

0

This works for me!

List<String> result= new ArrayList<>();
 
 superlist.stream().forEach(
 
 n->result.addAll(n)
 );
 
 System.out.println(result);
answered Jan 24, 2021 at 5:41

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.