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

Java Collections Introduction

The Collections Framework is a ready-made set of containers for groups of things, like lists, sets and maps. It is like a store of different boxes for different storage needs.

What is the Collections Framework?

The Collections Framework is Java's standardized set of interfaces and classes — List, Set, Map, and their implementations — for storing and manipulating groups of objects, replacing the need to hand-roll your own data structures.

Example: What is the 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<>(); // standardized interface + implementation
		names.add("Alice");
		System.out.println(names);
	}
}

Core Interfaces

List, Set, and Queue are the three core collection interfaces, each defining different behavior around ordering and duplicates; Map sits alongside them, storing key-value pairs rather than a flat sequence of elements.

Example: Core Interfaces

java
// Import java.util.List so it can be used by its short name
import java.util.List;
// Import java.util.Set so it can be used by its short name
import java.util.Set;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.HashMap;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new ArrayList object and store it in list
		List<String> list = new ArrayList<>();
		// Create a new HashSet object and store it in set
		Set<String> set = new HashSet<>();
		Map<String, Integer> map = new HashMap<>();
		// Print a line to the console
		System.out.println(list.size() + " " + set.size() + " " + map.size());
	}
}

Primitives vs Objects

Collections only store objects, not primitives, so an int must be autoboxed into an Integer before it can go into a List<Integer> — this has real performance implications when storing millions of numeric values.

Example: Primitives vs Objects

java
import java.util.List;
import java.util.ArrayList;
public class Main {
	public static void main(String[] args) {
		List<Integer> numbers = new ArrayList<>(); // int autoboxed into Integer
		numbers.add(5);
		int value = numbers.get(0);
		System.out.println(value);
	}
}

The Collections Utility Class

The Collections utility class provides static helper methods — sort(), reverse(), max(), unmodifiableList() — that operate on any collection without you having to implement common algorithms yourself.

Example: The Collections Utility Class

java
// Import java.util.Collections so it can be used by its short name
import java.util.Collections;
// Import java.util.ArrayList so it can be used by its short name
import java.util.ArrayList;
import java.util.List;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new ArrayList object and store it in numbers
		List<Integer> numbers = new ArrayList<>(List.of(3, 1, 2));
		Collections.sort(numbers);
		// Print a line to the console
		System.out.println(numbers);
	}
}

Advantages of Collections

Using the framework's interfaces instead of concrete classes directly (coding to List rather than ArrayList) lets you swap the underlying implementation later without touching the rest of your code.

Example: Advantages of Collections

java
import java.util.List;
import java.util.ArrayList;
public class Main {
	static void printAll(List<String> items) { // coded to the interface, not ArrayList
		for (String item : items) {
			System.out.println(item);
		}
	}
	public static void main(String[] args) {
		printAll(new ArrayList<>(List.of("a", "b")));
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Trying to store primitives such as int in a collection, when generics need wrapper types like Integer.
  2. Using raw types like List without <String>, so type errors appear at runtime.
  3. Modifying a collection while looping over it, which throws ConcurrentModificationException.

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.