Java iterator remove all elements

How to Remove Objects From ArrayList while Iterating in Java — Example Tutorial

One of the common problems many Java Programmers face is to remove elements while iterating over ArrayList in Java because the intuitive solution doesn’t work like you just cannot go through an ArrayList using a for loop and remove an element depending upon some condition. Even though java.util.ArrayList provides the remove() methods, like remove (int index) and remove (Object element) , you cannot use them to remove items while iterating over ArrayList in Java because they will throw ConcurrentModificationException if called during iteration. The right way to remove objects from ArrayList while iterating over it is by using the Iterator’s remove() method.

When you use iterator’s remove() method, ConcurrentModfiicationException is not thrown. Because it also updates the counters and variables used by the Iterator like modCount, which indicates that modification is done by the Iterator itself and not somewhere around.

In this article, I’ll show you an example of both ways and how they work in Java. You’ll also learn a little bit about java.util.ConcurrentModificationException , which is a common problem for non-concurrent collection classes like ArrayList or HashMap .

Though, if you are entirely new to Java and coming from a non-programming background, I suggest you to first go through a comprehensive Java course like The Complete Java MasterClass instead of learning from arbitrary articles.

The course provides structured learning, which is both efficient and works great for beginners. Once you know the fundamentals, you can learn any topic by reading a blog post or individual tutorial, they will make more sense by then also.

1. ArrayList remove() method Example

Now, let’s see an example of removing elements from ArrayList while looping using for() loop and ArrayList.remove() method, which is wrong, and the program will throw ConcurrentModificationExcetpion upon execution.

import java.util.ArrayList; import java.util.List; /* * Java Program to remove an element while iterating over ArrayList */ public class Main < public static void main(String[] args) throws Exception < ListString> loans = new ArrayList<>(); loans.add("personal loan"); loans.add("home loan"); loans.add("auto loan"); loans.add("credit line loan"); loans.add("mortgage loan"); loans.add("gold loan"); // printing ArrayList before removing any element System.out.println(loans); // removing element using ArrayList's remove method during iteration // This will throw ConcurrentModification for (String loan : loans) < if (loan.equals("personal loan")) < loans.remove(loan); >> // printing ArrayList after removing an element System.out.println(loans); > >

Output
Exception in thread «main» [personal loan, home loan, auto loan, credit line loan, mortgage loan, gold loan]
java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859)
at java.util.ArrayList$Itr.next(ArrayList.java:831)
at Main.main(Main.java:26)

Читайте также:  Java for loop description

Some of you may wonder that we are getting ConcurrentModificationException because we are not using the Iterator, but that’s not true, even if you use Iterator you will get java.util.ConcurrentModificationException as long as you will use ArrayList’s remove() method for removing element while iterating as shown in the following example:

Iterator itr = loans.iterator(); while (itr.hasNext()) < String loan = itr.next(); if (loan.equals("personal loan")) < loans.remove(loan); >>

Exception in thread «main» java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859)
at java.util.ArrayList$Itr.next(ArrayList.java:831)
at Main.main(Main.java:29)

In order to fix the above code, you just need to remove the loans.remove(loan) with the itr.remove() method, which is explained in the next example. Though, if you want to know more about Iterator and in general Java Collection Framework, which can feel daunting sometimes, I suggest you go through Java Fundamentals: Collections course on Pluralsight.

How to Remove Objects or Elements while Iterating over ArrayList in Java - The Right way

It’s a perfect course to both learn and master the Java Collection framework and I highly recommend you to join this course.

2. Iterator’s remove() method Example

Now, let’s try the other approach, which uses Iterator’s remove() method to remove an element from ArrayList while iterating over it.

import java . util . ArrayList ;

import java.util.Iterator; import java.util.List; /* * Java Program to remove an element while iterating over ArrayList */ public class Main < public static void main(String[] args) throws Exception < ListString> loans = new ArrayList<>(); loans.add("personal loan"); loans.add("home loan"); loans.add("auto loan"); loans.add("credit line loan"); loans.add("mortgage loan"); loans.add("gold loan"); // printing ArrayList before removing any element System.out.println(loans); // removing element using ArrayList's remove method during iteration // This will throw ConcurrentModification IteratorString> itr = loans.iterator(); while (itr.hasNext()) < String loan = itr.next(); if (loan.equals("personal loan")) < itr.remove(); >> // printing ArrayList after removing an element System.out.println(loans); > >

Output
[personal loan, home loan, auto loan, credit line loan, mortgage loan, gold loan]
[home loan, auto loan, credit line loan, mortgage loan, gold loan]

From the output, you can see that the «personal loan» element is removed from the ArrayList. The size of the ArrayList is also reduced by one, and there is no ConcurrentModficiationException in the code.

That’s all about how to remove elements while iterating over ArrayList in Java. As I have said that if you use ArrayList’s remove() method like remove(int index) or remove(Object obj) while iterating over ArrayList, then a ConcurrentModfiicationException will be thrown. You can avoid that by using Iterator’s remove() method, which removes the current object in the iteration.

  • How to remove duplicate elements from ArrayList in Java? (tutorial)
  • 10 Courses to learn Java Programming for beginners (courses)
  • How to create and initialize ArrayList in the same line? (example)
  • 10 Free Spring Boot Courses for Java developers (courses)
  • Difference between ArrayList and HashSet in Java? (answer)
  • 10 Free Courses to learn Spring Framework (courses)
  • Difference between an Array and ArrayList in Java? (answer)
  • How to reverse an ArrayList in Java? (example)
  • Top 5 Courses to learn Hibernate for Java developers (courses)
  • How to loop through an ArrayList in Java? (tutorial)
  • How to synchronize an ArrayList in Java? (read)
  • When to use ArrayList over LinkedList in Java? (answer)
  • Difference between ArrayList and HashMap in Java? (answer)
  • Difference between ArrayList and Vector in Java? (answer)
  • How to sort an ArrayList in descending order in Java? (read)
  • How to get a sublist from ArrayList in Java? (example)
  • How to convert CSV String to ArrayList in Java? (tutorial)
Читайте также:  Experimental javascript что это

P. S. — If you are new to the Java world and looking for a free online training course to learn Java then you can also check out this Java Tutorial for Complete Beginners course on Udemy. It’s completely free and you just need a Udemy account to join this online course.

Источник

Interface Iterator

This interface is a member of the Java Collections Framework.

Method Summary

Performs the given action for each remaining element until all elements have been processed or the action throws an exception.

Removes from the underlying collection the last element returned by this iterator (optional operation).

Method Details

hasNext

Returns true if the iteration has more elements. (In other words, returns true if next() would return an element rather than throwing an exception.)

next

remove

Removes from the underlying collection the last element returned by this iterator (optional operation). This method can be called only once per call to next() . The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method, unless an overriding class has specified a concurrent modification policy. The behavior of an iterator is unspecified if this method is called after a call to the forEachRemaining method.

forEachRemaining

Performs the given action for each remaining element until all elements have been processed or the action throws an exception. Actions are performed in the order of iteration, if that order is specified. Exceptions thrown by the action are relayed to the caller. The behavior of an iterator is unspecified if the action modifies the collection in any way (even by calling the remove method or other mutator methods of Iterator subtypes), unless an overriding class has specified a concurrent modification policy. Subsequent behavior of an iterator is unspecified if the action throws an exception.

 while (hasNext()) action.accept(next()); 

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Читайте также:  How to make html files

Источник

Java iterator remove all elements

Performs the given action for each remaining element until all elements have been processed or the action throws an exception.

Removes from the underlying collection the last element returned by this iterator (optional operation).

Method Detail

hasNext

Returns true if the iteration has more elements. (In other words, returns true if next() would return an element rather than throwing an exception.)

next

remove

Removes from the underlying collection the last element returned by this iterator (optional operation). This method can be called only once per call to next() . The behavior of an iterator is unspecified if the underlying collection is modified while the iteration is in progress in any way other than by calling this method.

forEachRemaining

Performs the given action for each remaining element until all elements have been processed or the action throws an exception. Actions are performed in the order of iteration, if that order is specified. Exceptions thrown by the action are relayed to the caller.

 while (hasNext()) action.accept(next()); 

Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2023, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.

Источник

Оцените статью