In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to implement the iterator pattern in Java. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.
Iterator pattern definition
The iterator pattern (Iterator) provides a way to access elements in an aggregated object sequentially without exposing the internal representation of the object.
The role composition of iterator pattern
(1) Iterator: define the method needed to traverse an element. Generally speaking, there are three methods: the method next () to get the next element, the method hasNext () to determine whether the traversal ends, and the method remove () to remove the current object.
(2) specific iterator role (Concrete Iterator): implement the methods defined in the iterator interface and complete the iteration of the set.
(3) Container role (Aggregate): generally an interface that provides an iterator () method, such as Collection interface, List interface, Set interface in java, etc.
(4) concrete container role (ConcreteAggregate): the concrete implementation class of the abstract container, such as the ordered list implementation of List interface, the linked list implementation of ArrayList,List interface, the hash list implementation of LinkList,Set interface, HashSet, and so on.
The scene and significance of the Application of iterator pattern
(1) access the contents of an aggregate object without exposing its internal representation
(2) support multiple traverses of aggregate objects
(3) provide a unified interface for traversing different aggregation structures.
The relationship between the four roles of the iterator pattern can be represented by a class diagram.
Specific code implementation:
Define the iterator role (Iterator)
Public interface Iterator {
Public boolean hasNext ()
Public Object next ()
}
Define specific iterator roles (Concrete Iterator)
Package patten.design
Import patten.design.List
Public class ConcreteIterator implements Iterator {
Private List list = null
Private int index
Public ConcreteIterator (List list) {
Super ()
This.list = list
}
@ Override
Public boolean hasNext () {
If (index > = list.getSize ()) {
Return false
} else {
Return true
}
}
@ Override
Public Object next () {
Object object = list.get (index)
Index++
Return object
}
}
Define the container role (Aggregate)
Package patten.design
/ / define what the collection can do
Public interface List {
Public void add (Object obj)
Public Object get (int index)
Public Iterator iterator ()
Public int getSize ()
}
Define specific container roles (ConcreteAggregate)
Package patten.design
Public class ConcreteAggregate implements List {
Private Object [] list
Private int size=0
Private int index=0
Public ConcreteAggregate () {
Index=0
Size=0
List=new Object [100]
}
@ Override
Public void add (Object obj) {
List [index++] = obj
Size++
}
@ Override
Public Iterator iterator () {
Return new ConcreteIterator (this)
}
@ Override
Public Object get (int index) {
Return list [index]
}
@ Override
Public int getSize () {
Return size
}
}
Code testing
Package patten.design
Public class IteratorTest {
/ * *
* @ param args
, /
Public static void main (String [] args) {
List list=new ConcreteAggregate ()
List.add ("a")
List.add ("b")
List.add ("c")
List.add ("d")
Iterator it=list.iterator ()
While (it.hasNext ()) {
System.out.println (it.next ())
}
}
}
Advantages and disadvantages of the iterator pattern:
The advantages of the iterator pattern are:
Simplified traversal way, for the traversal of the collection of objects, or more troublesome, for arrays or ordered lists, we can still use cursors to get, but the user needs to understand the collection very clearly, traversing the object, but for the hash table, the user traversal is more troublesome. With the introduction of the iterator method, it is much easier for users to use.
Can provide a variety of traversal methods, such as ordered list, we can provide positive order traversal, reverse order traversal two kinds of iterators, users only need to get our good iterator, you can easily traverse the collection.
The encapsulation is good, users only need to get the iterator to traverse, but do not care about the traversal algorithm.
Disadvantages of the iterator pattern:
For relatively simple traversing (such as arrays or ordered lists), traversing using iterators is more tedious, and everyone may have a feeling that, like ArrayList, we prefer to use for loops and get methods to traverse collections.
Generally speaking, the iterator pattern is symbiotic with collections. Generally speaking, as long as we implement a collection, we need to provide iterators for that collection at the same time, just like Collection,List, Set, Map in java, and so on. These collections all have their own iterators. If we want to implement such a new container, of course, we also need to introduce an iterator pattern to implement an iterator for our container.
The above is how to implement the iterator pattern in Java, and the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.