S
Sasha
Hi there,
is there an efficient way I can get the n largest objects from a SortedSet
(i.e. java.util.TreeSet), without modifying it ?
If the TreeSet has m entries and I want n values, then (I think) you can get
the n SMALLEST entries in O(n . lg m) steps. ie --
Iterator i = sortedSet.iterator()
for (int count = 0; i.hasNext() && count < n; count++) {
Object o = i.next();
// do stuff with the object
}
But if I don't want to actually modify or duplicate the set (it's extremely
large), how can I get the n LARGEST values? I don't have a reference to any
objects appropriate for tailSet(). I suppose what I really want is a reverse
iterator -- any such beast?
AFAIKS, duplicating the list and re-sorting (or successively removing the
tail) will give O(m . lg m + n . lg m).
Can anyone see what I've overlooked?
Thanks,
Sasha
is there an efficient way I can get the n largest objects from a SortedSet
(i.e. java.util.TreeSet), without modifying it ?
If the TreeSet has m entries and I want n values, then (I think) you can get
the n SMALLEST entries in O(n . lg m) steps. ie --
Iterator i = sortedSet.iterator()
for (int count = 0; i.hasNext() && count < n; count++) {
Object o = i.next();
// do stuff with the object
}
But if I don't want to actually modify or duplicate the set (it's extremely
large), how can I get the n LARGEST values? I don't have a reference to any
objects appropriate for tailSet(). I suppose what I really want is a reverse
iterator -- any such beast?
AFAIKS, duplicating the list and re-sorting (or successively removing the
tail) will give O(m . lg m + n . lg m).
Can anyone see what I've overlooked?
Thanks,
Sasha