What is Data Structure in Java & Classification (Tutorial)
A data structure is simply a way of organizing and storing data so it can be accessed and modified efficiently. In Java, choosing the right data structure often has a bigger impact on performance than any other single coding decision.
Classification of Data Structures
- Linear Data Structures: elements are arranged sequentially — arrays, linked lists, stacks, and queues
- Non-Linear Data Structures: elements are connected in a hierarchical or networked way — trees and graphs
- Primitive Structures: the basic building blocks like int, char, float, and boolean
- Non-Primitive Structures: built using primitives — arrays, classes, and collections
Why This Matters in Java
Java's built-in Collection Framework (covered later in this course) is essentially a rich set of ready-made data structures. Understanding the underlying classification helps you pick the right one — for example, a LinkedList for frequent insertions versus an ArrayList for fast random access.
Master Java with Uncodemy
Hands-on training, live projects, and placement support in our Java Programming Course.
Mapping the Classification to Java Classes
Each category in the classification has ready-made classes in java.util. Knowing the mapping saves you from writing structures yourself.
| Structure | Java class to reach for | Category |
|---|---|---|
| Dynamic array | ArrayList | Linear |
| Linked list | LinkedList | Linear |
| Stack | ArrayDeque (push/pop) | Linear |
| Queue | ArrayDeque or LinkedList | Linear |
| Priority queue (heap) | PriorityQueue | Non-linear (tree-based) |
| Sorted map / set | TreeMap, TreeSet | Non-linear (red-black tree) |
| Hash table | HashMap, HashSet | Non-linear (buckets) |
Graphs have no dedicated class, so they are usually built as a Map<Integer, List<Integer>> adjacency list.
A second way to classify structures is static versus dynamic. A plain Java array is static: its length is fixed when you create it. Collections such as ArrayList are dynamic and grow on their own, which is why they are the default choice when you cannot predict the size in advance.
Typical lookup costs also differ. Reading by index from an ArrayList is constant time, a HashMap lookup is constant time on average, a TreeMap lookup is logarithmic, and searching a LinkedList is linear because it must walk node by node.
Seeing Several Structures in One Program
import java.util.*;
public class StructuresDemo {
public static void main(String[] args) {
List<String> names = new ArrayList<>(List.of("Asha", "Ravi"));
names.add("Meena"); // fast append
Deque<Integer> stack = new ArrayDeque<>();
stack.push(1); stack.push(2);
System.out.println(stack.pop()); // 2 (last in, first out)
Queue<String> queue = new ArrayDeque<>();
queue.offer("job1"); queue.offer("job2");
System.out.println(queue.poll()); // job1 (first in, first out)
Map<String, Integer> marks = new HashMap<>();
marks.put("Asha", 88);
System.out.println(marks.get("Asha")); // 88
Map<Integer, List<Integer>> graph = new HashMap<>();
graph.computeIfAbsent(1, k -> new ArrayList<>()).add(2);
System.out.println(graph); // {1=[2]}
}
}
Notice how each structure enforces a different access rule: by index, last-in-first-out, first-in-first-out, by key, or by connection.
Picking a Structure Step by Step
- Ask how you will read the data: by position, by key, in sorted order, or always the smallest item first.
- Ask how you will change it: mostly appends, inserts in the middle, or removals from both ends.
- Check uniqueness: if duplicates are not allowed, a
Setdoes the job automatically. - Check ordering:
HashMaphas no guaranteed order,LinkedHashMapkeeps insertion order,TreeMapkeeps keys sorted. - Estimate size: for very large data, the cost of lookups and memory per element matters most.
For example, a leaderboard that must always show players by score fits a TreeMap or PriorityQueue, while a shopping cart with frequent "get item 3" calls fits an ArrayList.
Beginner Mistakes to Avoid
- Using the legacy
StackandVectorclasses in new code;ArrayDequeandArrayListare the usual modern choices. - Calling
get(i)in a loop on aLinkedList, which walks the list every time and becomes slow. - Using a mutable object as a
HashMapkey and changing it later, which makes the entry impossible to find. - Declaring variables with the concrete class instead of the interface; prefer
List<String> list = new ArrayList<>()so you can swap implementations easily.