views:

331

answers:

8

I have an Iterator that I use on a HashMap, and I save and load the iterator. is there a way to get the previous key in the HashMap with Iterator? (java.util.Iterator)

Update

I save it as an attribute in a Red5 connection and then load it back to continue working where i stopped.

Another update

I'm iterating through the keyset of the HashMap

A: 

using iterator, No you dont have an option to get a previous key value. it has only hasNext() and next() methods.

GK
+3  A: 

No, an Iterator<E> defines only 3 methods:

boolean hasNext()
E next()
void remove() 

You can of course implement your own iterator.

Bart Kiers
+3  A: 

Not directly, as others pointed out, but if you e.g. need to access one previous element you could easily save that in a separate variable.

T previous = null;
for (Iterator<T> i = map.keySet().iterator(); i.hasNext();) {
    T element = i.next();

    // Do something with "element" and "previous" (if not null)

    previous = element;
}
Jonik
...although I can't really think in what case this would be useful when iterating through *randomly ordered* HashMap keys
Jonik
Downvotes with a comment would be more useful.
Jonik
thanks for pointing out that the hashmap keys are randomly ordered.
ufk
@ufk: For further info, don't miss BalusC's comment on the question!
Jonik
+1  A: 

As others have said, you only access an element using next(). However it's sort of a matter of terminology. Once you call next() this is the current element.

Unless the problem is you need to see two consecutive items in the collection each iteration, in which case a simple variable would seem easiest.

John
A: 

No, you can't. The Iterator interface has no method to get the previous element.

But what you can do is - a little bit rubbish- creating a List<Entry<Integer, YourObjectType>> where the Integer-value represents the hash-code of the key-object. Then you can do something like this:

for (int i = 0; i < list.size(); i++)
{
    YourObjectType current = list.get(i).getValue();
    YourObjectType previous = (i == 0 ? null : list.get(i - 1).getValue());
    // Do whatever you want
}

I know this is very rubbish, but it is possible

Martijn Courteaux
+1  A: 

Although Set doesn't provide a method for a reverse iterator, Deque does. You can use descendingIterator() for an iterator in reverse order and iterator(), for an iterator in forwards order.

(You can create a Deque from a Set via Deque<T> deque = new LinkedList<T>(set), where set is your Set and T the generic type you're using.)

Beau Martínez
+1 for applying "know and use the libraries". Not sure if this would be the best solution to OP's root problem (whatever that is) though.
Jonik
+1  A: 

Ultimately Iterators are not fully suited for your task.

Why not create a List from your Set (via, eg, List list = new LinkedList(set)) and iterate by using a standard indexed for-loop? That way you know the previous element is at i - 1.

Beau Martínez
Beware that random-access into a LinkedList is deadly slow at large data sizes. For Linked data structures you pretty much always want to prefer an Iterator.
Ophidian
+6  A: 

It sounds like you want the array semantics more akin to a ListIterator rather than those provided by the Iterator interface. The easiest way to acquire such a thing is likely to construct a list ( from the key-set (LinkedList<K> keyList = new LinkedList<K>(map.keySet())), then use a ListIterator manually instead of a regular Iterator or foreach.

For very simple cases of needing to remember consecutive items, the simplest way to handle this is to store the previous Key in a local variable and update it at the end of the loop.

Ophidian