LinkedList is another data structure in Java that implements a list using a doubly linked list. A LinkedList is similar to an ArrayList in that it stores a collection of elements, but it differs in the way it is implemented. LinkedList provides a flexible and efficient way to manipulate a list of elements.
The LinkedList class in Java provides a flexible and efficient way to manipulate a list of elements.
The LinkedList class has several constructors that allow you to create a LinkedList object. Here are some of the constructors:
1. LinkedList(): This constructor creates an empty LinkedList.
2. LinkedList(Collection<? extends E> c): This constructor creates a LinkedList containing the elements of the specified collection, in the order they are returned by the collection's iterator.
LinkedList provides several methods to add, remove, retrieve, and manipulate elements in the list. It also provides methods to search, sort, and perform other operations on the elements of the list. The methods of LinkedList are similar to those of ArrayList.
Here are some of the methods of the LinkedList class in Java:
1. add(E element): This method is used to add an element to the end of the LinkedList.
2. add(int index, E element): This method is used to add an element at a specific index in the LinkedList.
3. remove(Object o): This method is used to remove the first occurrence of the specified element from the LinkedList.
4. remove(int index): This method is used to remove the element at the specified index from the LinkedList.
5. get(int index): This method is used to retrieve the element at the specified index from the LinkedList.
6. set(int index, E element): This method is used to replace the element at the specified index with the specified element.
7. size(): This method is used to return the number of elements in the LinkedList.
8. indexOf(Object o): This method is used to return the index of the first occurrence of the specified element in the LinkedList.
9. clear(): This method is used to remove all the elements from the LinkedList.
10. sort(Comparator<? super E> c): This method is used to sort the elements of the LinkedList using the specified Comparator.
Here's an example that demonstrates how to use LinkedList and its methods in Java:
import java.util.LinkedList;
public class LinkedListExample {
public static void main(String[] args) {
// Creating a LinkedList
LinkedList<String> list = new LinkedList<>();
// Adding elements to the LinkedList using add() method
list.add("apple");
list.add("banana");
list.add("cherry");
list.add("date");
list.add("elderberry");
// Printing the LinkedList
System.out.println("Original LinkedList: " + list);
// Adding an element at a specific index using add() method
list.add(2, "grape");
// Printing the updated LinkedList
System.out.println("LinkedList after adding 'grape' at index 2: " + list);
// Removing an element using remove() method
list.remove("date");
// Printing the updated LinkedList
System.out.println("LinkedList after removing 'date': " + list);
// Retrieving an element using get() method
String fruit = list.get(3);
System.out.println("Element at index 3: " + fruit);
// Replacing an element using set() method
list.set(4, "fig");
// Printing the updated LinkedList
System.out.println("LinkedList after replacing 'elderberry' with 'fig': " + list);
// Sorting the LinkedList using sort() method
list.sort(String.CASE_INSENSITIVE_ORDER);
// Printing the sorted LinkedList
System.out.println("Sorted LinkedList: " + list);
// Removing all the elements from the LinkedList using clear() method
list.clear();
// Printing the empty LinkedList
System.out.println("LinkedList after clearing all elements: " + list);
}
}
Output:
Original LinkedList: [apple, banana, cherry, date, elderberry]
LinkedList after adding 'grape' at index 2: [apple, banana, grape, cherry, date, elderberry]
LinkedList after removing 'date': [apple, banana, grape, cherry, elderberry]
Element at index 3: cherry
LinkedList after replacing 'elderberry' with 'fig': [apple, banana, grape, cherry, fig]
Sorted LinkedList: [apple, banana, cherry, fig, grape]
LinkedList after clearing all elements: []
In this example, we created a LinkedList and added elements to it using the add() method. We also demonstrated how to add an element at a specific index using add() method, remove an element using remove() method, retrieve an element using get() method, and replace an element using set() method.
We also showed how to sort the LinkedList using sort() method and clear all the elements from the LinkedList using clear() method.
Overall, the LinkedList class provides a powerful way to manipulate a list of elements in Java.