本文共 14557 字,大约阅读时间需要 48 分钟。
java 迭代器跳出迭代
In this post we are going to discuss about some basics of Java Enumeration and in-depth discussion about Java Iterator. As Java Enumeration interface is deprecated, it is not recommended to use this in our applications.
在本文中,我们将讨论有关Java枚举的一些基础知识,以及有关Java Iterator的深入讨论。 由于不推荐使用Java Enumeration接口,因此不建议在我们的应用程序中使用它。
In this post we are going to this discuss the following concepts about Java’s Iterator.
在本文中,我们将讨论有关Java迭代器的以下概念。
First of all, I would like to define what is a Java Cursor? A Java Cursor is an Iterator, which is used to iterate or traverse or retrieve a Collection or Stream object’s elements one by one.
首先,我想定义什么是Java游标? Java游标是一个迭代器,用于迭代,遍历或检索Collection或Stream对象的元素。
Java supports the following four different cursors.
Java支持以下四个不同的游标。
Each Java cursor have some advantages and drawbacks. We will discuss some basics about Enumeration and full details about Iterator in this posts. We will discuss about ListIterator and Spliterator in my coming posts.
每个Java游标都有一些优点和缺点。 在本文中,我们将讨论有关枚举的一些基础知识和有关Iterator的完整详细信息。 我们将在我的后续文章中讨论ListIterator和Spliterator。
Java Enumeration is available since Java 1.0 and it has many limitations and not advisable to use in new projects.
Java枚举自Java 1.0开始可用,它具有许多局限性,建议不要在新项目中使用。
NOTE:- What is CRUD operations in Collection API?
注意:-什么是Collection API中的CRUD操作?
To overcome all these issues, Java come-up with new Cursors: Iterator and ListIterator in Java 1.2. It has introduced a new type of Cursor: Spliterator in Java 1.8.
为了克服所有这些问题,Java提出了新的Cursors:Java 1.2中的Iterator和ListIterator。 它引入了一种新型的Cursor:Java 1.8中的Spliterator。
We will discuss about Iterator with some suitable examples in this post.
在本文中,我们将通过一些合适的示例来讨论Iterator。
In Java, Iterator is an interface available in Collection framework in java.util package. It is a Java Cursor used to iterate a collection of objects.
在Java中,Iterator是java.util包中Collection框架中可用的接口。 它是用于迭代对象集合的Java游标。
As shown in the Class Diagram below, Java Iterator has four methods. We are already familiar with first four methods. Oracle Corp has added fourth method to this interface in Java SE 8 release.
如下面的类图所示,Java Iterator有四个方法。 我们已经熟悉前四种方法。 Oracle Corp在Java SE 8版本中已向该接口添加了第四种方法。
In this section, we will discuss about Java Iterator methods in-brief. We will explore these methods in-depth with some useful examples in the coming section.
在本节中,我们将简要讨论Java Iterator方法。 在下一节中,我们将通过一些有用的示例深入探讨这些方法。
First discuss about how to get an Iterator object from a Collection. Each and every Collection class has the following iterator() method to iterate it’s elements.
首先讨论如何从集合中获取Iterator对象。 每个Collection类都有以下iterator()方法来迭代其元素。
Iteratoriterator()
It returns an iterator over the elements available in the given Collection object.
它在给定Collection对象中可用的元素上返回一个迭代器。
Example-1:-
示例1:-
import java.util.Iterator;import java.util.LinkedList;import java.util.List;public class ExternalIteratorDemo { public static void main(String[] args) { Listnames = new LinkedList<>(); names.add("Rams"); names.add("Posa"); names.add("Chinni"); // Getting Iterator Iterator namesIterator = names.iterator(); // Traversing elements while(namesIterator.hasNext()){ System.out.println(namesIterator.next()); } }}
Example-2:-
示例2:-
import java.util.LinkedList;import java.util.List;public class InternalIteratorDemo { public static void main(String[] args) { Listnames = new LinkedList<>(); names.add("Rams"); names.add("Posa"); names.add("Chinni"); for(String name: names){ System.out.println(name); } }}
If we observe the two above examples, both examples are doing the same thing. In Example-1, we have created Iterator object externally and retrieved List object elements one by one. In Example-2, we have Not created Iterator object externally. We are using Enhanced for loop to retrieve the List object elements one by one.
如果我们观察以上两个示例,则两个示例都在做相同的事情。 在Example-1中,我们在外部创建了Iterator对象,并一个接一个地检索List对象元素。 在示例2中,我们没有在外部创建Iterator对象。 我们正在使用增强的for循环来逐一检索List对象元素。
Enhanced for loop uses Iterator object internally and do the same thing like External Iterator example. So both examples gives the same output as shown below.
增强的for循环在内部使用Iterator对象,并执行与External Iterator示例相同的操作。 因此,两个示例的输出均相同,如下所示。
Output:-
输出:-
RamsPosaChinni
In previous section, we have discussed about how Collection API has implemented the iterator() method to iterate it’s elements with or without using Enhanced For Loop.
在上一节中,我们讨论了Collection API如何实现iterator()方法以在使用或不使用Enhanced For Loop的情况下迭代其元素。
In this section, we will discuss about how to provide similar kind of functionality for a User-Defined or Custom classes. We should follow these instructions to provide this functionality:
在本节中,我们将讨论如何为用户定义或自定义类提供类似的功能。 我们应该按照以下说明提供此功能:
If we implement these instructions to our Custom class, then it’s ready to use Enhanced For Loop or external Iterator object to iterate it’s elements.
如果我们对Custom类实现这些指令,则可以使用Enhanced For Loop或外部Iterator对象来迭代其元素。
Let us develop a simple example to understand these instructions clearly.
让我们开发一个简单的示例来清楚地理解这些说明。
Example:-
例:-
public class Employee { private int empid; private String ename; private String designation; private double salary; public Employee(int empid,String ename,String designation,double salary){ this.empid = empid; this.ename = ename; this.designation = designation; this.salary = salary; } public int getEmpid() { return empid; } public String getEname() { return ename; } public String getDesignation() { return designation; } public double getSalary() { return salary; } @Override public String toString(){ return empid + "\t" + ename + "\t" + designation + "\t" + salary; } }
import java.util.*;public class Employees implements Iterable{ private Listemps = null; public Employees(){ emps = new ArrayList<&glt;(); emps.add(new Employee(1001,"Rams","Lead", 250000L)); emps.add(new Employee(1002,"Posa","Dev", 150000L)); emps.add(new Employee(1003,"Chinni","QA", 150000L)); } @Override public Iterator iterator() { return emps.iterator(); }}
public class EmployeesTester { public static void main(String[] args) { Employees emps = new Employees(); for(Employee emp : emps){ System.out.println(emp); } }}
Output:-
输出:-
1001 Rams Lead 250000.01002 Posa Dev 150000.01003 Chinni QA 150000.0
In this section, we will try to understand how Java Iterator and it’s methods works internally. Let us take the following LinkedList object to understand this functionality.
在本节中,我们将尝试了解Java Iterator及其方法在内部如何工作。 让我们采用以下LinkedList对象来了解此功能。
Listnames = new LinkedList<>();names.add("E-1");names.add("E-2");names.add("E-3");...names.add("E-n");
Now create an iterator object on List object as shown below:
现在,在List对象上创建一个迭代器对象,如下所示:
IteratornamesIterator = names.iterator();
Let us assume “namesIterator” iterator looks like below:
让我们假设“ namesIterator”迭代器如下所示:
Here Iterator’s Cursor is pointing to the before first element of the List. Now we run the following code snippet.
这里,迭代器的游标指向列表的第一个元素之前。 现在,我们运行以下代码片段。
namesIterator.hasNext(); namesIterator.next();
When we run the above code snippet, Iterator’s Cursor points to the first element in the List as shown in the above diagram. Then run the following code snippet.
当我们运行上面的代码片段时,Iterator的Cursor指向List中的第一个元素,如上图所示。 然后运行以下代码片段。
namesIterator.hasNext(); namesIterator.next();
When we run the above code snippet, Iterator’s Cursor points to the second element in the List as shown in the above diagram. Do this process to reach the Iterator’s Cursor to the end element of the List.
当我们运行上面的代码片段时,Iterator的Cursor指向List中的第二个元素,如上图所示。 执行此过程以将Iterator的Cursor到达List的末尾元素。
After reading the final element, if we run the below code snippet, it returns “false” value.
阅读完最后一个元素后,如果我们运行下面的代码片段,它将返回“ false”值。
namesIterator.hasNext();
As Iterator’s Cursor points to the after the final element of the List, hasNext() method returns false value.
当Iterator的Cursor指向List的最后一个元素之后时,hasNext()方法将返回false值。
After observing all these diagrams, we can say that Java Iterator supports only Forward Direction Iteration as shown in the below diagram. So it is also know as Uni-Directional Cursor.
观察所有这些图之后,我们可以说Java Iterator仅支持正向迭代,如下图所示。 因此也称为单向游标。
Compare to Enumeration interface, Java Iterator has the following advantages or benefits.
与Enumeration接口相比,Java Iterator具有以下优点或好处。
However, Java Iterator has the following limitations or drawbacks.
但是,Java Iterator具有以下限制或缺点。
To overcome these limitations, Java has introduced two more Cursors: ListIterator and Spliterator. We will discuss about these two cursors in my coming posts.
为了克服这些限制,Java又引入了两个游标:ListIterator和Spliterator。 我们将在我的后续文章中讨论这两个游标。
In section we will discuss about similarities between two Java Cursors: Java Enumeration and Iterator
在本节中,我们将讨论两个Java游标之间的相似之处:Java枚举和迭代器
The following table describes the differences between Java Enumeration and Iterator:
下表描述了Java枚举和迭代器之间的区别:
Enumeration | Iterator |
---|---|
Introduced in Java 1.0 | Introduced in Java 1.2 |
Legacy Interface | Not Legacy Interface |
It is used to iterate only Legacy Collection classes. | We can use it for any Collection class. |
It supports only READ operation. | It supports both READ and DELETE operations. |
It’s not Universal Cursor. | Its a Universal Cursor. |
Lengthy Method names. | Simple and easy to use method names. |
枚举 | 迭代器 |
---|---|
在Java 1.0中引入 | 在Java 1.2中引入 |
旧版界面 | 不是旧版界面 |
它仅用于迭代Legacy Collection类。 | 我们可以将其用于任何Collection类。 |
它仅支持READ操作。 | 它支持READ和DELETE操作。 |
不是通用游标。 | 它是一个通用光标。 |
冗长的方法名称。 | 简单易用的方法名称。 |
That’s all of about Iterator in Java. I hope these Java Iterator theory and examples will help you in getting started with Iterator programming.
这就是Java中的Iterator的全部内容。 我希望这些Java Iterator理论和示例将帮助您入门Iterator编程。
Thank you for reading my tutorials. Please drop me a comment if you like my tutorials or have any issues or suggestions or any type errors.
感谢您阅读我的教程。 如果您喜欢我的教程或有任何问题或建议或任何类型错误,请给我评论。
翻译自:
java 迭代器跳出迭代
转载地址:http://nqlzd.baihongyu.com/