Java LinkedList is a data structure that represents a sequence of elements. It is a part of the Java Collections Framework and is implemented in the java.util package. LinkedList is a dynamic data structure that can grow or shrink as per the requirement. It is similar to an array, but it provides more flexibility in terms of adding or removing elements.
The LinkedList class in Java provides a doubly linked list implementation. It means that each element in the list has a reference to the previous and next element in the list. This makes it easy to add or remove elements from the list without affecting the other elements.
Let's take a look at some of the methods provided by the LinkedList class:
add(E e)
: Adds the specified element to the end of the list.add(int index, E element)
: Inserts the specified element at the specified position in the list.remove()
: Removes and returns the first element from the list.remove(int index)
: Removes and returns the element at the specified position in the list.get(int index)
: Returns the element at the specified position in the list.size()
: Returns the number of elements in the list.Let's see some code examples to understand how to use LinkedList in Java:
Example 1: Creating a LinkedList and adding elements to it.
import java.util.LinkedList;
public class LinkedListExample {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("John");
list.add("Mary");
list.add("Peter");
System.out.println(list);
}
}
Output:
[John, Mary, Peter]
Example 2: Removing an element from the LinkedList.
import java.util.LinkedList;
public class LinkedListExample {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("John");
list.add("Mary");
list.add("Peter");
list.remove("Mary");
System.out.println(list);
}
}
Output:
[John, Peter]
Example 3: Retrieving an element from the LinkedList.
import java.util.LinkedList;
public class LinkedListExample {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("John");
list.add("Mary");
list.add("Peter");
String element = list.get(1);
System.out.println(element);
}
}
Output:
Mary
Example 4: Iterating over the elements of the LinkedList.
import java.util.LinkedList;
public class LinkedListExample {
public static void main(String[] args) {
LinkedList<String> list = new LinkedList<>();
list.add("John");
list.add("Mary");
list.add("Peter");
for(String element : list) {
System.out.println(element);
}
}
}
Output:
John
Mary
Peter
These are just a few examples of how to use LinkedList in Java. There are many other methods provided by the LinkedList class that you can use as per your requirement.
Java LinkedList is a powerful data structure that provides flexibility in terms of adding or removing elements. It is implemented in the java.util package and provides a doubly linked list implementation. You can use LinkedList in Java to represent a sequence of elements and perform various operations on it.