LinkedList removeFirstOccurrence() Method in Java
The java.util.LinkedList.removeFirstOccurrence() is used to remove the first occurrence of the specified element from the list. If there is no occurrence of the specified element the list remains unchanged.
Syntax:
LinkedListObject.removeFirstOccurrence(Object element)
Parameters: The method takes one parameter element which is to be removed from the list. The type Object should be the same as the elements in the list.
Return Value: The method returns boolean True if the list contains the specified element and is deleted, otherwisr it returns false.
Below programs illustrate the removeFirstOccurrence() method:
Program 1 :
// Java code to demonstrate removeFirstOccurrence() method import java.util.LinkedList; public class GfG { // Main method public static void main(String[] args) { // Creating a LinkedList objec LinkedList<String> list = new LinkedList<String>(); // Adding an element at the last list.addLast("one"); // Adding an element at the last list.addLast("two"); // Adding an element at the last list.addLast("three"); // Adding an element at the last list.addLast("one"); System.out.print("List before removing the "+ "first Occurrence of \"one\" : "); // Printing the list System.out.println(list); // Removing first occurrence of one. boolean returnValue = list.removeFirstOccurrence("one"); // Printing the returned value System.out.println("Returned Value : " + returnValue); System.out.print("List after removing the"+ " first Occurrence of \"one\" : "); // Printing the list System.out.println(list); } } |
List before removing the first Occurrence of "one" : [one, two, three, one] Returned Value : true List after removing the first Occurrence of "one" : [two, three, one]
Program 2 :
// Java code to demonstrate removeFirstOccurrence method in LinkedList import java.util.LinkedList; public class GfG { // Main method public static void main(String[] args) { // Creating a LinkedList objec LinkedList<Integer> list = new LinkedList<Integer>(); // Adding an element at the last list.addLast(10); // Adding an element at the last list.addLast(20); // Adding an element at the last list.addLast(30); // Adding an element at the last list.addLast(10); System.out.print("List before removing the"+ " first Occurrence of \"10\" : "); // Printing the list System.out.println(list); // Removing first occurrence of one. boolean returnValue = list.removeFirstOccurrence(10); // Printing the returned value System.out.println("Returned Value : " + returnValue); System.out.print("List after removing the"+ " first Occurrence of \"10\" : "); // Printing the list System.out.println(list); } } |
List before removing the first Occurrence of "10" : [10, 20, 30, 10] Returned Value : true List after removing the first Occurrence of "10" : [20, 30, 10]
Recommended Posts:
- ConcurrentLinkedDeque removeFirstOccurrence() method in Java
- LinkedBlockingDeque removeFirstOccurrence() method in Java
- ArrayDeque removeFirstOccurrence() Method in Java
- BlockingDeque removeFirstOccurrence() method in Java with Examples
- LinkedList contains() Method in Java
- LinkedList get() Method in Java
- LinkedList add() Method in Java
- LinkedList set() Method in Java
- LinkedList pop() Method in Java
- LinkedList spliterator() method in Java
- LinkedList push() Method in Java
- LinkedList getFirst() Method in Java
- LinkedList getLast() Method in Java
- LinkedList clear() Method in Java
- LinkedList removeLastOccurrence() method in Java with Example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



