iterator to go backwards

I

Ike

How do I make an Iterator go through the elements,
(SortedMap.keySet().iterator() ) in reverse order? Thanks Ike
 
J

John C. Bollinger

Ike said:
How do I make an Iterator go through the elements,
(SortedMap.keySet().iterator() ) in reverse order? Thanks Ike

You can't. General Iterators don't work that way. You have some
alternatives:

(1) Use a Comparator on your SortedMap that reverses the sense of the
comparison. That's only useful if it is reasonable to use the reverse
order globally within the map.

(2) Dump the key set into a different collection (or an array) where you
can sort it how you want. You will still need an appropriate Comparator
with which to define the order. You could use a SortedSet, a List, or
an array (via toArray()) as the intermediate object; in the latter two
cases you would first populate it and than sort it with Collections.sort
or Arrays.sort, respectively. If performance matters then the array
approach will probably be fastest, the List should be reasonably fast
(depending on the type of list), and the SortedSet will probably be
considerably slower (because it is re-sorted with every insertion).
None will be as fast as keeping the map in the opposite order in the
first place, of course.


John Bollinger
(e-mail address removed)
 
S

Søren Bak

There is no support for backward iteration in Iterators.

You can use recursion or a stack to solve the problem, but you should only
do this for relatively small sets (or if performance is not an issue).

Here is a sketch of a new iterator-class that uses a stack internally to do
the reversal:

class ReverseIterator implements Iterator {

Stack stack;

public ReverseIterator(Iterator iterator) {
stack = new Stack();
while (iterator.hasNext())
stack.push(i.next());
}

public boolean hasNext() {
return !stack.isEmpty();
}

public Object next() {
return stack.pop();
}

}

Rgds,
Søren Bak
 
R

Roedy Green

How do I make an Iterator go through the elements,
(SortedMap.keySet().iterator() ) in reverse order? Thanks Ike

you define the Comparable in reverse for a different set. You can
extract the elements as an array and sort them in reverse order.
 
?

=?ISO-8859-15?Q?Thomas_Gagn=E9?=

I'll have to do some reading about Map, but couldn't aMap's elements be copied
to a collection that could be reverse()d and iterated over?

No need to answer. I'll read.
 
J

Jason

With an iterator you can't, but where there's a need, a way presents.
What you can do is extend the iterator class and add methods for
navigating backwards by keeping track of the current index of the
ArrayList. You would also have to overwrite hasNext() and next()
while providing your own hasPrevious() and previous() methods. The
critical element is that every time you get an element, you will have
to increment or decrement the index pointer to make sure that you are
where you're supposed to be.

Regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top