Remove item from ArrayList android
To remove elements from an ArrayList in Java you have the following options. Show
In this post we'll see when to use which method and why. Table of contents
ArrayList remove() methodArrayList provides two overloaded remove methods for removing element from an ArrayList in Java-
If you are just removing an element from an ArrayList without looping the list then you can remove an element by giving its index in the list or specifying the object itself. Example code import java.util.ArrayList; import java.util.List; public class RemoveFromListDemo { public static void main(String[] args) { ListOutput Original List- [Delhi, Mumbai, Kolkata, Hyderabad, Bangalore, Mumbai] List after removing elements- [Delhi, Kolkata, Hyderabad, Bangalore]Removing element from ArrayList while iterating the listNote that using ArrayList's remove method with enhanced for loop or iterator may result in ConcurrentModificationException as the iterators returned by ArrayList class's iterator and ListIterator methods are fail-fast. Which means 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. (A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.) Example code Here is an example where ArrayList's remove() method is used while iterating a list using For-Each loop. As you can see ConcurrentModificationException is thrown. public class RemoveFromListDemo { public static void main(String[] args) { ListOutput Exception in thread "main" java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(Unknown Source) at java.util.ArrayList$Itr.next(Unknown Source) at org.netjs.prog.RemoveFromListDemo.main(RemoveFromListDemo.java:20)Using Iterator's remove method to remove element from ArrayListWhile looping if you want to remove any element from an ArrayList you should use use Iterator's remove method so that ConcurrentModificationException is not thrown. public class RemoveFromListDemo { public static void main(String[] args) { ListOutput city Delhi city Mumbai city Kolkata city Hyderabad city Bangalore city Mumbai After deletion city Delhi city Mumbai city Kolkata city MumbaiUsing ArrayList's remove() method with normal for loopArrayList's remove method can be used with normal for loop to remove an ArrayList in Java. If index is passed then it may give undesired result because of the fact that all the other elements are shifted to the left when an element is removed. Output It can be seen that Hyderabad is removed alright but Mumbai is removed instead of Bangalore. This happened because after Hyderabad is removed elements in the list are shifted and Bangalore came in the place of Hyderabad and Mumbai in place of Bangalore. Thus Mumbai was at index 4 hence removed. In the previous code if we were using city(object) in the if condition then it would have run fine. public class RemoveFromListDemo { public static void main(String[] args) { ListOutput After deletion [Delhi, Mumbai, Hyderabad, Mumbai]Using removeIf() to remove element from ArrayListIf you are using Java 8 then removeIf method can be used to remove element from ArrayList, which takes Predicate functional interface as a parameter. In that case we can write the removal code with in one line as- for(int i = 0; i < cityList.size(); i++){ cityList.removeIf(p -> p.equalsIgnoreCase("Hyderabad") || p.equalsIgnoreCase("Bangalore")); }ArrayList remove method and AutoBoxing problemWhile removing elements from an ArrayList of Integers we may get problem because of Autoboxing. As already mentioned there are two overloaded remove methods-
If we give an int as parameter then it will always be treated as a call to remove method with int parameter. Output So, even if a user thinks while removing the second time that he is giving an object as parameter and hoping autoboxing will take care of all the wrapping to Integer Object chore it won't happen in this case. Second call will also be treated as a call to remove method with int parameter. First remove has already removed one element so now the list size is 3 and trying to access index 4 in such a list will throw IndexOutOfBoundsException. So in this case user has to explicitly tell the compiler that remove method which takes object as parameter has to be called. numberList.remove(new Integer(4));Recommendations for learning (Udemy Courses)
That's all for this topic How to Remove Elements From an ArrayList in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks! >>>Return to Java Advance Tutorial Page | Return to Java Collections Framework Tutorial Page>>> Related Topics
You may also like-
|