Detailed explanation of the use of iterators in Java

Detailed explanation of the use of iterators in Java

preface:

The iterator pattern encapsulates a collection, mainly providing a way for users to traverse its internal elements. Iterator mode has two advantages: ① it provides users with a way to traverse without exposing its internal implementation details; ② The responsibility of wandering between elements is handed over to the iterator rather than the aggregate object, which realizes the decoupling between the user and the aggregate object.

The iterator mode mainly manages an aggregate object through the iterator interface. When users use it, they only need to get an iterator type object to complete the traversal of the aggregate object. Aggregate objects here generally refer to ArrayList, LinkedList and objects with the same or similar characteristics implemented as arrays. The traversal of the aggregate object through the iterator mode is mainly carried out through the next() hasnext() method of the iterator interface, where the next() method will return the element value of the current traversal point, and the hasnext() method represents whether there are elements after the current traversal point. There is also a remove () method in the iterator interface, which will remove the elements of the current traversal point. In general, this method is not required. This method can be called in some special cases. If the traversal of the current aggregate object does not support this operation, an unsupported operationexception can be run in this method.

Here we illustrate the iterator pattern with the following example. There are two sets of menus in two restaurants. One set of menus is implemented by array, and the other set of menus is implemented by ArrayList. Now, due to the merger of the two restaurants, the two sets of menus need to be integrated. Since the chefs of both sides have been used to their own menu assembly methods, they all hope to continue to maintain their own menu styles. However, for waiters, when providing menus to customers, they must deal with them in two different ways according to the two sets of menus, which will inevitably increase the work difficulty of waiters. Moreover, if new restaurants are merged later, such as HashMap, the waiters will maintain this set of menus, which is not conducive to expansion. According to the needs of the waiter, what he needs is a menu list. If it faces different menu classes, it is bound to increase its work difficulty, and the methods provided in different menu classes are not necessarily required by the waiter. Therefore, according to the needs of the waiter, a menu specification needs to be formulated here, So that the waiter can traverse it in the same way. The iterator mode can be used here. The waiter only needs to traverse the iterator interface, and the menus owned by each chef only need to implement the iterator, and they can still maintain their menu items in their own way. In this way, the decoupling of different menus and waiters is realized. The following is the specific code to solve this problem using iterator mode.

Menu interface (mainly including the method of creating iterators):

Menu item:

Menu class (assembly method of menu items):

Iterator interface:

Iterator class:

Waiter:

It can be seen from the above code that the waiter does not program specific menus, but relies on a menu interface that creates a menu iterator and an iterator interface. The waiter does not need to know what kind of assembly menu is passed, but only needs to traverse the menu objects that implement the two interfaces, This achieves the purpose of realizing the interface by relying on polymorphism and the interface by relying on the interface unchanged, so as to realize the separation of waiter and menu assembly mode.

The iterator pattern can be seen everywhere in the collection of Java class libraries. The menu used here is equivalent to the iteratable interface in the Java class library. Its function is to create an iterator object, and the iterator interface is basically the same as the iterator interface of the Java class library. It should be noted here that actually, making a class implement the iterator pattern increases the maintenance burden of a class while adding functions to the class, because the basic method of a class is highly cohesive. The so-called cohesion is to realize a set of relevant and complete functions, and the iterator interface is actually a set of complete and relevant functions, Therefore, making a class implement the iterator pattern implicitly adds two sets of less "cohesive" functions to the class, which will lead to the need to take both sides into account when maintaining the functions of the class. ArrayList and LinkedList are embodied in the Java class library. They not only provide all the basic remove methods of the list, but also provide the remove methods to be implemented by the iterator. In order to achieve the unity of the two, they have to make some conventions. For example, when traversing the collection, they cannot call the basic remove or add methods of the class that will change the structure of the class.

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>