java 枚举迭代_Java中的枚举和迭代器之间的区别

java 枚举迭代

Java中的枚举与迭代器 (Enumeration vs Iterator in Java)

Here, we will see how Enumeration differs from Iterator?

在这里,我们将看到Enumeration与Iterator有何不同?

1)枚举 (1) Enumeration)

  • Enumeration is an interface which is introduced in java.

    枚举是Java中引入的接口。

  • While iterating an element by Enumeration we can traverse only legacy elements so here we will see the concept of legacy

    在通过枚举迭代元素时,我们只能遍历旧元素,因此在这里我们将看到旧概念

    Legacy: Previous or Earlier versions of java define various classes and one interface to store objects and collection framework didn't include at the time so when collection framework came so these classes are re-engineered to support collection framework.

    旧版: Java的早期或早期版本定义了各种类,并且一个用于存储对象的接口和当时不包含收集框架的接口,因此当收集框架出现时,这些类就被重新设计以支持收集框架。

  • We can create an Enumeration object by using elements() method of Enumeration interface.

    我们可以使用Enumeration接口的elements()方法创建一个Enumeration对象。

    Syntax:

    句法:

    Enumeration en = collection_object.elements();
    
  • While iterating an object or an element by Enumeration then we can perform only read operation.

    当通过Enumeration迭代对象或元素时,我们只能执行读取操作。

  • Enumeration is faster than Iterator.

    枚举比Iterator快。

  • In the case of Enumeration, we will use two methods to check the existing element and iterating the next element.

    在枚举的情况下,我们将使用两种方法检查现有元素并迭代下一个元素。

    1. boolean hasMoreElements()
    2. Object nextElement()
  • Enumeration concept is applicable only for legacy classes so it does not support few collections that's why it is not a universal interface.

    枚举概念仅适用于遗留类,因此它不支持少量集合,这就是为什么它不是通用接口的原因。

  • By using Enumeration we can get only ReadAccess and we can't perform any remove operation.

    通过使用枚举,我们只能获取ReadAccess,而不能执行任何删除操作。

Example:

例:

import java.util.*;

class EnumerationClass {
    public static void main(String[] args) {
        // creating a Vector instance
        Vector v = new Vector();

        // add few elements by using addElement() of Vector class
        v.addElement(10);
        v.addElement(20);
        v.addElement(30);
        v.addElement(40);
        v.addElement(50);

        // print the vector list
        System.out.println(v);

        // creating an object of Enumeration 
        Enumeration en = v.elements();

        // loop to check existing more elements
        while (en.hasMoreElements()) {
            Integer in = (Integer) en.nextElement();
        }
        System.out.println(v);
    }
}

Output

输出量

E:\Programs>javac EnumerationClass.java
Note: EnumerationClass.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

E:\Programs>java EnumerationClass
[10, 20, 30, 40, 50]
[10, 20, 30, 40, 50] 

2)迭代器 (2) Iterator)

  • Iterator is an interface which is introduced in java.

    Iterator是Java中引入的接口。

  • While iterating an element by Iterator we can perform read and remove operation and we can't perform any other operation like add an object, replace an object.

    在使用Iterator迭代元素时,我们可以执行读取和删除操作,而不能执行任何其他操作,例如添加对象,替换对象。

  • We can create an Iterator object by using iterator() method of Iterator interface.

    我们可以使用Iterator接口的iterator()方法创建一个Iterator对象。

    Syntax:

    句法:

    Interface_name object_name = Collection_class.Iterator_method
    
  • Iterator is slower than Enumeration.

    迭代器比枚举慢。

  • In the case of Iterator, we will use two methods to check the existing element and iterating the next element.

    就Iterator而言,我们将使用两种方法来检查现有元素并迭代下一个元素。

    1. boolean hasNext()
    2. Object next()
  • Iterator concept is not applicable only for legacy classes but also support for non-legacy classes so that's why it is a universal interface.

    迭代器概念不仅适用于遗留类,而且还支持非遗留类,因此这就是通用接口的原因。

Example:

例:

import java.util.*; 

class IteratorClass {
    public static void main(String[] args) {
        // Creating a Set interface object
        Set set = new HashSet();

        // By using add() method to add few elements in set
        set.add("Java");
        set.add("Python");
        set.add("C++");
        set.add("Ruby");
        set.add("C");

        // Creating an object of Iterator interface
        Iterator itr = set.iterator();

        // loop for traversing an element
        while (itr.hasNext()) {
            String str = (String) itr.next();
            if (str.equals("C"))
                itr.remove();
        }
        // Display elements of Set interface 
        System.out.println("The final list of Iterator :" + set);
    }
}

Output

输出量

E:\Programs>javac IteratorClass.java
Note: IteratorClass.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

E:\Programs>java IteratorClass
The final list of Iterator :[Ruby, Python, C++, Java]


翻译自: https://www.includehelp.com/java/differences-between-enumeration-and-iterator-in-java.aspx

java 枚举迭代

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值