Re: "PriorityMap"



Andreas Leitgeb wrote:
I'm in search of a data structure for the followig usecase:

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. (whichever is easier to achieve)
The keys (k) are not necessarily unique, in fact I usually get a
couple of different values (v) for each k, and I'm interested only
in the v's, that came with those ten(*) smallest k's. The actual
k's do not matter afterwards.

There are much too many to save them all and filter afterwards.

PriorityQueue and TreeMap do not seem to make it easy to efficiently prune them beyond the first ten(*) entries after each add().

I'd use a PriorityQueue but with either a Comparable element type or a
Comparator that uses descending order of the key values. That way, the
head item is the one with the largest key, and the one that should be
removed when an insertion increases the queue size beyond 10.

Patricia
.



Relevant Pages

  • Re: "PriorityMap"
    ... Some algorithm spits out *lots* of pairs of type. ... I only want either those tenpairs with the lowest keys, ... add every result to a SortedMap (or sorted set) and then do an iteration to find the first ten. ...
    (comp.lang.java.programmer)
  • Re: "PriorityMap"
    ... Some algorithm spits out *lots* of pairs of type. ... I only want either those tenpairs with the lowest keys, ... and will be rejected on the first comparison. ...
    (comp.lang.java.programmer)
  • "PriorityMap"
    ... Some algorithm spits out *lots* of pairs of type. ... I only want either those tenpairs with the lowest keys, ... k's do not matter afterwards. ... efficiently prune them beyond the first tenentries ...
    (comp.lang.java.programmer)
  • Re: "PriorityMap"
    ... Some algorithm spits out *lots* of pairs of type. ... I only want either those tenpairs with the lowest keys, ... I'd use a PriorityQueue but with either a Comparable element type or a ...
    (comp.lang.java.programmer)