Re: "PriorityMap"
- From: Logan Shaw <lshaw-usenet@xxxxxxxxxxxxx>
- Date: Sun, 30 Mar 2008 22:58:07 -0500
Eric Sosman wrote:
Andreas Leitgeb wrote:
Some algorithm spits out *lots* of pairs (k,v) of type (int,long).
I only want either those ten(*) pairs with the lowest keys, or all
pairs with the lowest ten(*) keys.
Since you have "*lots*" of (k,v) pairs, it would probably
be nice to avoid the overhead of creating an Integer and a Long
and a Map.Entry (or similar) for each pair. I'd suggest just
coding the thing up for the purpose at hand -- a simple heap
made out of an int[] and a long[] seems attractive. Should be
pretty fast, too: Once you've been through a few thousand pairs,
most of the remaining pairs will be bigger than the heap's root
and will be rejected on the first comparison.
Yeah, assuming you don't have the bad luck of somehow occasionally
getting all the pairs in decreasing order.
Another approach is just ditch the heap and do linear search. In
other words, keep track of the largest int value in the int[] array,
and if you find one smaller, go find the one to evict using brute
force linear search. Since this will happen so infrequently, and
since 10 is very small in comparison to millions, this should be
pretty negligible. An advantage of this approach is that it's easy
to make it "stable", which I'm defining as this: if two values of
equal value arrive but they both can't fit in the output of ten
pairs, you keep the first one you saw.
Also, you could use a heap, but optimize the common case by duplicating
the value at the top of the heap as a separate variable of type int.
Then you get the full generality and good asymptotic performance,
but 99.9% of the time when you are comparing to the top of the heap,
you avoid creating an Integer object.
- Logan
.
- References:
- "PriorityMap"
- From: Andreas Leitgeb
- Re: "PriorityMap"
- From: Eric Sosman
- "PriorityMap"
- Prev by Date: Re: Will Java Enterprise Edition work well on Vista Business?
- Next by Date: JSP Progress bar
- Previous by thread: Re: "PriorityMap"
- Next by thread: Extracting Metadata from Windows Media files
- Index(es):
Relevant Pages
|