Removing element from List is our primary requirement during project development, Here i like to specified so many ways to remove element from list. There different ways in java remove element from list.

Example 1 : List remove element using predicate or removeIf() method (Java 8)

Removes all of the elements of this collection that satisfy the given predicate.  Errors or runtime exceptions thrown during iteration or by the predicate are relayed to the caller.

List<String> languages = new ArrayList<>(Arrays.asList("Java", "C", "PHP", "Net", "VB"));

       languages.removeIf(language -> language.equalsIgnoreCase("C"));     // Its remove all element which satisfied predicate condition.

       languages.forEach(language -> {
           System.out.println(language);
       });
Output:
Java
PHP
Net
VB

 Example 2: List remove element using retailAll() method (Java 8)

Retains only the elements in this list that are contained in the specified collection (optional operation).  In other words, removes from this list all of its elements that are not contained in the specified collection.

List<String> languageAllowed = Arrays.asList("Java", "C", "VB", "Python");
       List<String> languages = new ArrayList<>(Arrays.asList("Java", "C", "PHP", "Net", "VB"));

       languages.retainAll(languageAllowed);           // its will remove all element which not in languageAllowed List

       languages.forEach(language -> {                     // lambda example
           System.out.println(language);
       });
Output
Java
C
VB

Example 3 : List remove element using iterator (Java 7)

List<String> languages = new ArrayList<>(Arrays.asList("Java", "C", "PHP", "Net", "VB"));

        Iterator<String> languageIterator=languages.iterator();
        while (languageIterator.hasNext()){
            if(languageIterator.next().equalsIgnoreCase("C")){
                languageIterator.remove();              // it will remove element from collection
            }
        }

        languages.forEach(language -> {
            System.out.println(language);
        });
Output
Java
PHP
Net
VB

Example 4: List remove element using Stream (Java 8)

When we are using Stream and collection List at that it will create another List. Its will not remove element from same Collection.

List<String> languages = new ArrayList<>(Arrays.asList("Java", "C", "PHP", "Net", "VB"));

        List<String> newLanguagesList = languages.stream().filter(language->{
           return !language.equalsIgnoreCase("C");                                        // its will return true or false based on that element will be removed.
        }).collect(Collectors.toList());

        newLanguagesList.forEach(language -> {
            System.out.println(language);
        });
 Output:
Java
PHP
Net
VB

Example 5 : Don’t try it

Here we are remove element using forEach which is very bad code and Its will throws java.util.ConcurrentModificationException. 

List<String> languages = new ArrayList<>(Arrays.asList("Java", "C", "PHP", "Net", "VB"));

        for(String language:languages){
            if(language.equalsIgnoreCase("C")){
                languages.remove(language);
            }
        }

        languages.forEach(language -> {
            System.out.println(language);
        });
Output: Always Exception
Exception in thread "main" java.util.ConcurrentModificationException
  at java.base/java.util.ArrayList$Itr.checkForComodification(ArrayList.java:937)
  at java.base/java.util.ArrayList$Itr.next(ArrayList.java:891)
  at module.maven.demo/com.javadeveloperzone.ListRemoveElement.example6(ListRemoveElement.java:83)
  at module.maven.demo/com.javadeveloperzone.ListRemoveElement.main(ListRemoveElement.java:13)

 

References:

https://stackoverflow.com/questions/32335335/how-to-remove-elements-from-a-list-with-lambda-based-on-another-list

https://stackoverflow.com/questions/35701337/java-8-lambda-get-and-remove-element-from-list

 

Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.
Yes
No
Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *