• Post Reply Bookmark Topic Watch Topic
  • New Topic

Storing Strings in Order  RSS feed

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....???
 
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference between the two approaches being that a TreeSet will eliminate duplicates.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!