← Back to Core Java Course | Chapter 11: Collections | Lesson 1 of 17

Java Data Structures

A data structure organizes and stores data so it can be accessed and modified efficiently, and Java's built-in options range from a simple fixed-size array to the flexible List, Set, and Map types in the Collections Framework.

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?

java
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);
	}
}

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

java
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);
	}
}

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

java
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);
	}
}

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

java
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());
	}
}

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

java
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 run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.