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....???
Joined: Jan 30, 2000
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
Joined: Jul 11, 2001
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