Re: A suitable container that can sort -- please help!
On Jul 7, 3:58 pm, Eric Sosman <Eric.Sos...@sun.com> wrote:
almu...@altavista.com wrote:
On Jul 7, 2:52 pm, Simon <count.numb...@web.de> wrote:
I need an object that can contain several hundred: Point objec=
ts and
their a corresponding double. I then need to be able to sort on the
double and then jump accross and grab the Point.
Any implementation of SortedSet [1], e.g TreeSet [2], together with a
suitable implementation of Comparator that compares your Point instanc=
es
should do the job. You can use the last() of a headSet() or the first(=
)
of a tailSet() to jump close to a particular point.
Just as an aside: For sorting points you have to define some relation,
e.g. "left-of" or "above" or "has greater norm". In Java, this is done
by the Comparator. I'm not sure what you mean by "corresponding double=
",
but it might be that none of the above is useful in your case and you
might be better off using a specialized geometric data structue, which
you probably have to implement yourself or take from a library.
Cheers,
Simon
[1]http://java.sun.com/javase/6/docs/api/java/util/SortedSet.html
[2]http://java.sun.com/javase/6/docs/api/java/util/TreeSet.html
Hi Simon,
You mean something liek:
SortedSet set = new TreeSet();
set.add(SomePoint, somedouble)
This won't work. The SortedSet contains instances of "somet=
hing,"
not pairs of instances. If you need to associate a Point with a double=
,
one straightforward approach is to make yourself a "holder" class that
contains the Point and its associated double, and to put instances of
the "holder" into the set:
class Holder {
final Point point;
final double value;
Holder(Point point, double value) {
this.point = point.clone();
this.value = value;
}
}
...
set.add(new Holder(somePoint, someDouble));
etc...
etc..
then extract first object?
If all you want is the first Point/double pair in sorted order=
,
keeping the entire collection sorted is a lot of wasted work. Some
possibilities:
- If you want only the extreme Point/double pair, just dump al=
l
the pairs into any convenient collection without regard to=
their
order. Then when you want the extreme, just sweep once =
over the
collection to find the pair with minimum (or maximum) valu=
e.
- If you gather all the Point/double pairs and then want to pr=
ocess
them in sorted order, begin by putting them in a List with=
out
regard to order. When you've gathered them all, sort th=
e List
with Collections.sort(), and then traverse the sorted List=
..
- If Point/double pairs are coming and going all the time and =
you
need to keep them in sorted order all the time, the extra =
work
of a SortedSet makes sense. Note that a SortedSet is a =
Set and
thus allows no duplicates; you can skirt this issue if nee=
d be
by letting your Holder class inherit the equals() method f=
rom
Object (so no two Holders will be "equal" even if they hap=
pen
to have equal Points and doubles).
--
Eric.Sos...@sun.com
Hi Eric,
I like the List option but I cannot add a double & Point
conbination to this. The "add" method under Lits is like this:-
List.add(int, object);
hence I can't add the double
Any ideas?
Al