I am using java.util.ArrayList
, I want to remove all the occurrences of a particular element.
List l = new ArrayList
You can use the removeAll() method.
list.removeAll(Arrays.asList("someDuplicateString"));
Another way using Java 8:
l.removeIf("first"::equals);
l.removeAll(Collections.singleton("first"));
while(l.remove("first")) { }
This removes all elements "first" from the list.
Since in your example you are using Strings I guess did should do the trick.
for(int i = 0; i < list.size();i++){
if(list.get(i).equals(someStringNameOrValue)){
list.remove(i--);
}
}
Looks like I misunderstood your question. I updated my answer. Am I right? you want to remove all occurrences of "first" ?