java - TreeSet definition of an iterator is not consistent with the interface Iterator? -


i using legacy code uses treeset perform sort of collection of objects. (alternatives using treeset reference, not looking change in particular).

the class has 2 methods iterator()

iterator()

returns iterator on elements in set in ascending order.

and descendingiterator()

descendingiterator()

returns iterator on elements in set in descending order.

my concept of iterator interface implemented collection or in own implementation, you can not assume specific ordering.

in implementation provided assumed 2 calls give iterator in correct ordering. results now, fear is false assumption , since violates iterator interface principles, change in future.

i not have experience treeset , not see traversal methods poll elements in order. there way or stick iterator's , hope 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    (beatdesc beatdesc : itbeatdesc){         .... } 

so using format create binding between block 1 , block 2

i don't know got idea "you can not assume specific ordering" of elements returned iterator. javadoc says:

an iterator on collection.

it doesn't ordering.

now, javadoc of iterator() method of collection says:

returns iterator on elements in collection. there no guarantees concerning order in elements returned (unless collection instance of class provides guarantee).

for example, treeset, linkedhashset, enumset, , list implementations do guarantee defined order:

  • treeset - elements ordered using natural ordering, or comparator provided @ set creation time, depending on constructor used.
  • linkedhashset - linked list defines iteration ordering, order in elements inserted set (insertion-order).
  • enumset - iterator returned iterator method traverses elements in natural order (the order in enum constants declared).
  • list - ordered collection (also known sequence). - iterator() returns iterator on elements in list in proper sequence.

Comments

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -