Remove elements from an arraylist in Java

preview_player
Показать описание
In this tutorial, we are going to learn how to safely remove elements from an ArrayList in Java while using a for loop to loop over that same list.

The other day I was using a for loop in Java to loop over a collection. Inside of that loop, I was evaluating each element to see if it met some criteria. If it did meet that criteria I wanted to remove that element from the collection.

Each time I tried to remove the element Java would throw a concurrent modification exception and my program would fail. There is actually a really good reason this happens. If you understand what is happening underneath the hood when we use a for loop it all makes sense. We are using an iterator to loop over the collection while using the collection to remove the element and this never updates the iterator to say "I removed an element".

In this tutorial, we will look at one way to solve it and then an even easier way to fix this problem. In Java 8 a new method was added to the collections interface called removeIf. This method takes a predicate and makes it super easy for us to remove items from a collection if they meet some criteria.

I really hope you enjoyed this tutorial and if you did please let me know what other tutorials you might enjoy seeing. Please Subscribe to this channel and leave a comment below.

Thank You
Dan Vega
Рекомендации по теме
Комментарии
Автор

Thanks! This was excellent! The font was too small, but the lesson was excellent.

WhoWhatWhy
Автор

Thanks Dan learned new method on collections

janakisrinvasdevalla
Автор

That helps me a lot thank you so much 🙏🏻💗

رند-سح
Автор

Thanks man it, really helped with my assignment

MrAbsinator
Автор

thhanks this helped, i thought you could just loop on collections like arrays, apparently you cant

nicholasc.
Автор

how would you be able to do this without using premade methods like .remove? ive tried to find information on it online but i cant find anything on it unfortunately :|

pokemonMJP
Автор

Why is there no exception in Java 17 for the first example?

dejanristicrica
Автор

hey from Germany, clean explanation very good! After i implemented the iterator it works fine for hours and suddenly the app crashes with an currentmodificationexception. This is my code: newText = newText.toLowerCase();
ArrayList<String> newList = new ArrayList<>();
for(Iterator<String> iterator =
String item = iterator.next(); //The Exception is thrown here
String date = item.toLowerCase();
if(date.contains(newText)){
newList.add(item);
}
}
Any ideas? Thanks!

Sampelmind
Автор

Holy shit, I almost wanna kiss you now, that was incredibly helpful

shoulderescape
Автор

Please increment font size. It is very small

perihanersoy
Автор

Nice. I love to learn new ways to code. I am a current student taking a Java course and I have a question, if thats ok. In an Auction class I have a method called removeLot. I am using ArrayLists and I want to to be able to remove lots from an auction when they have either sold or expired. I wrote out my method using a for loop to iterate but when the lot number gets removed it does not reset the lot number position. Say I create an instance in my Auction class, Auctuion1...and then enter 4 separate lots with index positions 1-4. If I delete lot 3, wouldn't it be prudent to update lot 4 to lot 3 position or would that confuse the compiler?

Here is my sample code:

/**
* Remove a lot once it has either sold or expired.
*/
public Lot removeLot(int lotNumber)
{
for (Lot currentLot : lots) {
if (currentLot.getNumber() == lotNumber) {
lots.remove(lotNumber);
return currentLot;
}
}
return null;
}

Thanks for your time and I have subscribed to your channel.

jeremysmith
welcome to shbcf.ru