Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Merging Multiple Lists

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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......
 
Sheriff
Posts: 6871
1312
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch
Please CarefullyChooseOneForum - I'm moving this one to the appropriate forum for you.
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use addAll() to add all the elements of each list to a single list.
 
Tapan Thakkar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....

 
Bartender
Posts: 11445
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to initialize "lst" before calling lst.addAll()
 
Devaka Cooray
Sheriff
Posts: 6871
1312
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...and please UseCodeTags
 
Master Rancher
Posts: 4055
56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Master Rancher
Posts: 4055
56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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............
>
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 22508
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Tapan Thakkar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22508
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic