• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
K DeLucia
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much! This really helps! I'm off to give it a try!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic