In this post, we will learn how to remove the first and last element from LinkedList using the following APIs:
- removeFirst()
- removeLast()
Let's first create LinkedList with few fruits and then use remove methods to remove fruits from LinkedList.
LinkedList<String> fruitList = new LinkedList<>();
fruitList.add("Apple");
fruitList.add("banana");
fruitList.add("mango");
fruitList.add("Pinaple");
System.out.println("Initial LinkedList = " + fruitList);
removeFirst()
Remove the first element in the LinkedList. Throws NoSuchElementException if the LinkedList is empty.
String element = fruitList.removeFirst();
System.out.println("Removed the first element " + element + " => " + fruitList);
removeLast()
Remove the last element in the LinkedList. Throws NoSuchElementException if the LinkedList is empty
element = fruitList.removeLast();
System.out.println("Removed the last element " + element + " => " + fruitList);
Complete Example
package com.javaguides.collections.linkedlistexamples; import java.util.LinkedList; public class RemoveElementsFromLinkedListExample { public static void main(String[] args) { LinkedList < String > fruitList = new LinkedList < > (); fruitList.add("Apple"); fruitList.add("banana"); fruitList.add("mango"); fruitList.add("Pinaple"); System.out.println("Initial LinkedList = " + fruitList); // Remove the first element in the LinkedList String element = fruitList.removeFirst(); System.out.println("Removed the first element " + element + " => " + fruitList); // Remove the last element in the LinkedList element = fruitList.removeLast(); System.out.println("Removed the last element " + element + " => " + fruitList); } }
Output
Initial LinkedList = [Apple, banana, mango, Pinaple]
Removed the first element Apple => [banana, mango, Pinaple]
Removed the last element Pinaple => [banana, mango]
Related Collections Examples
- Java LinkedHashMap Example
- Java HashSet Example
- Java LinkedList Example
- Java ArrayList Example
- Java Comparator Interface Example
- Java Comparable Interface Example
- Java IdentityHashMap Example
- Java WeakHashMap Example
- Java EnumMap Example
- Java CopyOnWriteArraySet Example
- Java EnumSet Class Example
- Guide to Java 8 forEach Method
- Different Ways to Iterate over a List in Java [Snippet]
- Different Ways to Iterate over a Set in Java [Snippet]
- Different Ways to Iterate over a Map in Java [Snippet]
- Iterate over TreeSet in Java Example
- Iterate over LinkedHashSet in Java Example
- Remove First and Last Elements of LinkedList in Java
- Iterate over LinkedList using an Iterator in Java
- Search an Element in an ArrayList in Java
- Iterate over ArrayList using Iterator in Java
- Remove Element from HashSet in Java
- Iterating over a HashSet using Iterator
- How To Remove Duplicate Elements From ArrayList In Java?
- Different Ways to Iterate over List, Set, and Map in Java
Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours
Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course
Comments
Post a Comment