tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From David Kerber <>
Subject Re: [OT] Help with java Lists
Date Wed, 12 Dec 2007 02:57:32 GMT
Christopher Schultz wrote:
> Hash: SHA1
> David,
> You need a collection that is:
> David kerber wrote:
>> I need to have some kind of list or collection that I can search quickly
>> for a specific entry
> Hashed
>> and then start stepping through the list item by
>> item from that point.
> Linked
> The only implementation that meets those needs is a LinkedHashSet.
> Unfortunately, there's no way to start iterating from the middle of this
> type of data structure (nor any Set for that matter). Sets do not have
> an ordering, but a LinkedHashSet provides an order. On the other hand,
> it does not provide a way to ask "where is this item in the set"?
> For that, you need a List.
> I would recommend building your own data type that contains both a List
> (for identifying items by index and performing manual iterating rather
> than using an Iterator) and a HashMap or something similar for quick
> lookups. Store your object -> list index in the map and the objects
> themselves in the list. You can even write your own iterator to start in
> the middle of a list once you've identified where to start.
>> If it seems like I'll never get reasonable speed this way, I could
>> switch to calling all the stores' data from the database at once, making
>> the lists huge, but only needing to load them once.  However, this makes
>> speed in searching the lists much more of an issue, and I don't know
>> which way is going to give me the best overall performance for this
>> report generation.
> Sounds like a lot of premature optimization to me. Why not just read
> what you need from the database each time?
I'm already on my 3rd attempt at optimization for this section.  The 
first round was having the db do _all_ the work, submitting a complex 
query (a view, actually) and returning a resultset with all the data I 
need.  The query took forever, and the java code to produce the report 
was really fast. 

2nd attempt was just the opposite:  the db calls were quick but the java 
code slowed everything down because looped through the list of sites, 
and for each site reads each its data from the db's base tables (5 of 
them), putting each table's data into a resultset and loads the 
resultset data into an iterable collection (ArrayList).  I haven't fully 
isolated the bottleneck of this version, but I think the slowness was 
due to searching the lists for the appropriate data. 

The 3rd one I'm trying now keeps the db queries simple, but I'm trying 
to be more intelligent in my handling of the lists, by using TreeMaps 
and keeping a set of pointers so I don't need to search the lists for 
each piece of data, but can gather all the report data with a single 
scan through each TreeMap.

One issue I ran into earlier today with plain HashMaps was that I 
couldn't figure out how to search for a specific piece of data, which 
requires matching on a site number, a date and a shift, and for some 
data another date.  However, as I was typing this I realized that I just 
need to generate a hash key from those fields when loading up the 
HashMap, and then generate a search key using the same algorithm when I 
need to find a given item.  Tomorrow I'll revisit this to see if it 
helps me out, but I think I have a workable solution right now, though 
the code is rather more complex than I would like to keep my navigation 
of all those TreeMaps in sync.

Thanks for the comments!

To start a new topic, e-mail:
To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message