File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Has anyone sort 2 lists before? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Has anyone sort 2 lists before?" Watch "Has anyone sort 2 lists before?" New topic

Has anyone sort 2 lists before?

Davie Lin
Ranch Hand

Joined: Aug 05, 2007
Posts: 294
I have 2 list of custom objects. Both are queries from same table in DB. The bigger list is with a specified column NOT NULL and the smaller list is with the specified column NULL. Pretty much all of the properties in the custom object are String. I need to have a way to compare one of the properties and merge the 2 list into one. Is that possible?
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

It's quite likely possible, although you would have to clean up the description of what you want to do. Right now it's a bit incomplete, or maybe garbled. I don't see what "compare one of the properties" is supposed to mean, and your post title mentions sorting but your post itself doesn't.

But anyway, yeah, it's probably doable.
Davie Lin
Ranch Hand

Joined: Aug 05, 2007
Posts: 294
Thanks Paul

The "Compare one of the properties" should be more like String.compareTo(OtherString) > 0 or < 0 or ==0 and sort them accordingly and place it into a new list. Merge these two into one and return it. I have try using iterator and the new list had duplicates. I will try a different approach` with List API.

Steve Harney

Joined: Jun 19, 2010
Posts: 19
I think the TreeSet will give you what you are after, Implement a Comparator to define the order you are after and make sure that your classes implement both the .equals() and .hashcode() methods.

It will remove duplicates and once you are done you can convert it out to an array.

The other option if the values are already in a database table, are you able to form a db query to give you what you are after?

I agree. Here's the link:
subject: Has anyone sort 2 lists before?
It's not a secret anymore!