Understanding LinkedList with Example Code
Table of Contents
- Introduction
- Overview of LinkedList in Java
- Understanding the Code
- Example Code Explanation
- Key Takeaways and Conclusion
1. Introduction
In Java programming, collections play a crucial role in handling dynamic data storage. Among these, the LinkedList is a fundamental data structure, which is a part of the Java Collections Framework. This article will dive into the working, advantages, and usage of LinkedList in Java, with a practical example to help you understand its implementation.
This guide is tailored for beginner developers with basic knowledge of Java, looking to deepen their understanding of LinkedList and its operations. We will explain the key concepts, advantages, and step-by-step breakdown of the code used to implement a LinkedList.
2. Overview of LinkedList in Java
A LinkedList in Java is a type of list where elements are stored in nodes, with each node containing the reference to the next node. This structure allows for efficient insertion and deletion of elements, especially in cases where the data size may change frequently.
Pros:
- Dynamic size: Unlike arrays, LinkedList can grow or shrink dynamically.
- Efficient insertion/deletion: Adding/removing elements from the list is faster compared to an array since no shifting is required.
Cons:
- Slower access time: Traversing the LinkedList to access elements is slower because it requires sequential access.
Comparison Table: LinkedList vs ArrayList
Feature | LinkedList | ArrayList |
---|---|---|
Memory allocation | Dynamic | Fixed |
Insertion time | Fast for insertions | Slower due to shifting |
Access time | Slower, sequential | Faster, random access |
3. Understanding the Code
Below is the Java code provided in the project, demonstrating how to use a LinkedList to store different types of elements:
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 26 27 28 29 30 31 32 33 |
package org.studyeasy; import java.util.LinkedList; import java.util.List; class Name { private String name; public Name(String name) { this.name = name; } @Override public String toString() { return name; } } public class Main { public static void main(String[] args) { List<Object> elements = new LinkedList<>(); elements.add(new Name("Chaand")); elements.add(new Name("Ed")); elements.add(new Name("John")); elements.add(new Name("Mia")); elements.add("Hello"); elements.add(25); elements.add(52.22521); System.out.println(elements); } } |
4. Example Code Explanation
In this example, we create a LinkedList named elements that stores objects of different types: custom Name
objects, strings, integers, and floating-point numbers.
- Creating the Name class: The Name class is a simple Java class with a single property,
name
, and a constructor to initialize this property. The toString() method is overridden to return the name when the object is printed. - Adding elements to the LinkedList: In the main() method, we declare a LinkedList named elements and use the add() method to insert different types of data:
- elements.add(new Name(“Chaand”)): Adds a Name object with the name “Chaand“.
- elements.add(“Hello”): Adds a string.
- elements.add(25): Adds an integer.
- elements.add(52.22521): Adds a floating-point number.
- Printing the elements: The System.out.println(elements) statement prints all the elements in the list. Since the toString() method is overridden in the Name class, it prints the name directly instead of the object reference.
Output:
1 |
[Chaand, Ed, John, Mia, Hello, 25, 52.22521] |
This output shows that the LinkedList can store heterogeneous elements and print them in the order they were added.
5. Key Takeaways and Conclusion
The LinkedList class in Java is versatile and can store various types of data, making it suitable for scenarios where the size of the collection can change frequently. It offers efficient insertion and deletion operations, but accessing elements sequentially may be slower compared to other collections like ArrayList.
In this article, we have explored:
- How to create and use a LinkedList in Java.
- Adding and printing heterogeneous elements.
- Step-by-step code explanation.