Java Data Structures
In this page:
What is a Data Structure?
A data structure is a way of organizing and storing data so it can be accessed and modified efficiently, ranging from a simple fixed-size array to more flexible structures like lists, sets, and maps that grow and shrink at runtime.
Example: What is a Data Structure?
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> numbers = new ArrayList<>(); // flexible structure vs fixed array
numbers.add(1);
numbers.add(2);
System.out.println(numbers);
}
}
Login to try C/C++/Java/PHP code in the editor
Linear vs Non-Linear Structures
Linear data structures like arrays, ArrayList, and LinkedList store elements in a single sequential order, while non-linear structures like HashMap and trees organize data by relationships, such as a key mapping to a value, rather than by position.
Example: Linear vs Non-Linear Structures
import java.util.ArrayList;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>(); // linear: sequential order
list.add("a");
HashMap<String, Integer> map = new HashMap<>(); // non-linear: relationships
map.put("a", 1);
System.out.println(list);
System.out.println(map);
}
}
Login to try C/C++/Java/PHP code in the editor
Java's Collections Framework
Java's Collections Framework provides a set of standard interfaces -- List, Set, Map, and Queue -- along with multiple ready-made implementations of each, so code can be written against the general interface rather than one specific implementation.
Example: Java's Collections Framework
import java.util.List;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
List<String> names = new ArrayList<>(); // coded against the List interface
names.add("Alice");
System.out.println(names);
}
}
Login to try C/C++/Java/PHP code in the editor
Choosing the Right Structure
Choosing the right data structure depends on how the data will be used: a List suits ordered data accessed by position, a Set suits data that must never contain duplicates, and a Map suits data that needs to be looked up by a unique key.
Example: Choosing the Right Structure
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
HashSet<String> uniqueNames = new HashSet<>(); // Set: no duplicates allowed
uniqueNames.add("Alice");
uniqueNames.add("Alice");
System.out.println(uniqueNames.size());
}
}
Login to try C/C++/Java/PHP code in the editor
Big O Intuition
Big O notation describes how a structure's performance changes as the amount of data grows -- accessing an ArrayList by index stays constant time no matter its size, while accessing a LinkedList by index gets slower as more elements are added.
Example: Big O Intuition
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
System.out.println(list.get(0)); // constant time, regardless of list size
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- Java Data Structures
- Java Collections Introduction
- Java List Interface
- Java ArrayList
- Java LinkedList
- Java Map Interface
- Java HashMap
- Java Set Interface
- Java TreeMap
- Java LinkedHashMap
- Java HashSet
- Java TreeSet
- Java LinkedHashSet
- Java Stack & Queue
- Java Iterator
- Java Collections Class
- Java List Sorting