1. Overview
1.概述
In this tutorial, we’ll learn about Enumeration and Iterator in Java. We’ll also learn how to use them in code and what are the differences between them.
在本教程中,我们将学习Java中的枚举和Iterator。我们还将学习如何在代码中使用它们,以及它们之间有哪些区别。
2. Introduction to Enumeration and Iterator
2.介绍枚举和迭代器
In this section, we’ll learn about Enumeration and Iterator conceptually and their use.
在本节中,我们将从概念上了解枚举和迭代器以及它们的用途。
2.1. Enumeration
2.1.枚举
Enumeration has been present in Java since version 1.0. It’s an interface and any implementation allows one to access elements one by one. In simple terms, it’s used to iterate over a collection of objects such as Vector and Hashtable.
枚举从1.0版本开始就出现在Java中。它是一个接口,任何实现都允许人们逐一访问元素。简单地说,它用于遍历对象的集合,如Vector和Hashtable。
Let’s look at an example for Enumeration:
让我们看一下枚举的例子。
Vector<Person> people = new Vector<>(getPersons());
Enumeration<Person> enumeration = people.elements();
while (enumeration.hasMoreElements()) {
System.out.println("First Name = " + enumeration.nextElement().getFirstName());
}
Here, we’re printing firstName of a Person using Enumeration. The elements() method provides a reference to Enumeration and by using that we can access elements one by one.
在这里,我们正在使用Enumeration打印Person的firstName。elements() 方法提供了对Enumeration的引用,通过使用它,我们可以一个一个地访问元素。
2.2. Iterator
2.2. 迭代器
Iterator has been present in Java since 1.2 and is used to iterate over collections that were also introduced in the same version.
Iterator从1.2开始就出现在Java中,用于迭代collections,也在同一版本中被引入。
Next, let’s print firstName of a Person using Iterator. iterator() provides a reference to Iterator and by using that we can access elements one by one:
接下来,让我们使用Iterator来打印Person的firstName。iterator() 提供了一个对Iterator的引用,通过使用它,我们可以一个一个地访问元素。
List<Person> persons = getPersons();
Iterator<Person> iterator = persons.iterator();
while (iterator.hasNext()) {
System.out.println("First Name = " + iterator.next().getFirstName());
}
So, we can see Enumeration and Iterator are both present in Java since 1.0 and 1.2 respectively, and are used to iterate over a collection of objects one at a time.
因此,我们可以看到枚举和迭代器都分别从1.0和1.2版本开始出现在Java中,并被用来逐一迭代对象的集合。
3. Difference Between Enumeration and Iterator
3.枚举和迭代器之间的区别
In this table, we’ll understand the differences between Enumeration and Iterator:
在这个表格中,我们将了解枚举和迭代器的区别:。
Enumeration | Iterator |
---|---|
Present since Java 1.0 to enumerate Vectors and Hashtables | Present since Java 1.2 to iterate over Collections such as List, Set, Map, etc |
Contains two methods: hasMoreElements() and nextElement() | Contains three methods: hasNext(), next() and remove() |
Methods have long names | Methods have short and concise names |
Have no methods to remove elements while iterating | Have remove() to remove elements while iterating |
asIterator() added in Java 9 gives Iterator on the top of Enumeration. However, remove() in this particular case throws UnsupportedOperationException | forEachRemaining() added in Java 8 performs actions on the remaining elements |
4. Conclusion
4.总结
In this article, we’ve understood Enumeration and Iterator, how to use them using code examples, and the various differences between them.
在这篇文章中,我们已经了解了枚举和迭代器,如何用代码实例来使用它们,以及它们之间的各种区别。
All the code example used in this article is available over on GitHub.
本文中使用的所有代码示例都可以在GitHub上找到over。