aspose file tools*
The moose likes Beginning Java and the fly likes Should I use a list for this? Or something else? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Should I use a list for this? Or something else?" Watch "Should I use a list for this? Or something else?" New topic
Author

Should I use a list for this? Or something else?

K DeLucia
Ranch Hand

Joined: Apr 11, 2008
Posts: 68
I have an application that pulls some data from a relational database table and writes it out to a web page (using java beans/jsp pages). Currently my code pulls the data from the relational database table into a java List like this:

List<String> myList = gateway.getData(connection,myKey.toString())

Now I'm in the position where I need to do the above more than once to pull data from different tables together. I can append one list to the other, but now I'm not sure how I can sort it.

An example of my list would be:
0 Alan
1 Smith
2 Red
3 09/09/2007
4 Ann
5 Jones
6 Yellow
7 10/11/2005
8 Fred
9 Williams
10 Blue
11 06/02/2008
...

I now need to sort it by date, keeping the sets of 4 together (Alan Smith Red 9/9/2007 needs to stay together).

When I populate my list, I could populate as such:
0 09/09/2007~Alan~Smith~Red
1 10/11/2005~Ann~Jones~Yellow
2 06/02/2008~Fred~Williams~Blue
...
and then sort it then split out the string to write it to web page. I'm not sure how well that would work, but it seems like there must be a better way.

Being new to java I don't really know what my options are. It seems like there must be a better way. Is there some other way I should be doing this? Should I be putting my data into an array? A map? Something else I don't know about yet (which is a lot!)

Any ideas are greatly appreciated.
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

I would recommend that the gateway.getData() method instead of returning a list with each piece of data in it, it returns an list of objects that each represents a data group:

0 DataObject{09/09/2007~Alan~Smith~Red}
1 DataObject{10/11/2005~Ann~Jones~Yellow}
2 DataObject{06/02/2008~Fred~Williams~Blue}

Then the data object has getters to getDate, getFirstName, getColor, etc.

You can then sort the list using the Collections.sort(List<T> list, Comparator<? super T> c) method. You can create a custom Comparator that sorts based on the date, another one that sorts based on the first name, etc.
[ August 20, 2008: Message edited by: Mark Vedder ]
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Your best bet would be to create an object that contains the four properties that are shared. Put all the instances of this object into a list, and generate a Comparator that orders the list as you need them.

An example:


You could then have a Comparator that compares one Person with another based on whatever criterion you define, and orders them appropriately. See This On Java Article for some help with that.


Steve
K DeLucia
Ranch Hand

Joined: Apr 11, 2008
Posts: 68
Thank you so much! This really helps! I'm off to give it a try!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Should I use a list for this? Or something else?