Use of iterator iterator in Java

Source: Internet
Author: User

The related classes under the map interface in the Java Collection class do not implement the Get () method like the related classes of the collection interface, so the Get () method is not directly used to get the data in the object in the scenario where the traversal output is to be implemented, but Java itself provides another way to traverse the data. The iterator iterator, although iterator can be used to traverse the read data, is essentially not a method, it is just a design pattern, it is an object, a "lightweight" object. Here's how iterator is used in different interfaces:

(a) 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 for them, and the following ArrayList as an example to discuss the two methods of iterator in collection:

1. Implement the traversal output with the while () loop:

  

1 New ArrayList (); 2 // the specific assignment procedure for list is omitted here 3 Iterator it = list.iterator (); 4          while (It.hasnext ()) {5            System.out.println (It.next ()); 6         }

The judgment condition in the while () It.hasnext () is used to determine if there is another element in it, and some continues to loop, and It.next () in the output statement can either make the pointer go backward, and return the current element for output.

2. Iterate through the output with a for () loop:

1 New ArrayList (); 2 // the assignment procedure for list is omitted here 3  for list.iterator (); It.hasnext ();) {4            System.out.println (It.next ()); 5 }

The use principle in the for () loop is the same as while (), where it is not too much to repeat.

However, the above is used in the general for () loop, and we can also use it with the For Each loop instead of iterator, because the for every itself is equivalent to an iterator:

1 New ArrayList (); 2 // the assignment process of list is also omitted here 3  for (Object array:list) {4           System.out.println (array); 5 }

It is important to note that the for-each is not intended to be used for adding or removing elements, which may be a little more concise if you are simply traversing the elements.

(ii) Use of the iterator in the map interface:

Use HashMap as an example to discuss two main ways of using iterators.

1. The combination with the while ()

 1  hashmap<k,v> myMap = new  Hashmap<k,v> ();  2  //  Omit the assignment procedure for MyMap  3  iterator<map.entry<k,v> it=mymap.entryset (). iterator ();  4  while   ( It.hasnext ()) { 5   System.out.println ( It.next ());  6  }  7  8  //  If you want the output to be more formatted, you can rewrite the ToString () method yourself, because the ToString method rewrite is not the focus of this article, so we will not discuss it for the moment. 

The iterator application method under the map interface is slightly different from the collection, using the EntrySet () method, and EntrySet () is used to return the entire key-value pair.

2. Combination with for ()

1 hashmap<k,v> mymap=new hashmap<k,v>(); 2 // the assignment process of omitting Mymap 3  for (iterator<map.entry<k,v>> it=mymap.entryset (). Iterator (); It.hasnext ();) {4             System.out.println (It.next ()); 5 }

With a few examples above, there is no need to continue explaining the principle.

Also here is the use of the for each instead of iterator:

1 hashmap<k,v> mymap=new hashmap<k,v>(); 2 // omitting the Mymap assignment process 3  for (Object oj:myMap.entrySet ()) {4            System.out.println (OJ); 5 }

The EntrySet () method is also used in the for each, specifically if the EntrySet method has doubts, please Baidu, this article but more description.

Conclusion: Since the Iterator class is encapsulated in the Java.util path, it is necessary to use the Iterator import java.util.Iterator First, or import java.util.*;

I am actually only a beginner, there are mistakes in the place to welcome correct.

Use of iterator iterator in Java

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.