迭代器有 .next() - 有没有办法获取上一个元素而不是下一个元素?

发布于 2024-08-31 01:42:53 字数 201 浏览 1 评论 0 原文

我有一个在 HashMap 上使用的迭代器,我保存并加载该迭代器。 有没有办法用 Iterator 获取 HashMap 中的前一个键? (java.util.Iterator)

更新

我将其保存为 Red5 连接中的属性,然后将其加载回来以继续在我停止的地方工作。

另一个更新

我正在迭代 HashMap 的键集的

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

挥剑断情 2024-09-07 01:42:53

您可以使用ListIterator 代替Iterator
ListIterator 有 previous()hasPrevious() 方法。

You can use ListIterator instead of Iterator.
ListIterator has previous() and hasPrevious() methods.

梦萦几度 2024-09-07 01:42:53

正如其他人指出的那样,不是直接的,但是如果您需要访问前一个元素,您可以轻松地将其保存在一个单独的变量中。

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;
}

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;
}
我早已燃尽 2024-09-07 01:42:53

听起来您希望数组语义更类似于 ListIterator 而不是 Iterator 接口提供的那些。获取此类内容的最简单方法可能是构造一个列表(从键集 (LinkedList keyList = new LinkedList(map.keySet())),然后使用手动使用 ListIterator 而不是常规 Iterator 或 foreach

对于需要记住连续项的非常简单的情况,处理此问题的最简单方法是将先前的 Key 存储在局部变量中并在循环结束时更新它。

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.

帅的被狗咬 2024-09-07 01:42:53

不,Iterator 仅定义了 3 个方法:

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

您当然可以实现自己的迭代器。

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

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

You can of course implement your own iterator.

初见 2024-09-07 01:42:53

正如其他人所说,您只能使用 next() 访问元素。然而,这有点是一个术语问题。一旦你调用next(),这个就是当前元素。

除非问题是您需要在每次迭代中查看集合中的两个连续项目,在这种情况下,简单的变量似乎最简单。

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.

咋地 2024-09-07 01:42:53

尽管 Set 没有提供反向迭代器的方法,但 Deque 可以。您可以使用 descendingIterator( ) 对于按反向顺序的迭代器和iterator(),用于按向前顺序的迭代器。

(您可以通过 Deque deque = new LinkedList(set)Set 创建一个 Deque,其中 set 是您的 Set ,而 T 是您正在使用的通用类型。)

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.)

酒解孤独 2024-09-07 01:42:53

最终,迭代器并不完全适合您的任务。

为什么不从您的 Set 创建一个 List(例如,通过 List list = new LinkedList(set))并使用标准索引进行迭代for循环?这样您就知道前一个元素位于 i - 1 处。

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.

爱你不解释 2024-09-07 01:42:53

使用迭代器,不,您没有选择获取先前的键值。它只有 hasNext() 和 next() 方法。

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

心清如水 2024-09-07 01:42:53

不,你不能。 Iterator 接口没有方法获取前一个元素。

但是你可以做的是 - 有点垃圾 - 创建一个 List> 其中 Integer 值表示的哈希码关键对象。然后你可以这样做:

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
}

我知道这很垃圾,但这是可能的

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

贱人配狗天长地久 2024-09-07 01:42:53

制作你自己的迭代器:

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);
    }
}

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文