How does Java delete elements during list or map traversal

There are many ways to traverse and delete the elements in the list or map, which will cause problems when used improperly. Let's learn more through this article.

1、 Delete elements during list traversal

Using index subscript traversal

Example: delete 2 from the list

Output results:

Question:

The results show that only one 2 is deleted and the other 2 is omitted. The reason is that after deleting the first 2, the number of elements in the set is reduced by 1, and the subsequent elements are moved forward by 1 bit, resulting in the omission of the second 2.

Using the for loop traversal

Example:

result:

explain:

Description of concurrentmodificationexception in JDK:

public class ConcurrentModificationException extends

RuntimeException this exception is thrown when the method detects concurrent modification of the object but does not allow such modification.

For example, when a thread iterates over a collection, another thread is usually not allowed to modify the collection linearly. Usually in these cases, the result of iteration is uncertain. If this behavior is detected, some iterator implementations (including all common collection implementations provided by JRE) may choose to throw this exception. The iterator performing this operation is called a fast failure iterator, because the iterator will soon fail completely without risking uncertain behavior at any time in the future.

Note: this exception does not always indicate that the object has been modified concurrently by different threads. If a single thread issues a method call sequence that violates an object contract, the object may throw this exception. For example, if a thread modifies a collection directly when iterating over it using a fast failure iterator, the iterator will throw this exception.

Note: the fast failure behavior of iterators cannot be guaranteed, because generally speaking, it is impossible to make any hard guarantee whether asynchronous concurrent modifications occur. A quick failure operation throws a concurrentmodificationexception as best it can. Therefore, it is wrong to write a program that depends on this exception to improve the correctness of such operations. The correct approach is: concurrent modificationexception should only be used to detect bugs.

For each in Java is actually handled by iterator. The iterator does not allow the collection to be deleted during the use of the iterator. This causes the iterator to throw a concurrentmodificationexception.

The right way

Example:

result:

2、 Deleting elements during map traversal

Examples of the right approach:

result:

be careful

However, for the remove () method of the iterator, there are also some points that we should pay attention to:

Every time the iterator is called The remove () method can only be called once for the next () method.

The next () method must be called once before the remove () method is called.

Description of the remove() method in jdk-api:

Void remove() removes the last element returned by the iterator from the collection pointed to by the iterator (optional operation). This method can only be called once per call to next. If the collection pointed to by the iterator is modified in a way other than calling this method during iteration, the behavior of the iterator is ambiguous.

Throw: Unsupported operationexception - if the iterator does not support the remove operation. IllegalStateException - if the next method has not been called, or if the remove method has been called since the next method was last called.

summary

The above is all about deleting elements during the traversal of list and map. I hope the content of this article can bring some help to your study or work. If you have any questions, you can leave a message.

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
分享
二维码
< <上一篇
下一篇>>