aspose file tools*
The moose likes Beginning Java and the fly likes Merging Multiple Lists Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Merging Multiple Lists" Watch "Merging Multiple Lists" New topic
Author

Merging Multiple Lists

Tapan Thakkar
Greenhorn

Joined: Mar 09, 2010
Posts: 24
Hey guys,....

i m trying to retrieve data from table....

i have stored each record in different List object.

when i print those all list they print it well on console..

but now i want to merge all those list to single list...

so that i can print all records using single print statement...............

List lst1 = (record1)
List lst2 = (record2)
List lst3 = (record3)

how to append all above list to single list......

please guys help me......
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3267
    
  52

Welcome to JavaRanch
Please CarefullyChooseOneForum - I'm moving this one to the appropriate forum for you.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Use addAll() to add all the elements of each list to a single list.


[Jess in Action][AskingGoodQuestions]
Tapan Thakkar
Greenhorn

Joined: Mar 09, 2010
Posts: 24
thanks Ernest Friedman-Hill for replying

i tried it but it gives me null pointer exception....

and in addAll it says pass collection as argument....

what should i do....

List lst;
Collection coll = (record from table);
if( lst.addAll(coll))
{
System.out.println("Added to list");
}
else
{
System.out.println("Fail");

and it always prints Fail....

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

You need to initialize "lst" before calling lst.addAll()


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3267
    
  52

...and please UseCodeTags
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Well, does it give you a NullPointerException, or does it print "Fail"? I don't see how it could do both, so which is it?

If you get a NullPointerException, it probably means that either lst or coll has not been initialized properly. Also, look carefully at the stack trace. Exactly which line is throwing the exception?

If you get "Fail", it may mean that coll is empty. Try printing the value of coll.size() - is it zero? Are you sure it shouldn't be zero? You may need to look at the code that loads records from the table.
Ravishanker kumar
Ranch Hand

Joined: Jul 20, 2006
Posts: 53
lst.addAll(coll) should work. If you are getting some issue, please check your code.
List lst;
Collection coll = (record from table);
......

Please initialize the list
List lst= new ArrayList()
and check whether (record from table) is null or not, if it is not null then what is its data type? is it of type Collection?
Tapan Thakkar
Greenhorn

Joined: Mar 09, 2010
Posts: 24
code i have used is...

List lst = Collections.EMPTY_LIST;
Collection coll1;

coll = (record from table);

System.out.println(coll1);
//it prints whole on console..
similarly two another Collection coll2,coll3...

it also prints record on console..

bt when i try to append then in a single List or Collection it gives error...n says null pointer exception.......

is there any other way to append list...
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
OK, so if it gives a NullPointerException, please don't tell us "it always prints Fail" - because that's not true. You're wasting people's time with false information, and we can't help you effectively that way.

I have a couple different theories about what the problem is, which may depend on the exact Java version and OS you're using. On one hand, I think using Collections.EMPTY_LIST is a bad idea here, since it's immutable, and therefore should throw an exception if you try to add stuff to it. But that shouldn't give a NullPointerException unless EMPTY_LIST is implemented very poorly. So maybe there's another problem. Let's focus on your exact error message. Can you give us the complete stack trace of the NullPointerException? That would be most helpful.
Tapan Thakkar
Greenhorn

Joined: Mar 09, 2010
Posts: 24
hello guys...
i have solved the error...
i have done that using vector..
code is...

Collection collection_fname;
Collection collection_email;
Vector collection_result = new Vector();
String search;

//i m creating search kind of project which retrieve data from table...
//when user enter data in textbox....it will be stored in string search...using getter n setter method....
search = getterMethod; //it has value say "abc+xzy"

String []temp = search.split("\\+");

//so now i have temp[0] = abc and temp[1] = xyz

int i = temp.length;
for(int j=0;j<i;j++)
{
collection_fname = table1LocalServiceUtil.findAllfname(temp[j]); //this will retrieve record with value temp[j] in Fname column...of table...
System.out.println("Collection in java="+collection_fname);
if(collection_fname!=null)
{
System.out.println("Inside If 1");
collection_result.addAll(collection_fname); //this will add record from collection to Vector declared above.
}
// similarly .....
//if it will not find record with current value of temp[i] then collection will be null....
//this will retrieve record with email xyz.....

collection_email = table1LocalServiceUtil.findAllEmail(temp[j]);
if(!collection_email.isEmpty())
{
collection_result.addAll(collection_email); //this will add record from collection to Vector declared above.
}

}
//now i have all records in single vector......
System.out.println(collection_result);
//this wil print all records...................

thank you friends...for your suggestion.... as this was my first post on this forum....

it really helped me to get the solution....

i didn't expected so fast reply.....

but you guys are awesome...........

once again thank you............
>
Geet Chaudhary
Greenhorn

Joined: Feb 15, 2010
Posts: 11
Tapan Thakkar wrote:code i have used is...

List lst = Collections.EMPTY_LIST;
Collection coll1;

coll = (record from table);

System.out.println(coll1);
//it prints whole on console..
similarly two another Collection coll2,coll3...

it also prints record on console..

bt when i try to append then in a single List or Collection it gives error...n says null pointer exception.......

is there any other way to append list...



hi Tapan ,
I have tried the below the code and its working absolutely fine
You can also try it in your code :

List<String> l1 = new ArrayList<String>();
l1.add("1");
l1.add("2");
l1.add("3");
List<String> l2 = new ArrayList<String>();
l2.add("4");
l2.add("5");
l2.add("6");
List<String> l3 = new ArrayList<String>();
l3.addAll(l1);
l3.addAll(l2);
System.out.println("Third: " + l3);

Its displaying output " Third: [1, 2, 3, 4, 5, 6] " on the console.

Thanks
Tapan Thakkar
Greenhorn

Joined: Mar 09, 2010
Posts: 24
Sugita Chaudhary wrote:
Tapan Thakkar wrote:code i have used is...

List lst = Collections.EMPTY_LIST;
Collection coll1;

coll = (record from table);

System.out.println(coll1);
//it prints whole on console..
similarly two another Collection coll2,coll3...

it also prints record on console..

bt when i try to append then in a single List or Collection it gives error...n says null pointer exception.......

is there any other way to append list...





hi Tapan ,
I have tried the below the code and its working absolutely fine
You can also try it in your code :

List<String> l1 = new ArrayList<String>();
l1.add("1");
l1.add("2");
l1.add("3");
List<String> l2 = new ArrayList<String>();
l2.add("4");
l2.add("5");
l2.add("6");
List<String> l3 = new ArrayList<String>();
l3.addAll(l1);
l3.addAll(l2);
System.out.println("Third: " + l3);

Its displaying output " Third: [1, 2, 3, 4, 5, 6] " on the console.

Thanks







thank you sugita....

thank you for your help...
Ravishanker kumar
Ranch Hand

Joined: Jul 20, 2006
Posts: 53

Post Today 1:03:21 PM Subject: Merging Multiple Lists
hello guys...
i have solved the error...
i have done that using vector..

A small suggestion here, we should not use Vector until it is required. Vector is thread safe and there are some overheads of this. If possible use ArrayList instead of Vector. Root cause of NullPointerException is not ArrayList that is something else. You should find out root cause of that.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

People, please UseCodeTags.

Tapan Thakkar wrote:List lst = Collections.EMPTY_LIST;

Collections.EMPTY_LIST is unmodifiable, you can't change its contents. How about following the advice Ravishanker kumar gave you:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Tapan Thakkar
Greenhorn

Joined: Mar 09, 2010
Posts: 24
hey guys thank you for your reply...

as i am getting record in my list...

but sometime i m getting same records more then one times....

how can i remove redundant records from my list...


thank you...

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

Don't use a List but a Set, and make sure to implement the equals and hashCode methods of the classes properly. If the insertion order is important you can use a LinkedHashSet as the implementation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Merging Multiple Lists