Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to add multple lists in one list in insertion order

 
Amit K Jain
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to add the list elements from multiple lists to one parent list. Can some some tell me how it can be done?
e.g.



I want the following in the output parent list
ParentList:[12,323,11,43,1,87]

Please help..
 
Rob Spoor
Sheriff
Pie
Posts: 20554
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Collection, and therefore also List, has a method called addAll to add all elements of any other collection in one go. For List, addAll will add all those elements at the end. So just create a new List, and call addAll for all Lists whose contents need to be added.
 
Claudiu Chelemen
Ranch Hand
Posts: 75
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just create the empty parentlist, then addAll the lists you wish.

Eg:

 
Campbell Ritchie
Sheriff
Pie
Posts: 49460
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and Lists retain insertion order as a default behaviour. You would have to look in the documentation for addAll to see which order it adds in. It says in the Java™ Tutorials that addAll concatenates Lists.
 
Amit K Jain
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh! It was so quick and easy.

Thank you all
 
Campbell Ritchie
Sheriff
Pie
Posts: 49460
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

You spend hours and hours designing the List classes, and once you have got them working, they become really easy to use.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic