LinkedList boolean removeFirstOccurrence(Object o) method Example Program
By candid | Posted :
Jun 30, 2015
| Updated :
Jun 30, 2015
Removes the first occurrence of the specified element in this list (when traversing the list from head to tail). If the list does not contain the element, it is unchanged.
Program
package com.candidjava;
import java.util.LinkedList;
/**
*
* @author : mohan
* @description :The java.util.LinkedList.removeFirstOccurrence(Object o) method
* removes the first occurrence of the specified element in this
* list (when traversing the list from head to tail). If the list
* does not contain the element, it is unchanged.
*/
public class LinkedListRemovFirstOccur {
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.add("anandh");
list.add("hari");
list.add("vinoth");
list.add("mohan");
list.add("kamal");
System.out.println("LinkedList:" + list);
boolean found = list.removeFirstOccurrence("anandh");
System.out.println("Element found:" + found);
System.out.println("LinkedList:" + list);
}
}
Output
LinkedList:[anandh, hari, vinoth, mohan, kamal]
Element found:true
LinkedList:[hari, vinoth, mohan, kamal]
Explanation
public boolean removeFirstOccurrence(Object o)
Removes the first occurrence of the specified element in this list (when traversing the list from head to tail). If the list does not contain the element, it is unchanged.
Specified by:
removeFirstOccurrence in interface Deque<E>
Parameters:
o - element to be removed from this list, if present