📜  Java的枚举 vs 迭代器 vs ListIterator

📅  最后修改于: 2021-09-13 03:01:34             🧑  作者: Mango

枚举是一个接口。在Java中的集合框架中使用它来一一检索元素。枚举是一个遗留接口,仅适用于 Vector、HashTable、Stack 等遗留类。它提供单向迭代。通过枚举,我们只能执行读操作,不能执行移除操作。

枚举对象可以通过调用 Vector 类中的 elements() 方法来创建。

// Here "v" is a vector object. enum is of
// type Enumeration interface and refers to "v"
Enumeration enum = v.elements();

迭代器是可以应用于任何集合对象的通用游标。它提供单方向迭代。通过使用迭代器,我们可以执行读取和删除操作,但不能执行替换操作。每当我们想要枚举所有集合框架实现的接口(如 Set、List、Queue、DeQue 以及 Map 接口的实现类)中的元素时,都必须使用迭代器。

可以通过调用 Collection 接口中的 iterator() 方法来创建迭代器对象

// Here "c" is any collection object. itr is of
// type Iterator interface and refers to "c"
Iterator itr = c.iterator();

ListIterator是所有三个游标中功能最强大的游标。 ListIterator 仅适用于列表实现的类,如 ArrayList、LinkedList、Stack 等。 ListIterator 向前和向后遍历。通过使用 ListIteartor,我们可以执行读取、删除和替换操作。当我们想要枚举列表的元素时,必须使用 ListIterator。

ListIterator 对象可以通过调用列表接口中的 listIterator() 方法来创建。

// Here "l" is any list object. ltr is of
// type ListIterator interface and refers to "l"
ListIterator ltr = l.listIterator();
Java
// A Java program to demonstrates the 
// difference between Enumeration,
// Iterator, and ListIterator
  
import java.io.*;
import java.util.*;
  
class GFG {
      
    public static void main(String args[])
    {
          
        // Creating a vector object
        Vector v = new Vector();
          
        // Adding elements to the vector object
        v.add(10);
        v.add(20);
        v.add(30);
        v.add(40);
        v.add(50);
          
        System.out.println("Enumeration: ");
          
        // Creating an Enumeration object
        Enumeration e = v.elements();
          
        // Checking the next element availability
        while (e.hasMoreElements()) {
              
            // Moving cursor to the next element
            int i = (Integer)e.nextElement();
              
            // Printing the element
            System.out.print(i + " ");
        }
        System.out.println();
        System.out.println();
          
        System.out.println("Iterator: ");
          
        // Creating Iterator object
        Iterator itr = v.iterator();
          
        // Checking the next element availability
        while (itr.hasNext()) {
              
            // Moving cursor to the next element
            int i = (Integer)itr.next();
              
            // Checking if i == 10 then
            // remove the element
            if (i == 10)
                itr.remove();
        }
        System.out.println(v);
        System.out.println();
          
        System.out.println("ListIterator: ");
          
        // Creating ListIterator object
        ListIterator ltr = v.listIterator();
          
        // Checking the next element availability
        while (ltr.hasNext()) {
              
            // Moving cursor to the next element
            int i = (Integer)ltr.next();
              
            // Performing add, remove, and 
            // replace operation
            if (i == 20)
                ltr.remove();
              
            else if (i == 30)
                ltr.add(60);
              
            else if (i == 40)
                ltr.set(100);
        }
          
        System.out.println(v);
    }
}


输出
Enumeration: 
10 20 30 40 50 

Iterator: 
[20, 30, 40, 50]

ListIterator: 
[30, 60, 100, 50]



表示出枚举,迭代器,和的ListIterator之间区别

Property               Enumeration                  Iterator              ListIterator
1. Where we can apply? It can be applied only to the legacy classes. It can be applied to any collection interface. It can be applied to the only list interface.
2. Is it a legacy?   Yes (introduced in 1.0 V).  No (introduced in 1.2 V). No (introduced in 1.2 V).
3. Allowed Movement Single direction, i.e we can traverse elements present in the collection only in the forward direction. Single direction, i.e we can traverse elements present in the collection only in the forward direction. Bidirectional, i.e we can traverse elements present in the collection both in forward and backward directions.
4. Allowed  Operation We can only perform the read operation. We can perform read and remove operation. We can perform read, remove, add, and replace operations.
5. How can we get it? By calling elements() method present in the vector class. By calling iterator() method present in any collection interface. By calling listIterator() method present in the list interface.