The Java Collections Framework
The Java Collections Framework is a set of ready-made classes for storing and manipulating groups of objects. Unlike arrays, collections grow and shrink as your program runs. The three core interfaces are List (an ordered sequence, allows duplicates), Set (no duplicates), and Map (key-to-value lookups). The most-used implementations are ArrayList, HashSet, and HashMap. Combined with generics (the <Type> syntax) they give you type-safe, flexible containers.
List — an ordered, resizable sequence
import java.util.ArrayList;
import java.util.List;
List<String> students = new ArrayList<>(); // <String> = it holds Strings
students.add("Asha");
students.add("Rohan");
students.add("Asha"); // duplicates allowed
System.out.println(students.get(0)); // "Asha" (access by index)
System.out.println(students.size()); // 3
students.remove("Rohan");
System.out.println(students); // [Asha, Asha]
ArrayList is the everyday List. The <String> is a generic type argument — it tells the compiler what the list contains, so you get errors at compile time instead of crashes at runtime.
Set — unique elements
import java.util.HashSet;
import java.util.Set;
Set<String> tags = new HashSet<>();
tags.add("java");
tags.add("oop");
tags.add("java"); // ignored — already present
System.out.println(tags.size()); // 2
System.out.println(tags.contains("oop")); // true
A Set automatically rejects duplicates. HashSet does not preserve order; use LinkedHashSet to keep insertion order or TreeSet to keep sorted order.
Map — key-value pairs
import java.util.HashMap;
import java.util.Map;
Map<String, Integer> marks = new HashMap<>(); // String keys, Integer values
marks.put("Asha", 72);
marks.put("Rohan", 65);
System.out.println(marks.get("Asha")); // 72
System.out.println(marks.getOrDefault("Sam", 0)); // 0 — safe default for a missing key
System.out.println(marks.containsKey("Rohan")); // true
A Map stores associations: look up a value by its key. Keys are unique; putting a value with an existing key replaces the old one.
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesIterating over collections
List<String> students = List.of("Asha", "Rohan", "Sam"); // immutable quick list
// for-each over a List or Set
for (String s : students) {
System.out.println(s);
}
// Iterating a Map's entries
Map<String, Integer> marks = new HashMap<>();
marks.put("Asha", 72);
marks.put("Rohan", 65);
for (var entry : marks.entrySet()) { // var infers Map.Entry<String,Integer>
System.out.println(entry.getKey() + " -> " + entry.getValue());
}
Choosing the right collection
- Need order and index access, with possible duplicates?
List/ArrayList. - Need uniqueness and fast membership tests?
Set/HashSet. - Need to look things up by a key?
Map/HashMap.
Pick the interface (List, Set, Map) as the variable type, and the implementation (ArrayList, etc.) on the right of new — this keeps your code flexible.
Common mistakes
- Forgetting the generic type.
List list = new ArrayList();(raw type) loses type safety; always writeList<String>. - Modifying a collection while iterating it. Removing inside a for-each loop throws
ConcurrentModificationException; use anIteratororremoveIf. - Assuming
HashSet/HashMapkeep order. They do not. UseLinkedHashSet/LinkedHashMapif order matters. - Calling
.get(key)and not handlingnull. A missing key returnsnull; usegetOrDefaultor check first. - Using arrays when a
Listfits better. If size changes, reach for aList, not a fixed array.
FAQ
What are generics, exactly? They let a class work with a specified type while staying type-safe — List<String> is a list that the compiler guarantees holds only Strings.
ArrayList or LinkedList? ArrayList is the right default for almost all use; LinkedList only helps in niche cases with heavy insertion at the ends.
Keep going
Next, run work concurrently with Multithreading in Java, or revisit Arrays in Java. All tutorials are on the Java hub; for guided practice in Jalgaon, see the Java course.
Master practical data structures at Infoplanet, Jalgaon. Join the waitlist for the Java course.
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesFounder, Infoplanet
Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.
Related guides
Arrays in Java
Store many values of one type with Java arrays: declaring, initializing, indexing, iterating, multidimensional arrays, and the Arrays helper class.
Classes & Objects in Java
Define classes with fields and methods, create objects with new, use the this keyword, and understand instance vs static members in Java.
Constructors & Overloading
Initialise objects properly with Java constructors — default and parameterized — and reduce duplication with constructor and method overloading.
