Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the ways to iterate over map in java

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the way of iterative map in java, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to know about it.

Iterator principle:

What are iterators and the benefits of using iterators?

An iterator is something used to traverse the objects in the collection, that is, for the collection, we do not iterate through the elements directly as we do with the original array, but iterate through the objects through the iterator. The advantage of this is that the traversal behavior for the collection type is separated from the collection object being traversed, so that there is no need to care about the specific implementation of the collection type. As long as you get the iterator of the collection object, you can traverse the objects in the collection. Details like traversing the order of objects and how to access object elements are all handled by its own iterator.

How is the iterator implemented?

The first step of the collection is to implement the iterable interface to indicate that the object can be iterated. The object that implements the iterable interface implements the iterator method, which returns an Iterator object. An iterator object requires methods in the Iterator interface: hasNext (), next (), remove (). The remove () method deletes the most recently called element, and calling remove () directly produces an error message (IllegalStateException) if remove () has not been called before next (). When we iterate over the collection object, next () returns the first object of the current object and returns, then next points to the next element, and the hasNext method just looks to see if there are any more elements behind the pointer.

The trap of the iterator?

Collections cannot be used for remove operations when using for iterations. At this point, you need to iterate with the iterator, and then delete it using the remove method in the iterator.

Why did such an error occur?

The remove () method also modifies a flag bit modCount for the number of modifications when deleting the element, and throws a ConcurrentModificationException exception if the expectedModCount of the iterator is not equal to the size of the modCount. The main purpose of modCount is to prevent other threads from modifying the current object during the current object iteration.

/ / iterable interface source code

Public interface Iterable {/ * * Returns an iterator over elements of type {@ code T}. * * @ return an Iterator. * / Iterator iterator (); default void forEach (Consumer)

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report