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.
在枚舉的情況下,我們將使用兩種方法檢查現有元素并迭代下一個元素。
- boolean hasMoreElements()
- 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 instanceVector v = new Vector();// add few elements by using addElement() of Vector classv.addElement(10);v.addElement(20);v.addElement(30);v.addElement(40);v.addElement(50);// print the vector listSystem.out.println(v);// creating an object of Enumeration Enumeration en = v.elements();// loop to check existing more elementswhile (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_methodIterator 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而言,我們將使用兩種方法來檢查現有元素并迭代下一個元素。
- boolean hasNext()
- 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 objectSet set = new HashSet();// By using add() method to add few elements in setset.add("Java");set.add("Python");set.add("C++");set.add("Ruby");set.add("C");// Creating an object of Iterator interfaceIterator itr = set.iterator();// loop for traversing an elementwhile (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 枚舉迭代
總結
以上是生活随笔為你收集整理的java 枚举迭代_Java中的枚举和迭代器之间的区别的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java ByteArrayInputS
- 下一篇: SpringBoot 过滤器、拦截器、监