iterator to go backwards

Discussion in 'Java' started by Ike, Jul 8, 2003.

  1. Ike

    Ike Guest

    How do I make an Iterator go through the elements,
    (SortedMap.keySet().iterator() ) in reverse order? Thanks Ike
    Ike, Jul 8, 2003
    #1
    1. Advertising

  2. Ike wrote:
    > 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
    John C. Bollinger, Jul 8, 2003
    #2
    1. Advertising

  3. Ike

    Søren Bak Guest

    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

    "Ike" <> skrev i en meddelelse
    news:4eGOa.90219$...
    > How do I make an Iterator go through the elements,
    > (SortedMap.keySet().iterator() ) in reverse order? Thanks Ike
    >
    >
    Søren Bak, Jul 8, 2003
    #3
  4. Ike

    Roedy Green Guest

    On Tue, 08 Jul 2003 20:56:32 GMT, "Ike" <> wrote or
    quoted :

    >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.
    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jul 9, 2003
    #4
  5. 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.

    --
    ..tom
    remove dashes in email for replies
    http://isectd.sourceforge.net
    =?ISO-8859-15?Q?Thomas_Gagn=E9?=, Jul 14, 2003
    #5
  6. Ike

    Jason Guest

    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,

    "Ike" <> wrote in message news:<4eGOa.90219$>...
    > How do I make an Iterator go through the elements,
    > (SortedMap.keySet().iterator() ) in reverse order? Thanks Ike
    Jason, Jul 15, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendrik Maryns
    Replies:
    18
    Views:
    1,418
  2. greg
    Replies:
    6
    Views:
    454
    Dietmar Kuehl
    Jul 17, 2003
  3. Replies:
    6
    Views:
    642
    Jim Langston
    Oct 30, 2005
  4. Steven D'Aprano

    What makes an iterator an iterator?

    Steven D'Aprano, Apr 18, 2007, in forum: Python
    Replies:
    28
    Views:
    1,155
    Steven D'Aprano
    Apr 20, 2007
  5. David Bilsby
    Replies:
    5
    Views:
    2,039
    David Bilsby
    Oct 9, 2007
Loading...

Share This Page