π Java Data Structures β Master Collections, Lists, and Maps (2025 Guide)
π§² Introduction β Why Learn Java Data Structures?
Java provides a robust Collections Framework to handle, store, and manipulate dynamic groups of objects efficiently. Understanding Java data structures helps in writing scalable, efficient, and organized programs.
π― In this guide, youβll learn:
- What Java data structures are and why they matter
- How to use ArrayList, LinkedList, and HashMap
- How to sort lists, use iterators, and understand wrapper classes
π Topics Covered
π’ Topic | π Description |
---|---|
Java Data Structures | Overview of collection types and utilities |
Java ArrayList | Dynamic arrays with fast access |
Java LinkedList | Node-based list with quick insertions/deletions |
Java List Sorting | Sorting elements using Collections.sort() |
Java HashMap | Key-value pair data structure |
Java Iterator | Sequential access and safe element removal |
Java Wrapper Classes | Converting primitives into objects (e.g., int β Integer ) |
π Java Data Structures β The Foundation
Javaβs data structures allow you to store and organize data in different ways:
- List β Ordered collection
- Set β Unordered unique elements
- Map β Key-value pairs
Weβll focus on List and Map, two of the most commonly used.
π Java ArrayList β Resizable Array
The ArrayList
class provides a dynamic array with fast random access.
π§ͺ Example:
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> colors = new ArrayList<>();
colors.add("Red");
colors.add("Blue");
colors.add("Green");
System.out.println(colors.get(1)); // Output: Blue
}
}
π Line-by-Line Explanation:
ArrayList<String>
β Declares a dynamic list of strings..add(...)
β Adds items to the list..get(1)
β Retrieves element at index 1.
π Java LinkedList β Efficient Insertions
LinkedList
allows faster insertions/removals compared to ArrayList
.
π§ͺ Example:
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
LinkedList<String> items = new LinkedList<>();
items.add("Milk");
items.addFirst("Bread");
items.addLast("Eggs");
System.out.println(items); // Output: [Bread, Milk, Eggs]
}
}
π Line-by-Line Explanation:
addFirst()
β Inserts at the beginning.addLast()
β Inserts at the end.
ποΈ Java List Sorting β Organize Data
Use Collections.sort()
to sort a list of elements.
π§ͺ Example:
import java.util.*;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>(Arrays.asList(5, 2, 8, 1));
Collections.sort(numbers);
System.out.println(numbers); // Output: [1, 2, 5, 8]
}
}
π Line-by-Line Explanation:
Arrays.asList(...)
β Creates list from array.Collections.sort(...)
β Sorts the list in ascending order.
π§ Java HashMap β Key-Value Store
Use HashMap
when you want to store data with unique keys.
π§ͺ Example:
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<String, Integer> ageMap = new HashMap<>();
ageMap.put("Alice", 28);
ageMap.put("Bob", 35);
System.out.println(ageMap.get("Bob")); // Output: 35
}
}
π Line-by-Line Explanation:
.put(key, value)
β Inserts key-value pair..get(key)
β Retrieves value using the key.
π Java Iterator β Traverse Collections Safely
Iterators allow sequential access and element removal during iteration.
π§ͺ Example:
import java.util.*;
public class Main {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>(List.of("Apple", "Banana", "Cherry"));
Iterator<String> it = fruits.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}
π Line-by-Line Explanation:
.iterator()
β Creates an iterator object..hasNext()
β Checks for more elements..next()
β Retrieves next element.
π§± Java Wrapper Classes β Primitives as Objects
Wrapper classes encapsulate primitive types into objects.
Primitive | Wrapper Class |
---|---|
int | Integer |
char | Character |
boolean | Boolean |
π§ͺ Example:
public class Main {
public static void main(String[] args) {
int num = 10;
Integer wrappedNum = num; // Autoboxing
System.out.println(wrappedNum);
}
}
π Line-by-Line Explanation:
Integer wrappedNum = num;
β Converts primitive to object automatically.
π Summary β Recap & Next Steps
Understanding and applying data structures is essential for real-world Java programming.
π Key Takeaways:
ArrayList
andLinkedList
manage dynamic lists efficientlyHashMap
offers quick key-based access to dataIterator
lets you loop and remove safelyWrapper Classes
let primitives act like objectsCollections.sort()
helps organize your list data
βοΈ Whatβs Next?
- Explore
HashSet
,TreeMap
, andPriorityQueue
- Learn custom sorting with
Comparator
- Build CRUD apps using
ArrayList
andHashMap
β Frequently Asked Questions (FAQs)
β When should I use ArrayList
vs LinkedList
?
β
Use ArrayList
for fast access and LinkedList
for frequent insertions/deletions.
β Can I store primitive data types in collections?
β No. Use wrapper classes like Integer
, Double
, etc., since collections store objects.
β Is HashMap
ordered?
β No. Use LinkedHashMap
or TreeMap
if you need insertion or sorted order.
β How do I remove elements while iterating a list?
β
Use an iterator and its remove()
method to safely remove elements.
β Whatβs the difference between autoboxing and unboxing?
β
Autoboxing converts primitives to objects.
β
Unboxing converts objects back to primitives.
Share Now :