Java LinkedList remove() Method



Description

The Java LinkedList remove() method removes first element of the list. Shifts any subsequent elements to the left (subtracts one from their indices).

Declaration

Following is the declaration for java.util.LinkedList.remove() method

public E remove(int index)

Parameters

NA

Return Value

This method returns the first element.

Exception

NoSuchElementException − if list is empty.

Java LinkedList remove(object) Method

Description

The Java LinkedList remove(int index) method removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).

Declaration

Following is the declaration for java.util.LinkedList.remove() method

public E remove(int index)

Parameters

index − The index of the element to be removed .

Return Value

This method returns the element that was removed from the list .

Exception

IndexOutOfBoundsException − if the index is out of range.

Java LinkedList remove(object) Method

Description

The Java LinkedList remove(Object) method removes the first occurrence of the specified element from this list, if it is present.If the list does not contain the element, it is unchanged.

Declaration

Following is the declaration for java.util.LinkedList.remove() method

public boolean remove(Object o)

Parameters

o − The element to be removed from this list, if present.

Return Value

This method returns true if this list contained the specified element, else the list is unchanged.

Exception

NA

Removing an Element from a LinkedList of Integers Example

The following example shows the usage of Java LinkedList remove() method. We're creating a LinkedList of Integers. We're adding couple of Integers to the LinkedList object using add() method calls per element. LinkedList size is printed, linkedList is printed and using remove() method, first element is removed. Then size and linkedList is printed again.

package com.tutorialspoint;

import java.util.LinkedList;

public class LinkedListDemo {
   public static void main(String[] args) {
      
      // create an empty linked list
      LinkedList<Integer> linkedList = new LinkedList<>();

      // use add() method to add elements in the linkedList
      linkedList.add(20);
      linkedList.add(15);
      linkedList.add(30);
      linkedList.add(45);

      System.out.println("Size of list: " + linkedList.size());
	  // let us print all the elements available in list again
      System.out.println("LinkedList = " + linkedList);
      
      // Removes first element 
      linkedList.remove();

      System.out.println("Now, Size of list: " + linkedList.size());
      
      // let us print all the elements available in list again
      System.out.println("LinkedList = " + linkedList);
   }
}   

Output

Let us compile and run the above program, this will produce the following result −

Size of list: 4
LinkedList = [20, 15, 30, 45]
Now, Size of list: 3
LinkedList = [15, 30, 45]

Removing an Element from a LinkedList of Strings Example

The following example shows the usage of Java LinkedList remove(object) method. We're creating a LinkedList of String. We're adding couple of Strings to the LinkedList object using add() method calls per element. LinkedList size is printed, linkedList is printed and using remove(object) method, an element is removed. Then size and linkedList is printed again.

package com.tutorialspoint;

import java.util.LinkedList;

public class LinkedListDemo {
   public static void main(String[] args) {
      
      // create an empty linked list
      LinkedList<String> linkedList = new LinkedList<>();

      // use add() method to add elements in the linkedList
      linkedList.add("A");
      linkedList.add("B");
      linkedList.add("C");
      linkedList.add("D");

      System.out.println("Size of list: " + linkedList.size());
	  // let us print all the elements available in list again
      System.out.println("LinkedList = " + linkedList);
      
      // Removes element B
      linkedList.remove("B");

      System.out.println("Now, Size of list: " + linkedList.size());
      
      // let us print all the elements available in list again
      System.out.println("LinkedList = " + linkedList);
   }
}   

Output

Let us compile and run the above program, this will produce the following result −

Size of list: 4
LinkedList = [A, B, C, D]
Now, Size of list: 3
LinkedList = [A, C, D]

Removing an Element from a LinkedList of Objects Example

The following example shows the usage of Java LinkedList remove(index) method. We're creating a LinkedList of Student objects. We're adding couple of Students to the LinkedList object using add() method calls per element. LinkedList size is printed, linkedList is printed and using remove(index) method, an element is removed. Then size and linkedList is printed again.

package com.tutorialspoint;

import java.util.LinkedList;

public class LinkedListDemo {
   public static void main(String[] args) {
      
      // create an empty linked list
      LinkedList<Student> linkedList = new LinkedList<>();

      // use add() method to add elements in the linkedList
      linkedList.add(new Student(1, "Julie"));
      linkedList.add(new Student(2, "Robert"));
      linkedList.add(new Student(3, "Adam"));

      System.out.println("Size of list: " + linkedList.size());
	  // let us print all the elements available in list again
      System.out.println("LinkedList = " + linkedList);
      
      // Removes element at 3rd position
      linkedList.remove(2);

      System.out.println("Now, Size of list: " + linkedList.size());
      
      // let us print all the elements available in list again
      System.out.println("LinkedList = " + linkedList);
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}   

Output

Let us compile and run the above program, this will produce the following result −

Size of list: 3
LinkedList = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
Now, Size of list: 2
LinkedList = [[ 1, Julie ], [ 2, Robert ]]
java_util_linkedlist.htm
Advertisements