The Wayback Machine - https://web.archive.org/web/20240930173253/https://www.geeksforgeeks.org/linkedlist-removefirstoccurrence-method-in-java/
Open In App

LinkedList removeFirstOccurrence() Method in Java

Last Updated : 30 Jun, 2022
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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, otherwise it returns false.
Below programs illustrate the removeFirstOccurrence() method: 
Program 1 :  

java




// Java code to demonstrate removeFirstOccurrence() method
import java.util.LinkedList;
public class GfG {
    // Main method
    public static void main(String[] args)
    {
 
        // Creating a LinkedList object
        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);
    }
}


Output: 

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




// 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 object
        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);
    }
}


Output: 

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]

 



Previous Article
Next Article

Similar Reads

ArrayDeque removeFirstOccurrence() Method in Java
The java.util.ArrayDeque.removeFirstOccurrence(Object) method in Java is used to remove the first occurrence of a specific element from this deque. If the element is present only once, then that element is removed and true is returned. If no such element is removed then false is returned. Syntax: Array_Deque.removeFirstOccurrence(Object O) Paramete
2 min read
LinkedBlockingDeque removeFirstOccurrence() method in Java
The removeFirstOccurrence() method of LinkedBlockingDeque removes the first occurrence of the specified element from this deque. If the deque does not contain the element, it remains unchanged. It returns true if this deque contained the specified element, else it returns false. Syntax: public boolean removeFirstOccurrence(Object o) Parameters: Thi
2 min read
ConcurrentLinkedDeque removeFirstOccurrence() method in Java
The java.util.concurrent.ConcurrentLinkedDeque.removeFirstOccurrence() method is an in-built method in Java which accepts a parameter and removes the first appearance of that element in the deque. Thus, in case the element is not present in the deque, it remains unchanged. Syntax: public boolean removeFirstOccurrence(Object o) Parameters: The funct
2 min read
BlockingDeque removeFirstOccurrence() method in Java with Examples
The removeFirstOccurrence() method of BlockingDeque removes the first occurrence of the specified element from this deque. If the deque does not contain the element, it remains unchanged. It returns true if this deque contained the specified element, else it returns false. Syntax: public boolean removeFirstOccurrence(Object o) Parameters: This meth
2 min read
Java.util.LinkedList.peek() , peekfirst(), peeklast() in Java
Linked list class offers the functionality to "look into" the first and last elements of the list and hence can be useful in cases where only the retrieval is required and not necessarily the deletion is required. Three functionalities are present and all are discussed in this article. 1. peek() : This method retrieves, but does not remove, the hea
4 min read
Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java
Linked list also has a function that does the work of flexible addition of elements and helps addition both at front and back of the list, these functions literally "offer" the facility and named offer(). Three types are available and are discussed in this very article below. 1. offer(E e) : This method adds the specified element as the tail (last
4 min read
Java.util.LinkedList.indexOf(), lastIndexof() in Java
Linked list library also offers to depict the first and last index of element that has to be found using the indexOf() and lastIndexOf() functions respectively. They offer a variety as the direct access is not available in the conventionally made linked list, hence knowledge of it is useful. 1. indexOf(Object o) : This method returns the index of t
4 min read
Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
Java's Linked list class offers a function that allows a "Queue Based" working called poll(). This function not only returns deletes the first element, but also displays them while being deleted and hence can have a lot of usage in daily life problems and competitive programming as well. There are 3 variants of poll(), all three are discussed in th
4 min read
Java.util.LinkedList.get(), getFirst(), getLast() in Java
In Java, the LinkedList class provides several methods for accessing the elements in the list. Here are the methods for getting the elements of a LinkedList: get(int index): This method returns the element at the specified position in the LinkedList. The index parameter is zero-based, so the first element has an index of 0.getFirst(): This method r
5 min read
LinkedList removeFirst() Method in Java
In Java, the LinkedList class provides the removeFirst() method to remove and return the first element of the list. If the list is empty, the method throws a NoSuchElementException. Sure, here is an example of using the removeFirst() method in Java, with the full code and output: Java Code import java.util.LinkedList; public class Example { public
3 min read
LinkedList add() Method in Java With Examples
LinkedList is a class implementation of the LinkedList data structure which is a linear data structure where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part. As we all know that class contains various methods so do here we will be discussing and implementing add() method t
3 min read
LinkedList getFirst() Method in Java
Linked List is a part of the Collection framework present in java.util package. This class is an implementation of the LinkedList data structure which is a linear data structure where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part. The Java.util.LinkedList.getFirst() meth
2 min read
LinkedList getLast() Method in Java
Linked List is a part of the Collection framework present in java.util package. This class is an implementation of the LinkedList data structure which is a linear data structure where the elements are not stored in contiguous locations and every element is a separate object with a data part and. The Java.util.LinkedList.getLast() method is used to
2 min read
LinkedList clear() Method in Java
The Java.util.LinkedList.clear() method is used to remove all the elements from a linked list. Using the clear() method only clears all the element from the list and not deletes the list. In other words we can say that the clear() method is used to only empty an existing LinkedList. Syntax: void clear() Parameters: This function does not accept any
1 min read
LinkedList addFirst() Method in Java
The java.util.LinkedList.addFirst() method in Java is used to insert a specific element at the beginning of a LinkedList. Syntax: void addFirst(Object element) Parameters: This function accepts a single parameter element as shown in the above syntax. The element specified by this parameter is appended at beginning of the list. Return Value: This me
1 min read
LinkedList addLast() Method in Java
The java.util.LinkedList.addLast() method in Java is used to insert a specific element at the end of a LinkedList.Syntax: void addLast(Object element) Parameters: This function accepts a single parameter element as shown in the above syntax. The element specified by this parameter is appended at end of the list.Return Value: This method does not re
1 min read
LinkedList addAll() Method in Java
java.util.LinkedList.addAll(Collection C): This method is used to append all of the elements from the collection passed as parameter to this function to the end of a list keeping in mind the order of return by the collections iterator. Syntax: boolean addAll(Collection C) Parameters: The parameter C is a collection of ArrayList. It is the collectio
3 min read
LinkedList set() Method in Java
The Java.util.LinkedList.set() method is used to replace any particular element in the linked list created using the LinkedList class with another element. This can be done by specifying the position of the element to be replaced and the new element in the parameter of the set() method. Syntax: LinkedList.set(int index, Object element)Parameters: T
3 min read
LinkedList remove() Method in Java
LinkedList as we all know is a way of storing data that contains sets of nodes where each node contains data and address part where address part is responsible for linking of nodes and hence forming a List over which now we can perform operations. Now here we want to remove a node/s using the remove() method of LinkedList class only. Illustration:
4 min read
LinkedList removeLast() Method in Java
In Java, the LinkedList class provides the removeLast() method to remove and return the last element of the list. If the list is empty, the method throws a NoSuchElementException. Example: Java Code import java.util.LinkedList; public class Example { public static void main(String[] args) { LinkedList<String> list = new LinkedList<>();
2 min read
LinkedList get() Method in Java
The Java.util.LinkedList.get() method is used to fetch or retrieve an element at a specific index from a LinkedList. Syntax: LinkedList.get(int index) Parameters: The parameter index is of integer data type that specifies the position or index of the element to be fetched from the LinkedList. Return Value: The method returns the element present at
1 min read
LinkedList size() Method in Java
The Java.util.LinkedList.size() method is used to get the size of the Linked list or the number of elements present in the linked list. Syntax: LinkedList.size()Parameters: This method does not take any parameter. Return Value: This method returns the size or the number of elements present in the LinkedList. Below program illustrates the Java.util.
2 min read
LinkedList indexOf() method in Java
The Java.util.LinkedList.indexOf(Object element) method is used to check and find the occurrence of a particular element in the list. If the element is present then the index of the first occurrence of the element is returned otherwise -1 is returned if the list does not contain the element. Syntax: LinkedList.indexOf(Object element) Parameters: Th
2 min read
LinkedList lastIndexOf() Method in Java with Examples
The lastIndexOf(Object element) method of LinkedList class present inside java.util package is used to check and find the occurrence of a particular element in the list. If the element is present in the list then the lastIndexOf() method returns the index of the last occurrence of the element otherwise it returns -1. This method is used to find the
2 min read
LinkedList contains() Method in Java
The Java.util.LinkedList.contains() method is used to check whether an element is present in a LinkedList or not. It takes the element as a parameter and returns True if the element is present in the list. Syntax: LinkedList.contains(Object element) Parameters: The parameter element is of type LinkedList. This parameter refers to the element whose
2 min read
LinkedList clone() Method in Java
The Java.util.LinkedList.clone() method is used to create a shallow copy of the mentioned linked list. It just creates a copy of the list. Syntax: LinkedList.clone() Parameters: This method does not take any parameters. Return Value: This function returns a copy of the instance of Linked list. Below program illustrate the Java.util.LinkedList.clone
1 min read
LinkedList listIterator() Method in Java
The Java.util.LinkedList.listIterator() method is used to return a list-iterator containing the same elements as that of the LinkedList in proper and same sequence starting from a specific position or index number which is passed as a parameter to this method. Syntax: ListIterator new_list = LinkedList.listIterator(int index); Parameters: The param
1 min read
LinkedList pop() Method in Java
The java.util.LinkedList.pop() method is used to remove and return the top element from the stack represented by the LinkedList. The method simply pops out an element present at the top of the stack. This method is similar to removeFirst method in LinkedList. Syntax: LinkedListObject.pop() Parameters: The method does not take any parameter. Return
2 min read
LinkedList spliterator() method in Java
The spliterator() method of LinkedList returns a Spliterator which is late-binding and fail-fast with the same elements as LinkedList. A late-binding Spliterator binds to the source of elements means LinkedList at the point of first traversal, first split, or first query for estimated size, rather than at the time the Spliterator is created. It can
3 min read
LinkedList descendingIterator() method in Java with Examples
The descendingIterator() method of java.util.LinkedList class is used to return an iterator over the elements in this LinkedList in reverse sequential order. The elements will be returned in order from last (tail) to first (head). Syntax: public Iterator descendingIterator() Return Value: This method returns an iterator over the elements in this Li
2 min read