ConcurrentModification Exception while Iterating [duplicate] - java
We all know you can't do the following because of ConcurrentModificationException:
for (Object i : l) {
if (condition(i)) {
l.remove(i);
}
}
But this apparently works sometimes, but not always. Here's some specific code:
public static void main(String[] args) {
Collection<Integer> l = new ArrayList<>();
for (int i = 0; i < 10; ++i) {
l.add(4);
l.add(5);
l.add(6);
}
for (int i : l) {
if (i == 5) {
l.remove(i);
}
}
System.out.println(l);
}
This, of course, results in:
Exception in thread "main" java.util.ConcurrentModificationException
Even though multiple threads aren't doing it. Anyway.
What's the best solution to this problem? How can I remove an item from the collection in a loop without throwing this exception?
I'm also using an arbitrary Collection here, not necessarily an ArrayList, so you can't rely on get.
Iterator.remove() is safe, you can use it like this:
List<String> list = new ArrayList<>();
// This is a clever way to create the iterator and call iterator.hasNext() like
// you would do in a while-loop. It would be the same as doing:
// Iterator<String> iterator = list.iterator();
// while (iterator.hasNext()) {
for (Iterator<String> iterator = list.iterator(); iterator.hasNext();) {
String string = iterator.next();
if (string.isEmpty()) {
// Remove the current element from the iterator and the list.
iterator.remove();
}
}
Note that Iterator.remove() is the only safe way to modify a collection during iteration; the behavior is unspecified if the underlying collection is modified in any other way while the iteration is in progress.
Source: docs.oracle > The Collection Interface
And similarly, if you have a ListIterator and want to add items, you can use ListIterator#add, for the same reason you can use Iterator#remove — it's designed to allow it.
In your case you tried to remove from a list, but the same restriction applies if trying to put into a Map while iterating its content.
This works:
Iterator<Integer> iter = l.iterator();
while (iter.hasNext()) {
if (iter.next() == 5) {
iter.remove();
}
}
I assumed that since a foreach loop is syntactic sugar for iterating, using an iterator wouldn't help... but it gives you this .remove() functionality.
With Java 8 you can use the new removeIf method. Applied to your example:
Collection<Integer> coll = new ArrayList<>();
//populate
coll.removeIf(i -> i == 5);
A simple test as example:
#Test
public void testRemoveIfOneList() {
List<String> outer = new ArrayList<>();
outer.add("one");
outer.add("two");
outer.add("three");
outer.removeIf(o -> o.length() == 3);
assertEquals(1, outer.size());
}
It even works when you compare two lists and want to remove from both.
#Test
public void testRemoveIfTwoLists() {
List<String> outer = new ArrayList<>();
outer.add("one");
outer.add("two");
outer.add("three");
List<String> inner = new ArrayList<>();
inner.addAll(outer);
// first, it removes from inner, and if anything is removed, then removeIf() returns true,
// leading to removing from outer
outer.removeIf(o -> inner.removeIf(i -> i.equals(o)));
assertEquals(0, outer.size());
assertEquals(0, inner.size());
}
However, if one of the list has duplicates, make sure it's iterated in the inner loop, because for inner list, it will remove all elements meeting the criteria, but for outer list, when any element is removed, it will return immediately and stops checking.
This test will fail:
#Test
public void testRemoveIfTwoListsInnerHasDuplicates() {
List<String> outer = new ArrayList<>();
outer.add("one");
outer.add("one");
outer.add("two");
outer.add("two");
outer.add("three");
outer.add("three");
List<String> inner = new ArrayList<>();
inner.addAll(outer); // both have duplicates
// remove all elements from inner(executed twice), then remove from outer
// but only once! if anything is removed, it will return immediately!!
outer.removeIf(o -> inner.removeIf(i -> i.equals(o)));
assertEquals(0, inner.size()); // pass, inner all removed
assertEquals(0, outer.size()); // will fail, outer has size = 3
}
Since the question has been already answered i.e. the best way is to use the remove method of the iterator object, I would go into the specifics of the place where the error "java.util.ConcurrentModificationException" is thrown.
Every collection class has a private class which implements the Iterator interface and provides methods like next(), remove() and hasNext().
The code for next looks something like this...
public E next() {
checkForComodification();
try {
E next = get(cursor);
lastRet = cursor++;
return next;
} catch(IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
Here the method checkForComodification is implemented as
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
So, as you can see, if you explicitly try to remove an element from the collection. It results in modCount getting different from expectedModCount, resulting in the exception ConcurrentModificationException.
You can either use the iterator directly like you mentioned, or else keep a second collection and add each item you want to remove to the new collection, then removeAll at the end. This allows you to keep using the type-safety of the for-each loop at the cost of increased memory use and cpu time (shouldn't be a huge problem unless you have really, really big lists or a really old computer)
public static void main(String[] args)
{
Collection<Integer> l = new ArrayList<Integer>();
Collection<Integer> itemsToRemove = new ArrayList<>();
for (int i=0; i < 10; i++) {
l.add(Integer.of(4));
l.add(Integer.of(5));
l.add(Integer.of(6));
}
for (Integer i : l)
{
if (i.intValue() == 5) {
itemsToRemove.add(i);
}
}
l.removeAll(itemsToRemove);
System.out.println(l);
}
In such cases a common trick is (was?) to go backwards:
for(int i = l.size() - 1; i >= 0; i --) {
if (l.get(i) == 5) {
l.remove(i);
}
}
That said, I'm more than happy that you have better ways in Java 8, e.g. removeIf or filter on streams.
Same answer as Claudius with a for loop:
for (Iterator<Object> it = objects.iterator(); it.hasNext();) {
Object object = it.next();
if (test) {
it.remove();
}
}
With Eclipse Collections, the method removeIf defined on MutableCollection will work:
MutableList<Integer> list = Lists.mutable.of(1, 2, 3, 4, 5);
list.removeIf(Predicates.lessThan(3));
Assert.assertEquals(Lists.mutable.of(3, 4, 5), list);
With Java 8 Lambda syntax this can be written as follows:
MutableList<Integer> list = Lists.mutable.of(1, 2, 3, 4, 5);
list.removeIf(Predicates.cast(integer -> integer < 3));
Assert.assertEquals(Lists.mutable.of(3, 4, 5), list);
The call to Predicates.cast() is necessary here because a default removeIf method was added on the java.util.Collection interface in Java 8.
Note: I am a committer for Eclipse Collections.
Make a copy of existing list and iterate over new copy.
for (String str : new ArrayList<String>(listOfStr))
{
listOfStr.remove(/* object reference or index */);
}
People are asserting one can't remove from a Collection being iterated by a foreach loop. I just wanted to point out that is technically incorrect and describe exactly (I know the OP's question is so advanced as to obviate knowing this) the code behind that assumption:
for (TouchableObj obj : untouchedSet) { // <--- This is where ConcurrentModificationException strikes
if (obj.isTouched()) {
untouchedSet.remove(obj);
touchedSt.add(obj);
break; // this is key to avoiding returning to the foreach
}
}
It isn't that you can't remove from the iterated Colletion rather that you can't then continue iteration once you do. Hence the break in the code above.
Apologies if this answer is a somewhat specialist use-case and more suited to the original thread I arrived here from, that one is marked as a duplicate (despite this thread appearing more nuanced) of this and locked.
With a traditional for loop
ArrayList<String> myArray = new ArrayList<>();
for (int i = 0; i < myArray.size(); ) {
String text = myArray.get(i);
if (someCondition(text))
myArray.remove(i);
else
i++;
}
ConcurrentHashMap or ConcurrentLinkedQueue or ConcurrentSkipListMap may be another option, because they will never throw any ConcurrentModificationException, even if you remove or add item.
Another way is to use a copy of your arrayList just for iteration:
List<Object> l = ...
List<Object> iterationList = ImmutableList.copyOf(l);
for (Object curr : iterationList) {
if (condition(curr)) {
l.remove(curr);
}
}
A ListIterator allows you to add or remove items in the list. Suppose you have a list of Car objects:
List<Car> cars = ArrayList<>();
// add cars here...
for (ListIterator<Car> carIterator = cars.listIterator(); carIterator.hasNext(); )
{
if (<some-condition>)
{
carIterator().remove()
}
else if (<some-other-condition>)
{
carIterator().add(aNewCar);
}
}
Now, You can remove with the following code
l.removeIf(current -> current == 5);
I know this question is too old to be about Java 8, but for those using Java 8 you can easily use removeIf():
Collection<Integer> l = new ArrayList<Integer>();
for (int i=0; i < 10; ++i) {
l.add(new Integer(4));
l.add(new Integer(5));
l.add(new Integer(6));
}
l.removeIf(i -> i.intValue() == 5);
Java Concurrent Modification Exception
Single thread
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String value = iter.next()
if (value == "A") {
list.remove(it.next()); //throws ConcurrentModificationException
}
}
Solution: iterator remove() method
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String value = iter.next()
if (value == "A") {
it.remove()
}
}
Multi thread
copy/convert and iterate over another one collection. For small collections
synchronize[About]
thread safe collection[About]
I have a suggestion for the problem above. No need of secondary list or any extra time. Please find an example which would do the same stuff but in a different way.
//"list" is ArrayList<Object>
//"state" is some boolean variable, which when set to true, Object will be removed from the list
int index = 0;
while(index < list.size()) {
Object r = list.get(index);
if( state ) {
list.remove(index);
index = 0;
continue;
}
index += 1;
}
This would avoid the Concurrency Exception.
for (Integer i : l)
{
if (i.intValue() == 5){
itemsToRemove.add(i);
break;
}
}
The catch is the after removing the element from the list if you skip the internal iterator.next() call. it still works! Though I dont propose to write code like this it helps to understand the concept behind it :-)
Cheers!
Example of thread safe collection modification:
public class Example {
private final List<String> queue = Collections.synchronizedList(new ArrayList<String>());
public void removeFromQueue() {
synchronized (queue) {
Iterator<String> iterator = queue.iterator();
String string = iterator.next();
if (string.isEmpty()) {
iterator.remove();
}
}
}
}
I know this question assumes just a Collection, and not more specifically any List. But for those reading this question who are indeed working with a List reference, you can avoid ConcurrentModificationException with a while-loop (while modifying within it) instead if you want to avoid Iterator (either if you want to avoid it in general, or avoid it specifically to achieve a looping order different from start-to-end stopping at each element [which I believe is the only order Iterator itself can do]):
*Update: See comments below that clarify the analogous is also achievable with the traditional-for-loop.
final List<Integer> list = new ArrayList<>();
for(int i = 0; i < 10; ++i){
list.add(i);
}
int i = 1;
while(i < list.size()){
if(list.get(i) % 2 == 0){
list.remove(i++);
} else {
i += 2;
}
}
No ConcurrentModificationException from that code.
There we see looping not start at the beginning, and not stop at every element (which I believe Iterator itself can't do).
FWIW we also see get being called on list, which could not be done if its reference was just Collection (instead of the more specific List-type of Collection) - List interface includes get, but Collection interface does not. If not for that difference, then the list reference could instead be a Collection [and therefore technically this Answer would then be a direct Answer, instead of a tangential Answer].
FWIWW same code still works after modified to start at beginning at stop at every element (just like Iterator order):
final List<Integer> list = new ArrayList<>();
for(int i = 0; i < 10; ++i){
list.add(i);
}
int i = 0;
while(i < list.size()){
if(list.get(i) % 2 == 0){
list.remove(i);
} else {
++i;
}
}
One solution could be to rotate the list and remove the first element to avoid the ConcurrentModificationException or IndexOutOfBoundsException
int n = list.size();
for(int j=0;j<n;j++){
//you can also put a condition before remove
list.remove(0);
Collections.rotate(list, 1);
}
Collections.rotate(list, -1);
Try this one (removes all elements in the list that equal i):
for (Object i : l) {
if (condition(i)) {
l = (l.stream().filter((a) -> a != i)).collect(Collectors.toList());
}
}
You can use a while loop.
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry<String, String> entry = iterator.next();
if(entry.getKey().equals("test")) {
iterator.remove();
}
}
I ended up with this ConcurrentModificationException, while iterating the list using stream().map() method. However the for(:) did not throw the exception while iterating and modifying the the list.
Here is code snippet , if its of help to anyone:
here I'm iterating on a ArrayList<BuildEntity> , and modifying it using the list.remove(obj)
for(BuildEntity build : uniqueBuildEntities){
if(build!=null){
if(isBuildCrashedWithErrors(build)){
log.info("The following build crashed with errors , will not be persisted -> \n{}"
,build.getBuildUrl());
uniqueBuildEntities.remove(build);
if (uniqueBuildEntities.isEmpty()) return EMPTY_LIST;
}
}
}
if(uniqueBuildEntities.size()>0) {
dbEntries.addAll(uniqueBuildEntities);
}
If using HashMap, in newer versions of Java (8+) you can select each of 3 options:
public class UserProfileEntity {
private String Code;
private String mobileNumber;
private LocalDateTime inputDT;
// getters and setters here
}
HashMap<String, UserProfileEntity> upMap = new HashMap<>();
// remove by value
upMap.values().removeIf(value -> !value.getCode().contains("0005"));
// remove by key
upMap.keySet().removeIf(key -> key.contentEquals("testUser"));
// remove by entry / key + value
upMap.entrySet().removeIf(entry -> (entry.getKey().endsWith("admin") || entry.getValue().getInputDT().isBefore(LocalDateTime.now().minusMinutes(3)));
The best way (recommended) is use of java.util.concurrent package. By
using this package you can easily avoid this exception. Refer
Modified Code:
public static void main(String[] args) {
Collection<Integer> l = new CopyOnWriteArrayList<Integer>();
for (int i=0; i < 10; ++i) {
l.add(new Integer(4));
l.add(new Integer(5));
l.add(new Integer(6));
}
for (Integer i : l) {
if (i.intValue() == 5) {
l.remove(i);
}
}
System.out.println(l);
}
Iterators are not always helpful when another thread also modifies the collection. I had tried many ways but then realized traversing the collection manually is much safer (backward for removal):
for (i in myList.size-1 downTo 0) {
myList.getOrNull(i)?.also {
if (it == 5)
myList.remove(it)
}
}
In case ArrayList:remove(int index)- if(index is last element's position) it avoids without System.arraycopy() and takes not time for this.
arraycopy time increases if(index decreases), by the way elements of list also decreases!
the best effective remove way is- removing its elements in descending order:
while(list.size()>0)list.remove(list.size()-1);//takes O(1)
while(list.size()>0)list.remove(0);//takes O(factorial(n))
//region prepare data
ArrayList<Integer> ints = new ArrayList<Integer>();
ArrayList<Integer> toRemove = new ArrayList<Integer>();
Random rdm = new Random();
long millis;
for (int i = 0; i < 100000; i++) {
Integer integer = rdm.nextInt();
ints.add(integer);
}
ArrayList<Integer> intsForIndex = new ArrayList<Integer>(ints);
ArrayList<Integer> intsDescIndex = new ArrayList<Integer>(ints);
ArrayList<Integer> intsIterator = new ArrayList<Integer>(ints);
//endregion
// region for index
millis = System.currentTimeMillis();
for (int i = 0; i < intsForIndex.size(); i++)
if (intsForIndex.get(i) % 2 == 0) intsForIndex.remove(i--);
System.out.println(System.currentTimeMillis() - millis);
// endregion
// region for index desc
millis = System.currentTimeMillis();
for (int i = intsDescIndex.size() - 1; i >= 0; i--)
if (intsDescIndex.get(i) % 2 == 0) intsDescIndex.remove(i);
System.out.println(System.currentTimeMillis() - millis);
//endregion
// region iterator
millis = System.currentTimeMillis();
for (Iterator<Integer> iterator = intsIterator.iterator(); iterator.hasNext(); )
if (iterator.next() % 2 == 0) iterator.remove();
System.out.println(System.currentTimeMillis() - millis);
//endregion
for index loop: 1090 msec
for desc index: 519 msec---the best
for iterator: 1043 msec
you can also use Recursion
Recursion in java is a process in which a method calls itself continuously. A method in java that calls itself is called recursive method.
Related
ConcurrentModificationException When removing element using list iterator java
I have an issue removing the 1st and 2nd element of my list even by using the iterator. I have read the following threads but can't fix my issue (those were the most relevant but I checked other material as well): ConcurrentModificationException when trying remove element from list Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop So my code looks like this: List<List<String>> list = cnf.read(); List<List<String>> nlist = new ArrayList<>(); for (List<String> l : list) { if (l.size() <= 3) { nlist.add(l); } else { int size = l.size(); while (size > 3) { List<String> three = l.subList(0, 2); three.add("Y" + (count++)); //Iterator itr = l.iterator(); ListIterator itr = l.listIterator(); int v = 0; while (itr.hasNext()) { itr.next(); if (v == 0 || v == 1) { itr.remove(); v++; } } l.add(0, "Y" + (count++)); size--; nlist.add(three); } nlist.add(l); } } for (List<String> l : nlist) { System.out.println(l.toString()); System.out.println(l.size()); } I get a ConcurrentModificationException at the print statement here : System.out.println(l.toString()); I tried using iterators for my 2 for loops as well but It doesn't seem to make a difference! I am new to posting questions so let me know If I am doing it right! Thank you.
After A long debugging, here is the solution. The sublist function passes by reference and not by value, a sublist created by ArrayList.subList call keeps a reference to the original list and accesses its elementData array directly. For this reason, when adding an element to the "three" list, we alter the state of the original list. this happens here: three.add("Y" + (count++)); A way of fixing it for this specific case is to create and initialize the "three" list the following way: String one = l.get(0); String two = l.get(1); List<String> three = new ArrayList<>(); three.add(one); three.add(two); three.add("Y" + (count)); This allows us to manipulate our lists without getting Concurrency Exceptions (ConcurrentModificationException). However, if you are manipulating big lists, I would suggest you use another less hardcoded method for list creation. I will mark this thread as answered and hope it helps people.
Iterating over unique pairs in a HashSet (Java)
I want to iterate over unique pairs in a HashSet, Ideally, I'd be able to make a copy of the iterator and when it has exhausted itself, iterate the iterator I am making copies of, but Iterators do not like being copied, and I suspect that there is a good reason for this, but I do not know what that is. More specifically, what prevents this (below) behavior from being supported? Iterator<Object> iter = myhash.iterator(); while(iter.hasNext()){ object=iter.next(); Iterator<Object> iterclone = iter.clone(); while(iterclone.hasNext()){ setOfObjectPairs.add(object,iterclone.next()); } } Edit: The point of doing this is to save the current state of the iterator, which is already pointing at the i-th element.
If you cloned an Iterator and the clone modified the underlying Iterable, the original Iterator will throw a ConcurrentModificationException. You can simply use another iterator for the same set. Iterator<Object> iter = h.iterator(); while(iter.hasNext()){ Object o=iter.next(); Iterator<Object> iterclone = h.iterator(); while(iterclone.hasNext()){ //logic } } or you can use and array and iterate over it.
If the order to generate the pairs doesn't matter, then you could use the following code, which avoids having to clone the iterator, while still running in O(n*n) time: HashSet<Integer> set = new HashSet<Integer>(); for (int i = 0; i < 10; i++) { set.add(i); } int to = 0; for (int a : set) { int index = 0; for (int b : set) { if (index++ >= to) { break; } System.out.println(a + ", " + b); } to++; }
If you will not change iterator (you're only want to save state) The best way is to do it here is with for each loop I guess I expect iterators to be only for cases when I modify collection in loop through this collection. For example remove() method from this iterator. The best way I see it is: HashSet<Integer> set = new HashSet<Integer>(); for (int i = 0; i < 10; i++) { set.add(i); } LinkedList<Integer> arr = new LinkedList<Integer>(set); for (Integer i : set) { arr.pollFirst(); for (Integer k : arr) { System.out.println(i + " " + k); } } Here you're coping you're hashset to LinkedList and you're droping first element from LinkedList every time you're iterating through hashset Cloning anything in Java is not good practice. See for example Clone() vs Copy constructor- which is recommended in java
How to remove items from generic arraylist in Android (Java)
I have a generic arraylist of an object here I want to remove certain elements, The problem is when I iterate the list with for loop, I can't do a simple sequence of remove()'s because the elements are shifted after each removal. Thanks
Use Iterator to remove element Like Iterator itr = list.iterator(); String strElement = ""; while (itr.hasNext()) { strElement = (String) itr.next(); if (strElement.equals("2")) { itr.remove(); } } See here
You can iterate the list this way ... public void clean(List<Kopek> kopeks) { for(Kopek kopek : kopeks) { if (kopek.isDirty()) kopeks.remove(kopek); } } Which is equiv to ... public void clean1(List<Kopek> kopeks) { Iterator<Kopek> kopekIter = kopeks.iterator(); while (kopekIter.hasNext()) { Kopek kopek = kopekIter.next(); if (kopek.isDirty()) kopeks.remove(kopek); } } Don't do this ... (due to the reason you have already observed.) public void clean(List<Kopek> kopeks) { for(int i=0; i<kopeks.size(); i++) { Kopek kopek = kopeks.get(i); if (kopek.isDirty()) kopeks.remove(i); } } However, I believe removal by index rather than by object is more efficient. Removal by object is not efficient because the list is in most cases not a hashed list. kopeks.remove(kopek); vs kopeks.remove(i); To achieve positional remove, by treating a moving target appropriately ... public void clean(List<Kopek> kopeks) { int i=0; while(i<kopeks.size()) { Kopek kopek = kopeks.get(i); if (kopek.isDirty()) // no need to increment. kopeks.remove(i); else i++; } }
If you have the objects that you want to remove from your ArrayList<T> you can use : mArrayList.remove(object); or you can use an Iterator to remove your objects: while(iterator.hasNext()){ if(iterator.next() == some condition for removal){ iterator.remove(); } }
You could iterate backwards and remove as you go through the ArrayList. This has the advantage of subsequent elements not needing to shift and is easier to program than moving forwards. List<String> arr = new ArrayList<String>(); ListIterator<String> li = arr.listIterator(arr.size()); // Iterate in reverse. while(li.hasPrevious()) { String str=li.previous(); if(str.equals("A")) { li.remove(); } }
Create a separate ArrayList of Index of the data to be removed from the original ArrayList, then remove those elements by looping over it with for loop. ArrayList<Myobj> arr = new ArrayList<Myobj>(); for (Myobj o : arr){ arr.remove(arr.indexOf(o)); }
without using iterators also solves the issue.. All i wanted to do is get the index which are to be deleted and sort it in decending order then remove it from the list. check the code below Arraylist<obj> addlist = getlist(); List<Integer> indices = new ArrayList<Integer>(); for(int i=0; i<addlist.size() ;i++){ if(addlist.get(i).getDelete()){ indices.add(i); } } Collections.sort(indices, Collections.reverseOrder()); for (int i : indices) addlist.remove(i);
How to avoid java.util.ConcurrentModificationException when iterating through and removing elements from an ArrayList
I have an ArrayList that I want to iterate over. While iterating over it I have to remove elements at the same time. Obviously this throws a java.util.ConcurrentModificationException. What is the best practice to handle this problem? Should I clone the list first? I remove the elements not in the loop itself but another part of the code. My code looks like this: public class Test() { private ArrayList<A> abc = new ArrayList<A>(); public void doStuff() { for (A a : abc) a.doSomething(); } public void removeA(A a) { abc.remove(a); } } a.doSomething might call Test.removeA();
Two options: Create a list of values you wish to remove, adding to that list within the loop, then call originalList.removeAll(valuesToRemove) at the end Use the remove() method on the iterator itself. Note that this means you can't use the enhanced for loop. As an example of the second option, removing any strings with a length greater than 5 from a list: List<String> list = new ArrayList<String>(); ... for (Iterator<String> iterator = list.iterator(); iterator.hasNext(); ) { String value = iterator.next(); if (value.length() > 5) { iterator.remove(); } }
From the JavaDocs of the ArrayList The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException.
You are trying to remove value from list in advanced "for loop", which is not possible, even if you apply any trick (which you did in your code). Better way is to code iterator level as other advised here. I wonder how people have not suggested traditional for loop approach. for( int i = 0; i < lStringList.size(); i++ ) { String lValue = lStringList.get( i ); if(lValue.equals("_Not_Required")) { lStringList.remove(lValue); i--; } } This works as well.
In Java 8 you can use the Collection Interface and do this by calling the removeIf method: yourList.removeIf((A a) -> a.value == 2); More information can be found here
You should really just iterate back the array in the traditional way Every time you remove an element from the list, the elements after will be push forward. As long as you don't change elements other than the iterating one, the following code should work. public class Test(){ private ArrayList<A> abc = new ArrayList<A>(); public void doStuff(){ for(int i = (abc.size() - 1); i >= 0; i--) abc.get(i).doSomething(); } public void removeA(A a){ abc.remove(a); } }
While iterating the list, if you want to remove the element is possible. Let see below my examples, ArrayList<String> names = new ArrayList<String>(); names.add("abc"); names.add("def"); names.add("ghi"); names.add("xyz"); I have the above names of Array list. And i want to remove the "def" name from the above list, for(String name : names){ if(name.equals("def")){ names.remove("def"); } } The above code throws the ConcurrentModificationException exception because you are modifying the list while iterating. So, to remove the "def" name from Arraylist by doing this way, Iterator<String> itr = names.iterator(); while(itr.hasNext()){ String name = itr.next(); if(name.equals("def")){ itr.remove(); } } The above code, through iterator we can remove the "def" name from the Arraylist and try to print the array, you would be see the below output. Output : [abc, ghi, xyz]
Do the loop in the normal way, the java.util.ConcurrentModificationException is an error related to the elements that are accessed. So try: for(int i = 0; i < list.size(); i++){ lista.get(i).action(); }
Here is an example where I use a different list to add the objects for removal, then afterwards I use stream.foreach to remove elements from original list : private ObservableList<CustomerTableEntry> customersTableViewItems = FXCollections.observableArrayList(); ... private void removeOutdatedRowsElementsFromCustomerView() { ObjectProperty<TimeStamp> currentTimestamp = new SimpleObjectProperty<>(TimeStamp.getCurrentTime()); long diff; long diffSeconds; List<Object> objectsToRemove = new ArrayList<>(); for(CustomerTableEntry item: customersTableViewItems) { diff = currentTimestamp.getValue().getTime() - item.timestamp.getValue().getTime(); diffSeconds = diff / 1000 % 60; if(diffSeconds > 10) { // Element has been idle for too long, meaning no communication, hence remove it System.out.printf("- Idle element [%s] - will be removed\n", item.getUserName()); objectsToRemove.add(item); } } objectsToRemove.stream().forEach(o -> customersTableViewItems.remove(o)); }
One option is to modify the removeA method to this - public void removeA(A a,Iterator<A> iterator) { iterator.remove(a); } But this would mean your doSomething() should be able to pass the iterator to the remove method. Not a very good idea. Can you do this in two step approach : In the first loop when you iterate over the list , instead of removing the selected elements , mark them as to be deleted. For this , you may simply copy these elements ( shallow copy ) into another List. Then , once your iteration is done , simply do a removeAll from the first list all elements in the second list.
In my case, the accepted answer is not working, It stops Exception but it causes some inconsistency in my List. The following solution is perfectly working for me. List<String> list = new ArrayList<>(); List<String> itemsToRemove = new ArrayList<>(); for (String value: list) { if (value.length() > 5) { // your condition itemsToRemove.add(value); } } list.removeAll(itemsToRemove); In this code, I have added the items to remove, in another list and then used list.removeAll method to remove all required items.
Instead of using For each loop, use normal for loop. for example,the below code removes all the element in the array list without giving java.util.ConcurrentModificationException. You can modify the condition in the loop according to your use case. for(int i=0; i<abc.size(); i++) { e.remove(i); }
Sometimes old school is best. Just go for a simple for loop but make sure you start at the end of the list otherwise as you remove items you will get out of sync with your index. List<String> list = new ArrayList<>(); for (int i = list.size() - 1; i >= 0; i--) { if ("removeMe".equals(list.get(i))) { list.remove(i); } }
You can also use CopyOnWriteArrayList instead of an ArrayList. This is the latest recommended approach by from JDK 1.5 onwards.
Do somehting simple like this: for (Object object: (ArrayList<String>) list.clone()) { list.remove(object); }
An alternative Java 8 solution using stream: theList = theList.stream() .filter(element -> !shouldBeRemoved(element)) .collect(Collectors.toList()); In Java 7 you can use Guava instead: theList = FluentIterable.from(theList) .filter(new Predicate<String>() { #Override public boolean apply(String element) { return !shouldBeRemoved(element); } }) .toImmutableList(); Note, that the Guava example results in an immutable list which may or may not be what you want.
for (A a : new ArrayList<>(abc)) { a.doSomething(); abc.remove(a); }
"Should I clone the list first?" That will be the easiest solution, remove from the clone, and copy the clone back after removal. An example from my rummikub game: SuppressWarnings("unchecked") public void removeStones() { ArrayList<Stone> clone = (ArrayList<Stone>) stones.clone(); // remove the stones moved to the table for (Stone stone : stones) { if (stone.isOnTable()) { clone.remove(stone); } } stones = (ArrayList<Stone>) clone.clone(); sortStones(); }
I arrive late I know but I answer this because I think this solution is simple and elegant: List<String> listFixed = new ArrayList<String>(); List<String> dynamicList = new ArrayList<String>(); public void fillingList() { listFixed.add("Andrea"); listFixed.add("Susana"); listFixed.add("Oscar"); listFixed.add("Valeria"); listFixed.add("Kathy"); listFixed.add("Laura"); listFixed.add("Ana"); listFixed.add("Becker"); listFixed.add("Abraham"); dynamicList.addAll(listFixed); } public void updatingListFixed() { for (String newList : dynamicList) { if (!listFixed.contains(newList)) { listFixed.add(newList); } } //this is for add elements if you want eraser also String removeRegister=""; for (String fixedList : listFixed) { if (!dynamicList.contains(fixedList)) { removeResgister = fixedList; } } fixedList.remove(removeRegister); } All this is for updating from one list to other and you can make all from just one list and in method updating you check both list and can eraser or add elements betwen list. This means both list always it same size
Use Iterator instead of Array List Have a set be converted to iterator with type match And move to the next element and remove Iterator<Insured> itr = insuredSet.iterator(); while (itr.hasNext()) { itr.next(); itr.remove(); } Moving to the next is important here as it should take the index to remove element.
List<String> list1 = new ArrayList<>(); list1.addAll(OriginalList); List<String> list2 = new ArrayList<>(); list2.addAll(OriginalList); This is also an option.
If your goal is to remove all elements from the list, you can iterate over each item, and then call: list.clear()
What about of import java.util.Collections; List<A> abc = Collections.synchronizedList(new ArrayList<>());
ERROR There was a mistake when I added to the same list from where I took elements: fun <T> MutableList<T>.mathList(_fun: (T) -> T): MutableList<T> { for (i in this) { this.add(_fun(i)) <--- ERROR } return this <--- ERROR } DECISION Works great when adding to a new list: fun <T> MutableList<T>.mathList(_fun: (T) -> T): MutableList<T> { val newList = mutableListOf<T>() <--- DECISION for (i in this) { newList.add(_fun(i)) <--- DECISION } return newList <--- DECISION }
Just add a break after your ArrayList.remove(A) statement
Deleting while iterating in java! but deleting other then the current object
iterator.remove(). is good however my question: if I want while to iterate, for a kind of condition, to delete another object from the array List. Example (al being the arraylist) for (Iterator i = al.iterator(); i.hasNext(); ){ IEvent event =(IEvent) i.next(); if (nbSendingWTS > 0 || nbSendingCTS > 0){ i.remove(); al.remove(swtsee); al.remove(sdctsee); System.out.println("dropping evtg"); } This is giving me an error: Exception in thread "main" java.util.ConcurrentModificationException Also the normal iteration: for(IEVEnt event:al){} is giving an error To be more clear the swtsee a d sdctsee are taken from previous iterations on the arraylist and saved so i can delete if I have the new condition. So is there a way when I detect them to shift them to higher indexes and then I use a reverse iteration? What to do?
You can't remove element as discussed by you. Do not delete while iterating. Keep a Hash for all the objects you want to delete. Do a second iteration which delete using .remove() if the object is in Hash.
You can't delete if you are using for each style or iterator. Use normal for loop like following for(int i=0; i<al.size ; i++){ if(something){ al.remove(i) i--; } } This will work.
To remove with an Iterator, you collect your stuff in a new Collection, and remove in a final step, for example: // list := List (1, 2, 4, 3, 4, 9, 6, 5, 7, 8); List <Integer> toRemove = new ArrayList <Integer> (); for (int i : list) if (i % 2 == 0) toRemove.add (i); list.removeAll (toRemove); I can't see how a1 is connected to your i. As long as it isn't iterated over, it should be secure to call those 2 a1.remove (...)-ings while iterating.
For your reference Java Collections your code should work fine commenting the following lines for (Iterator i = al.iterator(); i.hasNext(); ) { IEvent event =(IEvent) i.next(); if (nbSendingWTS > 0 || nbSendingCTS > 0) { // You have got the iterator for the underlying array list(al) **only remove the elements through iterator.** i.remove(); // after remove thru iterator // you are structurally modifiying arraylist directly(al.remove()) // which gives u concurrent modification // al.remove(swtsee); // al.remove(sdctsee); System.out.println("dropping evtg"); } } and the best way to do is List<Integer> l = new ArrayList<Integer>(); List<Integer> itemsToRemove = new ArrayList<Integer>(); for (int i=0; i < 10; ++i) { l.add(new Integer(1)); l.add(new Integer(2)); } for (Integer i : l) { if (i.intValue() == 2) itemsToRemove.add(i); } l.removeAll(itemsToRemove); System.out.println(l);