Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




Java - Remove from List


As we have seen the implementations of a List are :

  1. ArrayList

  2. LinkedList


Let us see the ArrayList implementation first.


How to remove an element from the ArrayList by its name/value?


Let us say, we have a List that contains three names, Mohan, Kriti and Salim. And we want to remove Kriti from the List.


It can be done with the remove() Method


Example :



import java.util.ArrayList;
import java.util.List;

public class MyApplication {
    public static void main(String[] args) {

        List x = new ArrayList<>();

        x.add("Mohan");
        x.add("Kriti");
        x.add("Salim");

        x.remove("Kriti");

        for (String data : x) {
            System.out.println(data);
        }
    }
}


Output :



  Mohan
  Salim

So, in the above code we have created a List,


List x = new ArrayList<>();

And initialised three names to the variable x,


x.add("Mohan");
x.add("Kriti");
x.add("Salim");

Below is how the values are positioned in the List,

Spring_Boot

Next, we have used the remove() method that searches for the name Kriti and removes it from the List.


x.remove("Kriti");
Spring_Boot

Output :



  Mohan
  Salim

How to remove an element from the ArrayList by its index/position?


Let us say, we have a List that contains three names, Mohan, Kriti and Salim. And we want to remove the element at index/position 2 from the List.


So, we can use remove() method again to remove an element from the List.


Example :



import java.util.ArrayList;
import java.util.List;

public class MyApplication {
    public static void main(String[] args) {

        List x = new ArrayList<>();

        x.add("Mohan");
        x.add("Kriti");
        x.add("Salim");

        x.remove(2);

        for (String data : x) {
            System.out.println(data);
        }
    }
}


Output :



  Mohan
  Kriti

So, in the above code we have created a List and initialised to the variable x.


List x = new ArrayList<>();

x.add("Mohan");
x.add("Kriti");
x.add("Salim");

Below is how the values are positioned in the List,

Spring_Boot

Next, we have used the remove() method that searches for the element at index/position 2 and removes it from the List.


x.remove(2);

And as we can see, there is Salim at index/position 2. So Salim is removed from the List.

Spring_Boot

And we get the below output,

Output :



  Mohan
  Kriti

How to remove all the elements from the ArrayList?


The removeAll() Method can be used to remove all the elements from the List.


Example :



import java.util.ArrayList;
import java.util.List;

public class MyApplication {
    public static void main(String[] args) {

        List x = new ArrayList<>();

        x.add("Mohan");
        x.add("Kriti");
        x.add("Salim");

        x.removeAll(x);

        for (String data : x) {
            System.out.println(data);
        }
    }
}


Output :




So, in the above code we have created a List and initialised to the variable x.


List x = new ArrayList<>();

x.add("Mohan");
x.add("Kriti");
x.add("Salim");

Below is how the values are positioned in the List,

Spring_Boot

Next, we have used the removeAll() method that removes all the elements from the ArrayList making the List empty.


And we get an empty List as output,


Note : The same code applies for LinkedList as well. Just that you need to replace ArrayList LinkedList.