iterator的基本用法

 1.iterator输出

package com.iterator.demo;

import java.util.Iterator;
import java.util.Set;

public class IteratorDemo {
    public static void main(String[] args) {
        Set<String> all = Set.of("Hello", "world","sina","sohu");
        Iterator<String> iter = all.iterator();
        while (iter.hasNext()) {
            String string = iter.next();
            System.out.println(string);
        }
    }
}

 运行结果:

world
hello
sina
sohu

2.remove()删除当前元素

package com.iterator.demo;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class IteratorDemo {
    public static void main(String[] args) {
        Set<String> all = new HashSet<String>();
        all.add("hello");
        all.add("world");
        all.add("sina");
        all.add("sohu");
        Iterator<String> iter = all.iterator();
        while (iter.hasNext()) {
            String string = iter.next();
            System.out.println(string);
            if("world".equals(string)) {
                iter.remove();//删除当前的数据
            }
        }
        System.out.println(all);
    }
}

运行结果:

world
hello
sina
sohu
[world, hello, sina, sohu]

3.如果使用Collection中的remove()方法,则会报并发修改异常。

package com.iterator.demo;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class IteratorDemo {
    public static void main(String[] args) {
        Set<String> all = new HashSet<String>();
        all.add("hello");
        all.add("world");
        all.add("sina");
        all.add("sohu");
        Iterator<String> iter = all.iterator();
        while (iter.hasNext()) {
            String string = iter.next();
            System.out.println(string);
            all.remove("world");//删除当前的数据
        }
        System.out.println(all);
    }
}

运行结果:

world
Exception in thread "main" java.util.ConcurrentModificationException
    at java.base/java.util.HashMap$HashIterator.nextNode(HashMap.java:1490)
    at java.base/java.util.HashMap$KeyIterator.next(HashMap.java:1513)
    at com.iterator.demo.IteratorDemo.main(IteratorDemo.java:16)

所以,必须使用iterator提供的remove()方法来删除,但是不是必须的情况下,我们很少使用iterator的remove()方法。

4.ListIterator双向迭代

  如果想实现由后向前的遍历,那么首先要实现由前向后遍历。

package com.iterator.demo;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

public class IteratorDemo {
    public static void main(String[] args) {
        List<String> all = new ArrayList<String>();
        all.add("hello");
        all.add("world");
        all.add("sina");
        all.add("sohu");
        ListIterator<String> iter = all.listIterator();
        System.out.print("由前向后遍历:");
        while (iter.hasNext()) {
            System.out.print(iter.next()+"、");
        }
        System.out.print("
由后向前遍历:");
        while (iter.hasPrevious()) {
            System.out.print(iter.previous()+"、");
        }
    }
}

运行结果:

由前向后遍历:hello、world、sina、sohu、
由后向前遍历:sohu、sina、world、hello、
原文地址:https://www.cnblogs.com/sunzhongyu008/p/11230101.html