Collection (Collection) traversal iteration





foreword

In the process of development, it is often necessary to traverse all the elements in the collection. For this requirement, JDK provides an interface java.util.Iterator.

If you want to traverse the Collection collection, you need to get the collectioniteratorComplete the iterative operation.

  • Iterators apply to all single-column collections


What is iteration?

Iteration: That is, the general way to obtain the elements of the Collection collection. Before obtaining the elements, it is necessary to judge whether there are elements in the collection. If there are, take out the element and continue to judge. is taken out. This way of taking out is called iteration.

Iteration (iteration) repeats the activity of the feedback process, and the result of each iteration will be used as the initial value of the next iteration. (A repeatedly calls B). Iteration is also a ring structure. Starting from the initial state, each iteration traverses the ring and updates the state, and iterates multiple times until reaching the end state.




What is traversal? Why is traversal required in development?

Traversal is to visit the elements in the container one by one.

In development, it is often necessary to count the sum of elements, find the most value, find out a certain data and then kill it, etc. Business needs to traverse.


There are three ways to traverse the Collection collection:
(1) iterator;
(2) foreach (enhanced for loop);
(3) Lambda expression.












Note:
Likes, comments, and reprints are welcome. Please give the link to the original text in an obvious place on the article page
. Those who know, thank you for reading my article in the vast crowd.
Where is the signature without personality!
For details, please follow me
and continue to update

Scan to have a surprise!
© 2021 01 - Guyu.com | 【Copyright all infringements must be investigated】

Guess you like

Origin blog.csdn.net/weixin_49770443/article/details/112902473