Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove element from ArrayList by checking its value?

I have ArrayList, from which I want to remove an element which has particular value...

for eg.

ArrayList<String> a=new ArrayList<String>(); a.add("abcd"); a.add("acbd"); a.add("dbca"); 

I know we can iterate over arraylist, and .remove() method to remove element but I dont know how to do it while iterating. How can I remove element which has value "acbd", that is second element?

like image 219
vikas devde Avatar asked Jan 09 '13 09:01

vikas devde


People also ask

Can we remove element from ArrayList?

Using the remove() method of the ArrayList class is the fastest way of deleting or removing the element from the ArrayList. It also provides the two overloaded methods, i.e., remove(int index) and remove(Object obj).

How do you remove an element from an ArrayList based on an index?

The remove(int index) method present in java. util. ArrayList class removes the element at the specified position in this list and shifts any subsequent elements to the left (i.e. subtracts one from their indices).

How do you remove an item from an ArrayList object?

There are two ways to remove objects from ArrayList in Java, first, by using the remove() method, and second by using Iterator. ArrayList provides overloaded remove() method, one accepts the index of the object to be removed i.e. remove(int index), and the other accept objects to be removed, i.e. remove(Object obj).


2 Answers

In your case, there's no need to iterate through the list, because you know which object to delete. You have several options. First you can remove the object by index (so if you know, that the object is the second list element):

 a.remove(1);       // indexes are zero-based 

Or, you can remove the first occurence of your string:

 a.remove("acbd");  // removes the first String object that is equal to the                     // String represented by this literal 

Or, remove all strings with a certain value:

 while(a.remove("acbd")) {} 

It's a bit more complicated, if you have more complex objects in your collection and want to remove instances, that have a certain property. So that you can't remove them by using remove with an object that is equal to the one you want to delete.

In those case, I usually use a second list to collect all instances that I want to delete and remove them in a second pass:

 List<MyBean> deleteCandidates = new ArrayList<>();  List<MyBean> myBeans = getThemFromSomewhere();   // Pass 1 - collect delete candidates  for (MyBean myBean : myBeans) {     if (shallBeDeleted(myBean)) {        deleteCandidates.add(myBean);     }  }   // Pass 2 - delete  for (MyBean deleteCandidate : deleteCandidates) {     myBeans.remove(deleteCandidate);  } 
like image 177
Andreas Dolk Avatar answered Sep 22 '22 06:09

Andreas Dolk


One-liner (java8):

list.removeIf(s -> s.equals("acbd")); // removes all instances, not just the 1st one 

(does all the iterating implicitly)

like image 37
Andrejs Avatar answered Sep 20 '22 06:09

Andrejs