Reputation: 3532
I am using some legacy code which uses TreeSet
to perform the sort of a collection of objects. (alternatives to using TreeSet are a good reference, but I am not looking to change that in particular).
The class has two methods iterator()
iterator()
Returns an iterator over the elements in this set in ascending order.
and descendingIterator()
descendingIterator()
Returns an iterator over the elements in this set in descending order.
My concept of the Iterator
interface implemented by a Collection
or in own implementation, is that you can not assume any specific ordering.
In the implementation already provided it is assumed that the two calls give an iterator in correct ordering. The results are good for now, my fear is that it is a false assumption and since it violates the Iterator
interface principles, it could change in the future.
I do not have experience with TreeSet
and I do not see traversal methods to poll the elements in order. Is there a way to do this or just stick with the Iterator
's and hope for the best?
EDIT
Example usage:
TreeSet<BeatDesc> beatsOrderedTS = new TreeSet<>(new Comparator<BeatDesc>() {
@Override
public int compare(BeatDesc lhs, BeatDesc rhs) {
return lhs.getTS() - rhs.getTS() < 0 ? -1 : 1;
}
});
BeatDesc latest = beatsOrderedTS.descendingIterator().next()
EDIT
{//block 1
Iterator<BeatDesc> itBeatDesc = beatsOrderedTS.descendingIterator();
}
{//block 2
for (BeatDesc beatDesc : itBeatDesc){
....
}
So by using this format you create a binding between block 1 and block 2
Upvotes: 0
Views: 393
Reputation: 159114
I don't know where you got the idea that "you can not assume any specific ordering" of the elements returned by an Iterator
. The javadoc just says:
An iterator over a collection.
It doesn't say anything about ordering.
Now, the javadoc of the iterator()
method of Collection
says:
Returns an iterator over the elements in this collection. There are no guarantees concerning the order in which the elements are returned (unless this collection is an instance of some class that provides a guarantee).
For example, TreeSet
, LinkedHashSet
, EnumSet
, and all List
implementations do guarantee a defined order:
TreeSet
- The elements are ordered using their natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used.LinkedHashSet
- This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order).EnumSet
- The iterator returned by the iterator method traverses the elements in their natural order (the order in which the enum constants are declared).List
- An ordered collection (also known as a sequence). - iterator()
returns an iterator over the elements in this list in proper sequence.Upvotes: 3
Reputation: 37033
TreeSet is meant to maintain the order (either ascending or descending) of the elements as per either :
The Object that you are going to insert into the set implement's Comparable
something like:
class MyObject implements Comparable<MyObject> {..
while constructing TreeSet
where you pass your implementation of how you want to order elements within set by implementing Comparator
interface something like:
class MyComparator implements Comparator<MyObject> {..
... new TreeSet<>(new MyComparator());
Hence it is guaranteed you would get the elements in order as per your implementation of those interfaces.
Upvotes: 0