aspose file tools*
The moose likes Beginning Java and the fly likes How to add multple lists in one list in insertion order Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to add multple lists in one list in insertion order" Watch "How to add multple lists in one list in insertion order" New topic
Author

How to add multple lists in one list in insertion order

Amit K Jain
Ranch Hand

Joined: Nov 06, 2008
Posts: 55
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

Joined: Oct 27, 2005
Posts: 19759
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Claudiu Chelemen
Ranch Hand

Joined: Mar 25, 2011
Posts: 71

Just create the empty parentlist, then addAll the lists you wish.

Eg:

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
. . . 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

Joined: Nov 06, 2008
Posts: 55
Ahh! It was so quick and easy.

Thank you all
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to add multple lists in one list in insertion order