Remove Specific Elements From LinkedList In Java
Chapter:
Data Structures
Last Updated:
28-08-2016 19:28:35 UTC
Program:
/* ............... START ............... */
import java.util.LinkedList;
public class JavaRemoveSpecificElement {
public static void main(String[] args) {
// Create a LinkedList
LinkedList<String> linkedlist = new LinkedList<String>();
// Add elements to LinkedList
linkedlist.add("Item1");
linkedlist.add("Item2");
linkedlist.add("Item3");
linkedlist.add("Item4");
linkedlist.add("Item5");
// Displaying Elements before remove
System.out.println("Before Remove:");
for (String str : linkedlist) {
System.out.println(str);
}
// Removing "Item4" from the list
linkedlist.remove("Item4");
System.out.println("\nAfter Remove:");
for (String str2 : linkedlist) {
System.out.println(str2);
}
}
}
/* ............... END ............... */
Output
Before Remove:
Item1
Item2
Item3
Item4
Item5
After Remove:
Item1
Item2
Item3
Item5
Notes:
-
remove(Object o) method to perform this remove.
- public boolean remove(Object o): Removes the first occurrence of the specified element from this list, if it is present. If this list does not contain the element, it is unchanged. Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).
Tags
Remove Specific Elements From LinkedList, Java, Data Structures