Sadina Khatun
Sadina Khatun

Reputation: 1166

Arraylist not able to remove duplicate strings

public static void main(String[] args) {
       List<String> list = new ArrayList();
       list.add("AA");list.add("Aw");list.add("Aw");list.add("AA");
       list.add("AA");list.add("A45");list.add("AA");
       list.add("Aal");list.add("Af");list.add("An");

       System.out.println(list);

       for(int i=0;i<list.size();i++){
          if(list.get(i).equals("AA")){
             list.remove(i);
          }
       }

       System.out.println(list);
}

I am currently attempting to remove all the elements within the ArrayList that have the value of "AA", However, it's only removing some of them and not all. can anyone explain to me what am I doing wrong?

elements within arraylist:

[AA, Aw, Aw, AA, AA, A45, AA, Aal, Af, An]

output after i've attempted to remove all the strings that have the value of "AA".

[Aw, Aw, AA, A45, Aal, Af, An]

why still AA is within the output list?

Upvotes: 2

Views: 147

Answers (7)

Donald Raab
Donald Raab

Reputation: 6686

If you're using Java 8 there is a removeIf method on Collection that takes a Predicate, so you can write the code as follows:

list.removeIf(each -> each.equals("AA"));

If you look at the implementation of this method, you will see it uses an Iterator and calls remove for each element that satisfies the given Predicate.

Upvotes: 0

ATG
ATG

Reputation: 105

Your for loop skips the 5th element when i=3. To bypass this, use the following code:

List<String> list = new ArrayList();
list.add("AA");list.add("Aw");list.add("Aw");list.add("AA");
list.add("AA");list.add("A45");list.add("AA");
list.add("Aal");list.add("Af");list.add("An");
System.out.println(list);
List newList = new ArrayList();
for(int i=0;i<list.size();i++){
    String tempStr = list.get(i);
    if(!("AA").equals(tempStr)){
         newList.add(tempStr);
     }
 }
 System.out.println(newList);

This gives me proper output as:

[AA, Aw, Aw, AA, AA, A45, AA, Aal, Af, An] [Aw, Aw, A45, Aal, Af, An]

Upvotes: 0

Ousmane D.
Ousmane D.

Reputation: 56433

The size of the list is changing as you're removing the elements, i.e the elements within the list are shifting to the left as more and more elements are being removed.

An alternative to removing all the duplicated "AA" from the list is using Stream#filter method to keep all the elements that don't have the value of "AA".

System.out.println(list.stream().filter(s -> !s.equals("AA")).collect(Collectors.toList()));

Upvotes: 0

Gaurav Raj Ghimire
Gaurav Raj Ghimire

Reputation: 9

The index of item is shifted in array-list as items are removed so the iteration is incorrect. Every time your checker finds "AA". You need to start the loop again.

Upvotes: 0

this is wrong:

 for (int i = 0; i < list.size(); i++) {
        if (list.get(i).equals("AA")) {
            list.remove(i);
        }
    }

because your list is changing its size as long as you remove elements....

you need an iterator:

Iterator<String> iter = list.iterator();
    while (iter.hasNext()) {
        if (iter.next().equals("AA")) {
            iter.remove();
        }
    }

using java8:

 List<String> newFilteredList = list.stream().filter(i -> !i.equals("AA")).collect(Collectors.toList());
    System.out.println(newFilteredList);

Upvotes: 5

Brett Okken
Brett Okken

Reputation: 6306

You are skipping items as you iterate.

When you call list.remove(i), all the elements shift to the left. So if you remove item at index 3, the item previously at index 4 will shift over to be at index 3. However, you are still incrementing i for next loop and thus skip checking an entry.

Upvotes: 3

JC97
JC97

Reputation: 1620

If you use an Iterator, it's possible to remove the current item in your loop. Take a look here: Why do we need to use iterator on ArrayList in Java?

Upvotes: 0

Related Questions