File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Storing Strings in Order Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Storing Strings in Order" Watch "Storing Strings in Order" New topic
Author

Storing Strings in Order

Gobind Singh
Ranch Hand

Joined: Aug 04, 2006
Posts: 60
I have a requirement to read data from a database and store the data in POJO instances.

Each iteration of the resultset, i will instantiate a pojo and populate its attributes with the database data. I will then put the pojo into a list.

One of the fields I am reading from the database and populating the POJO with is called "productDescription".

I need to be able to store the pojo instances in ORDER.

One way to do it is to order the data using the ORDER BY clause in the SQL.

Is there a way in java that you can insert stuff into a list type data structure in order? perhaps implementing Comparable interface....???
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
A java.util.List is always ordered (it retains elements in whatever order you put them in), but it isn't necessarily sorted. You can sort the elements after you're done inserting them using Collections.sort(). Or you can replace the List with a TreeSet, which will sort the elements as they are added. Note that both Collections.sort() and TreeSet can work with either Comparable or Comparator objects - consult the docs to see how.


"I'm not back." - Bill Harding, Twister
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
The difference between the two approaches being that a TreeSet will eliminate duplicates.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Storing Strings in Order
 
Similar Threads
reg Value List Handler Pattern !!!!
Printing out info from mysql database
Need help with tab delimited scanning
writing pojo in database
Nested Loop