Home > Java > javaTutorial > body text

Detailed explanation of iterator in Java

黄舟
Release: 2017-09-25 10:16:18
Original
1344 people have browsed it

Java uses iterators to provide a common operating interface for various containers. This article will share with you the usage analysis of Iterator in Java. Friends who need it can refer to it

What is an iterator

In Java, there are many data containers, and their operations have many commonalities. Java uses iterators to provide a common operation interface for various containers. This isolates the traversal operation of the container from its specific underlying implementation, achieving a decoupling effect.

Three methods are defined in the Iterator interface:

The related classes under the Map interface in the Java collection class are not the same as the related classes of the Collection interface Implement the get() method, so in scenarios where traversal output is to be achieved, the get() method cannot be used directly to obtain the data in the object, but Java itself provides another method of traversing data, that is, using the Iterator. Although Iterator can be used to traverse and read data, but it is not a method essentially. It is just a design pattern. It is an object, a "lightweight" object. The following describes how to use Iterator in different interfaces:

(1) The use of Iterator in the Collection interface.

Although the related classes of the Collection interface implement the get() method, it is still appropriate to use Iterator on them. Let’s take ArrayList as an example to discuss the two functions of Iterator in Collection. How to use:

 1. Cooperate with while() loop to achieve traversal output:


 ArrayList list = new ArrayList();
 //此处省略list的具体赋值过程
 Iterator it = list.iterator();
     while(it.hasNext()){
       System.out.println(it.next());
     }
Copy after login

Judgment conditions in while() it.hasNext() is used to determine whether there is another element in it. If so, continue the loop. it.next() in the output statement can not only move the "pointer" back one position, but also move the current element Element returned for output.

 2. Cooperate with the for() loop to achieve traversal output:


 ArrayList list = new ArrayList();
 //此处省略list的赋值过程
 for(Iterator it = list.iterator();it.hasNext();){
       System.out.println(it.next());
 }
Copy after login

The principle of use in the for() loop is the same as that of while( ) are the same, so I won’t go into details here.

However, the above is used in a general for() loop. We can also use it with the for each loop to replace Iterator, because for each itself is equivalent to an iterator:


 ArrayList list = new ArrayList();
 //此处同样省略list的赋值过程
 for(Object array:list){
      System.out.println(array);
 }
Copy after login

It should be noted that for each is not suitable for adding or deleting elements. If it is simply used to traverse elements, this writing method may be more concise.

(2) The use of Iterator in the Map interface:

The following uses HashMap as an example to discuss the two main methods of using iterators. .

1. Combination with while()


 HashMap<K,V> myMap = new HashMap<K,V>();
 //省略myMap的的赋值过程
 Iterator<Map.Entry<K,V> it=myMap.entrySet().iterator();
 while(it.hasNext()){
     System.out.println(it.next());
 }
Copy after login

//If you want the output to be more formatted, you can rewrite it yourself toString() method, since the rewriting of toString method is not the focus of this article, we will not discuss it for now.

The iterator application method under the Map interface is slightly different from that in Collection. The entrySet() method is used. entrySet() is used to return the entire key-value pair.

2. Combination with for()


 HashMap<K,V> myMap=new HashMap<K,V>();
 //省略myMap的赋值过程
 for(Iterator<Map.Entry<K,V>> it=myMap.entrySet().iterator();it.hasNext();){
       System.out.println(it.next());
 }
Copy after login

There are several examples above, so there is no need to continue to explain the principle here. .

Similarly, the usage of for each instead of Iterator is posted here:


HashMap<K,V> myMap=new HashMap<K,V>();
 //省略myMap赋值过程
 for(Object oj:myMap.entrySet()){
       System.out.println(oj);
 }
Copy after login

The entrySet() method is also used in for each, specifically if the entrySet If you have any questions about the method, please search it yourself. This article will not describe it in detail.

Conclusion: Since the Iterator class is encapsulated in the java.util path, to use Iterator you need to import java.util.Iterator; or import java.util.*;

Summarize

The above is the detailed content of Detailed explanation of iterator in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!