Languages
[Edit]
EN

Java - iterate over List and remove indicated items

0 points
Created by:
Jasmin
305

In this article, we would like to show you how to iterate over List and remove indicated items in Java.

Note:

By iterating and deleting, we change the structure of the list. After deleting an element, loop iterators get lost, that's why the API was issued.

1. Using iterator

In this example, we use an iterator to remove indicated items from the letters ArrayList.

import java.util.*;

public class Example {

    public static void main(String[] args) {
        List<String> letters = new ArrayList<>();

        letters.add("A");
        letters.add("B");
        letters.add("A");
        letters.add("C");

        Iterator<String> iterator = letters.iterator();

        while (iterator.hasNext()) {
            String element = iterator.next();
            if (element.equals("A")) {
                iterator.remove();
            }
        }

        System.out.println(letters);
    }
}

Output:

[B, C]

2. Using Collection.removeIf()

In this example, we use Java 8 removeIf() method to remove indicated elements from the letters ArrayList.

import java.util.*;

public class Example {

    public static void main(String[] args) {
        List<String> letters = new ArrayList<>();

        letters.add("A");
        letters.add("B");
        letters.add("A");
        letters.add("C");

        letters.removeIf(element -> element.equals("A"));

        System.out.println(letters);
    }
}

Output:

[B, C]

3. Collect and remove using removeAll()

In this example, we collect indicated elements in the new ArrayList, then we use removeAll() method that removes all the collected elements from the original ArrayList.

import java.util.*;

public class Example {

    public static void main(String[] args) {
        List<String> letters = new ArrayList<>();

        letters.add("A");
        letters.add("B");
        letters.add("A");
        letters.add("C");

        String letterToRemove = "A";
        List<String> lettersToRemove = new ArrayList<>();

        // find letters to remove
        for (String letter : letters) {
            if (letter.equals(letterToRemove)) {
                lettersToRemove.add(letter);
            }
        }

        // remove letters
        letters.removeAll(lettersToRemove);

        System.out.println(letters);
    }
}

Output:

[B, C]

4. Using stream

In this example, we create an instance of a stream from the letters ArrayList, filter each element and collect the result in the new filteredLetters.

import java.util.*;
import java.util.stream.Collectors;

public class Example {

    public static void main(String[] args) {
        List<String> letters = new ArrayList<>();

        letters.add("A");
        letters.add("B");
        letters.add("A");
        letters.add("C");

        List<String> filteredLetters = letters
                .stream()
                .filter(element -> !element.equals("A"))
                .collect(Collectors.toList());

        System.out.println(filteredLetters);
    }
}

Output:

[B, C]

5*. Using reversed for loop

In this example, we remove all items from the letters ArrayList using reversed for loop.

import java.util.*;

public class Example {

    public static void main(String[] args) {
        List<String> letters = new ArrayList<>();

        letters.add("A");
        letters.add("B");
        letters.add("C");


        for (int i = letters.size(); i-- > 0; ) {
            letters.remove(i);
        }

        System.out.println(letters);
    }
}

Output:

[]

Note:

This solution is inefficient and not recommended.

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join