Data structures are fundamental concepts in programming that help organize, store, and manage data efficiently. In Java, data structures are implemented using the Collections Framework, which provides a set of interfaces and classes to handle different types of data.
This guide explores the most commonly used Java data structures, their characteristics, and when to use them.
Types of Data Structures in Java
Data structures in Java can be broadly categorized into two types:
-
Primitive Data Structures
-
Basic data types like
int
,char
,float
,boolean
, etc. -
Stored directly in memory.
-
-
Non-Primitive (Object-Based) Data Structures
-
Built using classes and interfaces.
-
Divided into:
-
Linear Data Structures (sequential arrangement)
-
Non-Linear Data Structures (hierarchical or networked arrangement)
-
-
Linear Data Structures in Java
1. Arrays
-
Fixed-size, contiguous memory allocation.
-
Stores elements of the same type.
-
Fast access via index, but resizing is inefficient.
int[] numbers = {1, 2, 3, 4, 5};
2. ArrayList (Dynamic Array)
-
Resizable array implementation (
List
interface). -
Allows duplicates and maintains insertion order.
-
Slower for insertions/deletions in the middle.
ArrayList<String> list = new ArrayList<>(); list.add("Java"); list.add("Python");
3. LinkedList
-
Doubly-linked list implementation (
List
&Deque
interfaces). -
Faster insertions/deletions than
ArrayList
but slower random access.
LinkedList<Integer> linkedList = new LinkedList<>(); linkedList.add(10); linkedList.addFirst(5); // Adds to the beginning
4. Stack (LIFO – Last In, First Out)
-
Uses
push()
,pop()
, andpeek()
operations. -
Used in recursion, undo operations, and parsing.
Stack<String> stack = new Stack<>(); stack.push("A"); stack.push("B"); String top = stack.pop(); // Returns "B"
5. Queue (FIFO – First In, First Out)
-
Implemented via
LinkedList
orPriorityQueue
. -
Used in scheduling, BFS algorithms, and task management.
Queue<String> queue = new LinkedList<>(); queue.add("Task1"); queue.add("Task2"); String next = queue.poll(); // Removes & returns "Task1"
6. PriorityQueue
-
Elements are processed based on priority (natural ordering or custom
Comparator
). -
Used in Dijkstra’s algorithm, job scheduling.
PriorityQueue<Integer> pq = new PriorityQueue<>(); pq.add(30); pq.add(10); // 10 will be polled first (default min-heap)
Non-Linear Data Structures in Java
1. HashMap (Hash Table Implementation)
-
Stores key-value pairs (
O(1)
average time complexity forget
&put
). -
No duplicate keys; allows
null
keys/values.
HashMap<String, Integer> map = new HashMap<>(); map.put("Alice", 25); map.put("Bob", 30); int age = map.get("Alice"); // 25
2. TreeMap (Red-Black Tree Implementation)
-
Sorted key-value pairs (natural ordering or custom
Comparator
). -
Slower than
HashMap
(O(log n)
operations).
TreeMap<Integer, String> treeMap = new TreeMap<>(); treeMap.put(3, "Three"); treeMap.put(1, "One"); // Sorted as {1=One, 3=Three}
3. HashSet (Unordered Unique Elements)
-
Backed by
HashMap
, stores only unique values. -
Fast lookups (
O(1)
average time).
HashSet<String> set = new HashSet<>(); set.add("Apple"); set.add("Banana"); boolean exists = set.contains("Apple"); // true
4. TreeSet (Sorted Unique Elements)
-
Implements
SortedSet
(elements stored in sorted order). -
Slower than
HashSet
(O(log n)
operations).
TreeSet<Integer> treeSet = new TreeSet<>(); treeSet.add(5); treeSet.add(2); // Stored as [2, 5]
5. Graph (Adjacency List or Matrix)
-
Represents nodes (vertices) and edges.
-
Implemented using
HashMap
or custom classes.
Map<Integer, List<Integer>> graph = new HashMap<>(); graph.put(1, Arrays.asList(2, 3)); // Node 1 connected to 2 & 3
When to Use Which Data Structure?
Use Case | Best Data Structure |
---|---|
Fast access by index | Array , ArrayList |
Frequent insertions/deletions | LinkedList |
LIFO operations (undo, DFS) | Stack |
FIFO operations (BFS, tasks) | Queue , LinkedList |
Priority-based processing | PriorityQueue |
Key-value storage | HashMap (unsorted), TreeMap (sorted) |
Unique elements storage | HashSet (unsorted), TreeSet (sorted) |
Hierarchical data | Tree , Graph |
Conclusion
Java provides a rich set of built-in data structures through the Collections Framework. Choosing the right one depends on:
-
Access patterns (random vs sequential)
-
Insertion/Deletion frequency
-
Memory usage
-
Ordering requirements
By understanding these structures, you can optimize performance in algorithms, database operations, and real-world applications.
Further Learning:
-
Java
Collections
API documentation -
Algorithm books (e.g., Introduction to Algorithms by Cormen)
-
Practice on coding platforms (LeetCode, HackerRank)
Visit our website: