Java Collections Framework:LinkedList-2.1(Add/Remove)
Java – Remove element from a specific index in LinkedList example
In this example, we are gonna see how to remove an element from LinkedList.
Example
We will be using remove(int index) method of LinkedList class to remove an element from a specific index. Method definition and description are as follows:
public E remove(int index)
: Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.
import java.util.LinkedList; public class RemoveFromLinkedList { public static void main(String[] args) { // Create a LinkedList LinkedList<String> linkedlist = new LinkedList<String>(); // Add elements to LinkedList linkedlist.add("Cobol"); linkedlist.add("JCL"); linkedlist.add("C++"); linkedlist.add("C#"); linkedlist.add("Java"); // Displaying Elements before replace System.out.println("LinkedList Elements:"); for(String str: linkedlist){ System.out.println(str); } // Removing 3rd element Object e1 = linkedlist.remove(2); System.out.println("\nElement "+ e1+ " removed from the list\n"); // LinkedList elements after remove System.out.println("After removal:"); for(String str2: linkedlist){ System.out.println(str2); } } }
Output:
LinkedList Elements: Cobol JCL C++ C# Java Element C++ removed from the list After removal: Cobol JCL C# Java
Java – Remove specific elements from LinkedList example
Example
We will be using remove(Object o) method to perform this remove. More about this method is as follows:
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).
import java.util.LinkedList;
public class RemoveExample {
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");
// LinkedList elements after remove
System.out.println("\nAfter Remove:");
for(String str2: linkedlist){
System.out.println(str2);
}
}
}
Output:
Before Remove:
Item1
Item2
Item3
Item4
Item5
After Remove:
Item1
Item2
Item3
Item5
Java – Remove all elements from LinkedList example
In this example we will see how to remove all the elements from LinkedList. We will be using clear() method of LinkedList class to do this. Method definition and description are as follows:
public void clear()
: Removes all of the elements from this list. The list will be empty after this call returns.
Example
In this example we have a LinkedList of String type that has 5 elements. We are calling clear() method and displaying the LinkedList elements before and after calling the clear() method.
import java.util.LinkedList;
public class RemoveAllExample {
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 clear():");
for(String str: linkedlist){
System.out.println(str);
}
// Removing all the elements from LinkedList
linkedlist.clear();
// LinkedList elements after remove
System.out.println("After clear():");
for(String str2: linkedlist){
System.out.println(str2);
}
}
}
Output:
Before clear():
Item1
Item2
Item3
Item4
Item5
After clear():
Append all the elements of a List to LinkedList – Java
Description
Program to add all the elements of a List to the LinkedList using addAll() method of LinkedList class.
Example
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
class LinkedListAddAll {
public static void main(String[] args) {
// create a LinkedList
LinkedList<String> list = new LinkedList<String>();
// Add elements to the LinkedList
list.add("AA");
list.add("BB");
list.add("CC");
list.add("DD");
// Displaying linked list before add
System.out.println("Before: LinkedList: " + list);
// create a new list having few elements
List<String> arrayList = new ArrayList<String>();
arrayList.add("Item1");
arrayList.add("Item2");
arrayList.add("Item3");
// Append the list elements to LinkedList
list.addAll(arrayList);
// Displaying the LinkedList after addAll
System.out.println("After: LinkedList: " + list);
}
}
Output:
Before: LinkedList: [AA, BB, CC, DD]
After: LinkedList: [AA, BB, CC, DD, Item1, Item2, Item3]
addAll() method:
public boolean addAll(Collection<? extends E> c)
: Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection’s iterator. Source: addAll() method – Javadoc.
Comments
Post a Comment