Example 1: Remove element using remove()
import java.util.LinkedList; class Main { public static void main(String[] args) { LinkedList<String> languages = new LinkedList<>(); // add elements in LinkedList languages.add("Java"); languages.add("Python"); languages.add("JavaScript"); languages.add("Kotlin"); System.out.println("LinkedList: " + languages); // remove elements from index 1 String str = languages.remove(3); System.out.println("Removed Element: " + str); System.out.println("Updated LinkedList: " + languages); } } Output
LinkedList: [Java, Python, JavaScript, Kotlin] Removed Element: Kotlin Updated LinkedList: [Java, Python, JavaScript]
In the above example, we have created a linkedlist named languages. Here, the remove() method to remove an element from the linkedlist.
The method takes the index number of the element as its parameter.
Example 2 Using listIterator() Method
We can also the listsIterator() to remove elements from the linkedlist.
import java.util.ArrayList; import java.util.ListIterator; class Main { public static void main(String[] args) { ArrayList<String> animals= new ArrayList<>(); // add elements animals.add("Dog"); animals.add("Cat"); animals.add("Horse"); System.out.println("LinkedList: " + animals); // creating an object of ListIterator ListIterator<String> listIterate = animals.listIterator(); listIterate.next(); // चemove element returned by next() listIterate.remove(); System.out.println("New LinkedList: " + animals); } } Output
LinkedList: [Dog, Cat, Horse] New LinkedList: [Cat, Horse]
In the above example, the listIterator() method returns an iterator to access each element of the linkedlist.
Here, the next() method returns the next element in the linkedlist.
Example 3: Remove all elements using clear()
We can use the clear() method to remove all the elements of a linkedlist.
import java.util.LinkedList; class Main { public static void main(String[] args) { LinkedList<String> languages = new LinkedList<>(); // add elements in LinkedList languages.add("Java"); languages.add("Python"); languages.add("Swift"); System.out.println("LinkedList: " + languages); // remove all the elements languages.clear(); System.out.println("LinkedList after clear(): " + languages); } } Output
LinkedList: [Java, Python, Swift] LinkedList after clear(): []
Note: We can also use the removeAll() method to remove all the elements. However, the clear() method is considered more efficient than the removeAll() method.
Example 4: Using the removeIf() Method
We can also remove elements from a linkedlist if they satisfied a certain condition. For this, we use the removeIf() method.
import java.util.LinkedList; class Main { public static void main(String[] args) { LinkedList<Integer> numbers = new LinkedList<>(); // add elements in LinkedList numbers.add(2); numbers.add(3); numbers.add(4); numbers.add(5); System.out.println("LinkedList: " + numbers); // remove all elements less than 4 numbers.removeIf((Integer i) -> i < 4); System.out.println("Updated LinkedList: " + numbers); } } Output
LinkedList: [2, 3, 4, 5] Updated LinkedList: [4, 5]
In the above example, (Integer i) -> i < 4 is a lambda expression. To learn more about lambdas, visit Java Lambda Expression. It returns the number less than 4.
The removeIf() method removes the number returned by the lambda expression.