IntelliJ Java IDE
The moose likes Performance and the fly likes ArrayList searching and sorting Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Performance
Reply Bookmark "ArrayList searching and sorting" Watch "ArrayList searching and sorting" New topic
Author

ArrayList searching and sorting

Jon Dornback
Ranch Hand

Joined: Apr 24, 2002
Posts: 137
i have a sticky performance question. here's the situation:
my jsp page uses a bean to connect to the database and then store the results in an ArrayList of Articles. Articles is my class that stores the title, body, database id, and order (which order to display the articles on the page). this is done for encapsulation purposes, so that anyone implementing the classes do not need to handle db connections directly. The articles in the arraylist are automatically stored in order of their rank, so that a single loop will print them out in the correct order. however, i also need to get a single article by it's db id.
the question is: would it be better to re-sort the arraylist based on the database id (requiring a comparator, etc), or simply do a linear search through the arraylist? in my situation, the arraylist will never be very big, but i'm interested in opinions on which approach is better for medium or huge lists as well.
thanks in advance,
Jon Dornback


use the [CODE] tags - it makes it much easier for people to help you.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18652
I would use two different collection-type objects to store references to the Articles, one for order by rank, and one for looking up by db id number. This may seem wasteful, but remember that each collection doesn't really store all the articles inside it - just a reference to each one. So if you have 1000 articles, you will have 1000 Article objects, and two collections containing a total of 2000 references to the Articles. (Not 2000 Article objects.)
For the rank attribute, it seems you need to maintain an order for iteration, but don't really need to look up by rank, so a List (ArrayList or LinkedList) might be best. If you need to look up by rank as well as keep articles in order, a TreeMap might be best. (Also good if you are adding and removing articles frequently - you don't need to re-sort the whole tree for each change.)
For the db id attribute, a HashMap is probably the best bet, as it generally offers the fastest lookup for larger collections - but it doesn't maintain order for iteration.
Naturally, the best choice for each collection will depend on how you use it. How often do you perform each type of operation, and which operations are most time-critical. Thomas Paul has written several good articles about this for our newsletter - check out this one in particular. Remember that the performance numbers can vary substantially depending on your application and environment, so what looks best on paper may not turn out to be best in practice - test for yourself.


"I'm not back." - Bill Harding, Twister
Chris Smith
Ranch Hand

Joined: May 03, 2002
Posts: 42
This really depends on what kinds of stuff you're doing. If the search by database ID happens on average once every four or five searches, then you're probably better off simply doing a linear search when it needs to occur. It's also possible that you want to maintain a data structure that allows efficient searching by database ID.
As Jim mentioned, you can store data in multiple data structures simultaneously. There's no reason the records can't be placed into both a HashMap and an ArrayList. However, remember that the time required to build a HashMap will always be higher than the time required to do a linear search by database ID. The memory requirement will also be linear on size, where a search through an unsorted array is still constant space. So, before you start building data structures for that purpose, check that you're doing more than one search per query, on average.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Chris Smith:
As Jim mentioned, you can store data in multiple data structures simultaneously. There's no reason the records can't be placed into both a HashMap and an ArrayList. However, remember that the time required to build a HashMap will always be higher than the time required to do a linear search by database ID. The memory requirement will also be linear on size, where a search through an unsorted array is still constant space.

Another drawback is the duplicated data: You now have to maintain two collections, to ensure consistency between them. So don't do this lightly, but only when you are sure that the improved performance will justify the increased maintenance costs.


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
Jon Dornback
Ranch Hand

Joined: Apr 24, 2002
Posts: 137
thanks for the help - great advice all around. i'm sticking with a linear search for retrieving by db id since there will only be about 20 elements at most in the ArrayList, and it will not be frequently called. it was just one of those interesting "what if this was huge...?" type questions that i wanted to look in to. thanks again.
Jon
Chad McGowan
Ranch Hand

Joined: May 10, 2001
Posts: 265
If you are using JDK 1.4, how about a LinkedHashMap? Use database Id as the key and insert the Articles in order of their rank. This way you maintain the original ordering and still have quick retreival using database Id.
(I haven't yet used 1.4, so there may be other considerations with this class, but it sounds like exactly what you need).
Chad
 
IntelliJ Java IDE
 
subject: ArrayList searching and sorting
 
Threads others viewed
Database structure
Disabling html:select by form bean property
Some question about Java Progarmming
Efficient Caching
Java Progarmming about Database
developer file tools

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture