The LinkedList class in Java is a collection class that is used to implement the linked list data structure. A linked list is a data structure that consists of a series of nodes, each of which contains a value and a reference to the next node in the sequence. In this article, we will discuss the LinkedList class in Java in detail.
What is LinkedList in Java?
The LinkedList class in Java is a part of the java.util package, and it is used to implement the linked list data structure. The LinkedList class is a doubly-linked list, which means that each node in the list contains a reference to both the next node and the previous node in the sequence.
LinkedList Syntax:
The syntax for creating a LinkedList in Java is as follows:
LinkedList linkedListName = new LinkedList();
In this syntax, “DataType” refers to the data type of the elements that will be stored in the LinkedList, and “linkedListName” is the name of the LinkedList.
LinkedList Methods:
The LinkedList class provides a set of methods that can be used to manipulate the elements in the linked list. Here are some of the most commonly used methods:
- add(element): This method is used to add an element to the end of the linked list.
- addFirst(element): This method is used to add an element to the beginning of the linked list.
- addLast(element): This method is used to add an element to the end of the linked list.
- remove(): This method is used to remove the first element from the linked list.
- removeFirst(): This method is used to remove the first element from the linked list.
- removeLast(): This method is used to remove the last element from the linked list.
- get(index): This method is used to retrieve the element at the specified index in the linked list.
- size(): This method is used to get the size of the linked list.
Example of LinkedList in Java:
Here is an example of how to use a LinkedList in Java:
import java.util.LinkedList;
public class LinkedListExample {
public static void main(String[] args) {
LinkedList linkedList = new LinkedList();
linkedList.add("apple");
linkedList.add("banana");
linkedList.add("orange");
System.out.println(linkedList);
linkedList.removeFirst();
System.out.println(linkedList);
linkedList.addFirst("grape");
System.out.println(linkedList);
System.out.println(linkedList.get(1));
System.out.println(linkedList.size());
}
}
In this example, we first import the LinkedList class from the java.util package. We then create a new LinkedList called “linkedList” that can hold strings. We add three elements to the LinkedList. We then print out the entire linked list, which is “apple”, “banana”, and “orange”. We then remove the first element from the linked list, which is “apple”, and print out the updated linked list. We then add “grape” to the beginning of the linked list and print out the updated linked list. We then retrieve the element at index 1, which is “banana”, and print it out. Finally, we print out the size of the linked list, which is 3.
Conclusion:
In conclusion, the LinkedList class in Java is a useful data structure that is used to implement the linked list data structure. It provides a set of methods that can be used to manipulate the elements in the linked list. The LinkedList class is useful in various scenarios, such as in algorithms that require dynamic resizing and fast insertions and deletions.