Understanding LinkedList Operations in Java
Table of Contents
- Introduction
- Understanding LinkedList in Java
- LinkedList vs ArrayList
- LinkedList Methods Explained
- Example Code and Explanation
- Conclusion
Introduction
In this article, we will explore the LinkedList class in Java, a fundamental data structure that is part of the Java Collections Framework. LinkedLists provide a dynamic structure that efficiently handles operations such as inserting and deleting elements. While arrays in Java have fixed sizes, LinkedLists offer flexibility, making them an excellent choice in situations where dynamic data manipulation is needed.
This guide is aimed at Java developers who are familiar with basic programming concepts and are looking to dive deeper into collections like LinkedList.
Understanding LinkedList in Java
What is LinkedList?
A LinkedList is a doubly linked list that implements both the List
and Deque
interfaces in Java. It allows for the insertion and deletion of elements from both ends of the list. Unlike an ArrayList
, a LinkedList
allocates space dynamically, making it more efficient for frequently inserting and removing elements from the list.
Key Methods of LinkedList
Method | Description |
---|---|
add() |
Inserts elements into the LinkedList. |
get() |
Retrieves an element from a specified index. |
remove() |
Removes an element from the LinkedList. |
printList() |
Prints all elements of the LinkedList. |
LinkedList vs ArrayList
While both LinkedList
and ArrayList
implement the List
interface, they serve different purposes due to their underlying structure. Here’s a comparison between the two:
Feature | LinkedList | ArrayList |
---|---|---|
Internal Structure | Doubly Linked List | Dynamic Array |
Element Access | Slower (O(n)) for accessing random elements | Faster (O(1)) for accessing random elements |
Insertion/Deletion | Faster (O(1)) for inserting/deleting elements at the ends | Slower (O(n)) for inserting/deleting elements in the middle |
Use Case | Best for dynamic insertions and deletions | Best for frequent read operations |
LinkedList Methods Explained
add()
The add()
method inserts elements into the LinkedList. It appends the element at the end of the list by default.
1 2 3 |
list1.add("Chaand"); list1.add("Jai"); list1.add("Veeru"); |
Here, three elements are added to the LinkedList
.
get()
The get()
method retrieves an element from the specified index. However, in the example code provided, this method is not explicitly used.
remove()
The remove()
method deletes an element from the list. It can remove the element by index or by object value.
printList()
The printList()
method, as implemented in the provided code, prints all the elements in a List
:
1 2 3 4 5 |
void printList(List<String> list){ for (String name : list) { System.out.println(name); } } |
This method iterates through the list and prints each element to the console.
Example Code and Explanation
Let’s break down the provided Java code, which demonstrates the use of both LinkedList
and ArrayList
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package org.studyeasy; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class Main { public static void main(String[] args) { LinkedList<String> list1 = new LinkedList<>(); list1.add("Chaand"); list1.add("Jai"); list1.add("Veeru"); ArrayList<String> list2 = new ArrayList<>(); list2.add("StudyEasy"); list2.add("organization"); list2.add("team"); new Main().printList(list2); } void printList(List<String> list) { for (String name : list) { System.out.println(name); } } } |
Code Breakdown
- Creating a LinkedList:
1LinkedList<String> list1 = new LinkedList<>();
This creates an emptyLinkedList
that stores strings. - Adding Elements to the LinkedList:
123list1.add("Chaand");list1.add("Jai");list1.add("Veeru");
These lines add three elements to the LinkedList. - Creating and Populating an ArrayList:
1234ArrayList<String> list2 = new ArrayList<>();list2.add("StudyEasy");list2.add("organization");list2.add("team");
Similarly, anArrayList
is created and three elements are added to it. - Printing the List:
1new Main().printList(list2);
This line calls theprintList()
method, which prints the contents oflist2
(theArrayList
).
Output:
1 2 3 |
StudyEasy organization team |
Conclusion
LinkedLists are a crucial part of the Java Collections Framework, offering flexibility in dynamic data manipulation. The primary advantage of LinkedLists is their efficiency in adding and removing elements compared to ArrayList
. While both structures have their strengths, it’s important to choose the right one based on your specific use case.
Understanding these key methods will help you master the use of LinkedList in Java and use them effectively in your applications.