可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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
回答1:
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;
}
回答2:
You can use ListIterator
instead of Iterator
.
ListIterator has previous()
and hasPrevious()
methods.
回答3:
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.
回答4:
No, an Iterator<E>
defines only 3 methods:
boolean hasNext()
E next()
void remove()
You can of course implement your own iterator.
回答5:
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.
回答6:
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.)
回答7:
Ultimately Iterator
s 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
.
回答8:
using iterator, No you dont have an option to get a previous key value. it has only hasNext() and next() methods.
回答9:
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
回答10:
Make your own Iterator:
public class EnhancedIterator<E> implements Iterator<E>{
private List<E> list;
private int indexSelected=-1;
public EnhancedIterator(List<E> list){
this.list=list;
}
@Override
public boolean hasNext() {
return indexSelected<list.size()-1;
}
@Override
public E next() {
indexSelected++;
return current();
}
@Override
public void remove() {
list.remove(indexSelected);
}
public void remove(int i){
list.remove(i);
if(i<indexSelected){
indexSelected--;
}
}
public E previous(){
indexSelected--;
return current();
}
public E current(){
return list.get(indexSelected);
}
public E get(int i){
return list.get(i);
}
}