Iterator and AVAIterator in AVA

Source: Internet
Author: User
Tags concurrentmodificationexception

Iterator and AVAIterator in AVA

Iterator pattern (Iterator pattern) I. Introduction iteration is a perfect concept for Java beginners. We often use the iterative interface provided by JDK to traverse java collections: Iterator it = list. iterator (); while (it. hasNext () {// using "it. next (); "do some businesss logic}. This is a good example of the application of the iterator mode. Ii. Definition and structure Iterator (Iterator) mode, also called Cursor mode. GOF defines a method to access each element in a container object without exposing the internal details of the object. From the definition, we can see that the iterator mode is generated for containers. Obviously, accessing container objects involves Traversal Algorithms. You can plug the Traversal method into the container object in one brain; or you don't provide any Traversal Algorithms at all, so that the container users can implement them themselves. Both cases seem to be able to solve the problem. However, in the previous case, the container is subject to too many functions. It is not only responsible for maintaining the elements (adding, deleting, and so on) in its own "container", but also provides interfaces for Traversing itself; in addition, you cannot traverse the same container object at the same time because the traversal status is saved. The second method saves trouble, but exposes the internal details of the container. The appearance of the iterator mode solves the disadvantages of the above two situations. Let's take a look at the real face of the iterator mode. The Iterator mode consists of the following roles: 1) Iterator role (Iterator): The Iterator role defines interfaces for accessing and traversing elements. 2) Concrete Iterator: The specific Iterator role must implement the Iterator interface and record the current position in the traversal. 3) Container role: the Container role is responsible for providing interfaces for creating specific iterator roles. 4) Concrete Container: The Container role implements the interface for creating a specific iterator role. This specific iterator role is related to the structure of the Container. The class diagram of the iterator mode is as follows: it can be seen from the structure that the iterator mode adds the iterator role between the customer and the container. The addition of the iterator role can avoid the exposure of internal details of the container, and also make the design symbol "single responsibility principle ". Note: In the iterator mode, the specific iterator role is coupled with the specific container role-the traversal algorithm is closely related to the internal details of the container. In order to remove the customer program from the dilemma of coupling with the specific iterator role, and to avoid modifications to the customer program due to the replacement of the specific iterator role, the iterator mode abstracts the specific iterator role, making the customer program more general and reusable. This is called a multi-state iteration. 3. For example, the implementation of the iterator mode is varied because its rules are loose. Let's just take an example here. We cannot present the implementation methods one by one. For this reason, let's first list the implementation methods of the iterator mode. 1. The iterator role defines the traversal interface, but does not specify who controls the iteration. In Java collection Applications, traversal processes are controlled by client programs and called external iterators. Another implementation method is to control iterations by the iterator itself, it is called an internal iterator. The external iterator is more flexible and powerful than the internal iterator, and the internal iterator has poor availability in the java language environment. 2. In the iterator mode, there is no rule on who will implement the traversal algorithm. It seems that it should be implemented in the iterator role. Because it is easy to use different Traversal Algorithms on a container, it is also convenient to apply a traversal algorithm to different containers. But in this way, container encapsulation is destroyed. The Container role must expose its own private attributes. in java, this means that its private attributes are disclosed to other classes. Let's put it in the container role. In this way, the iterator role is shelved to only store a function that traverses the current position. However, the traversal algorithm is closely bound to a specific container. In Java Collection Applications, the specific iterator role is an internal class defined in the container role. This protects the container encapsulation. However, containers also provide traversal algorithm interfaces, allowing you to expand your iterator. Let's take a look at how the iterator in the Java Collection is implemented. // Iterator role, which only defines the traversal interface public interface Iterator {boolean hasNext (); Object next (); void remove () ;}// container role, the following uses List as an example. It is also just an interface and does not List the specific container roles. It is an ArrayList class that implements the List interface. To highlight this point, it refers to listing the content related to the iterator // specific iterator role, which is in the form of internal classes. AbstractList exists to extract the public part of each specific container role. Public abstract class extends actlist extends actcollection implements List {...... // This is the factory method for creating the specific Iterator role public iterator Iterator () {return new Itr ();} // private class Itr implements Iterator {int cursor = 0; int lastRet =-1; int expectedModCount = modCount; public boolean hasNext () {return cursor! = Size () ;}public Object next () {checkForComodification (); try {Object next = get (cursor); lastRet = cursor ++; return next ;} catch (IndexOutOfBoundsException e) {checkForComodification (); throw new NoSuchElementException () ;}} public void remove () {if (lastRet =-1) throw new IllegalStateException (); checkForComodification (); try {policactlist. this. remove (lastRet); if (lastRet <cursor) cursor --; lastRet =-1; e XpectedModCount = modCount;} catch (IndexOutOfBoundsException e) {throw new ConcurrentModificationException () ;}} final void checkForComodification () {if (modCount! = ExpectedModCount) throw new ConcurrentModificationException () ;}} for the use of iterator mode. As described in the introduction, the customer program first obtains the specific container role, and then obtains the specific iterator role through the specific container role. In this way, you can use the specific iterator role to traverse the container ...... 4. When implementing your own iterator, it is generally necessary to operate the container with the supported interfaces. In addition, we also need to pay attention to the following questions: Is it safe to use this iterator to increase or decrease container elements during iterator traversal? If a composite object exists in the container, how can the iterator support deep traversal and multiple traversal? The two problems above vary with the container roles of different structures and are worth considering. 5. The applicability is described above. We can see that the iterator mode brings the following benefits to container applications: 1) supports traversing a container role in different ways. The effects vary depending on the implementation method. 2) simplified the container interface. However, in java Collection, containers provide traversal interfaces to improve scalability. 3) the same container object can be traversed at the same time. Because the traversal status is saved in each iterator object. The applicability of the iterator mode can also be obtained: 1) access the content of a container object without exposing its internal representation. 2) supports multiple traversal of container objects. 3) provides a unified interface (multi-state iteration) for Traversing different container structures ).

How to Use Iterator in java

This... You should look at the API more...
For example
Set hashSet = new HashSet (); // such as Set.
Iterator I = hashSet. iterator ();
While (I. hasNext ){
Object obj = I. next ();
// Obj operations
}
You can also
For (Iterator I = hashSet. iterator (); I. hasNext ;){
Object obj = I. next ();
}

What is the specific role of Iterator in JAVA?

Iterator pattern)
I. Introduction
The term "iteration" is no stranger to those familiar with Java. We often use the iterative interface provided by JDK to traverse java collections:
Iterator it = list. iterator ();
While (it. hasNext ()){
// Using "it. next ();" do some businesss logic
}
This is a good example of the application of the iterator mode.
Ii. Definition and Structure
The Iterator mode is also called the Cursor mode. GOF defines a method to access each element in a container object without exposing the internal details of the object.
From the definition, we can see that the iterator mode is generated for containers. Obviously, accessing container objects involves Traversal Algorithms. You can plug the Traversal method into the container object in one brain; or you don't provide any Traversal Algorithms at all, so that the container users can implement them themselves. Both cases seem to be able to solve the problem.
However, in the previous case, the container is subject to too many functions. It is not only responsible for maintaining the elements (adding, deleting, and so on) in its own "container", but also provides interfaces for Traversing itself; in addition, you cannot traverse the same container object at the same time because the traversal status is saved. The second method saves trouble, but exposes the internal details of the container.
The appearance of the iterator mode solves the disadvantages of the above two situations. Let's take a look at the real face of the iterator mode.
The iterator mode consists of the following roles:
1) Iterator role (Iterator): The Iterator role defines interfaces for accessing and traversing elements.
2) Concrete Iterator: The specific Iterator role must implement the Iterator interface and record the current position in the traversal.
3) Container role: the Container role is responsible for providing interfaces for creating specific iterator roles.
4) Concrete Container: The Container role implements the interface for creating a specific iterator role. This specific iterator role is related to the structure of the Container.
The class diagram of the iterator mode is as follows:

It can be seen from the structure that the iterator mode adds the iterator role between the customer and the container. The addition of the iterator role can avoid the exposure of internal details of the container, and also make the design symbol "single responsibility principle ".
Note: In the iterator mode, the specific iterator role is coupled with the specific container role-the traversal algorithm is closely related to the internal details of the container. In order to remove the customer program from the dilemma of coupling with the specific iterator role, and to avoid modifications to the customer program due to the replacement of the specific iterator role, the iterator mode abstracts the specific iterator role, making the customer program more general and reusable. This is called a multi-state iteration.
Iii. Example
Since the iterator mode itself is relatively loose, the specific implementation is varied. Let's just take an example here. We cannot present the implementation methods one by one. For this reason, let's first list the implementation methods of the iterator mode.
1. The iterator role defines the traversal interface, but does not specify who controls the iteration. In Java collection Applications, traversal processes are controlled by client programs and called external iterators. Another implementation method is to control iterations by the iterator itself, it is called an internal iterator. The external iterator is more flexible and powerful than the internal iterator, and the internal iterator has poor availability in the java language environment.
2. In the iterator mode, there is no rule on who will implement the traversal algorithm. It seems that it should be implemented in the iterator role. Because it is easy to use different Traversal Algorithms on a container, it is also convenient to apply a traversal algorithm to different containers. But in this way, container encapsulation is destroyed. The Container role must expose its own private attributes. in java, this means that its private attributes are disclosed to other classes.
Let's put it in the container role. In this way, the iterator role is shelved to only store a function that traverses the current position. However, the traversal algorithm is closely bound to a specific container.
In Java Collec... the remaining full text>

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.