From Arrays to Collections: The Java Collections Framework Map
An array of 3 students can't seat John, so Java throws Index 3 out of bounds. A List just grows. Then the map of the framework: List, Set and Queue under Collection, Map beside it, four behaviours in one program, and why the variable type should be the interface.
What you will learn
- Explain why a fixed-size array can't take one more value
- Create a List that grows as you add
- Name the Collection interfaces List, Set and Queue, Map beside them, and their main classes
- Declare collections with the interface type so one method accepts any List
Before you start
- Java arrays
- Java interfaces
Arrays have one problem: their size is fixed. A list just grows when you add to it. This article shows the problem, the fix, and a map of the whole collections family you'll meet in the next lessons.
An array cannot grow
package org.studyeasy.s04l01;
public class Ex01_ArrayIsFull {
public static void main(String[] args) {
String[] students = new String[3]; // size fixed at 3
students[0] = "Asha";
students[1] = "Ravi";
students[2] = "Meera";
try {
students[3] = "John"; // a 4th student joins
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("No room: " + e.getMessage());
}
System.out.println("Students: " + students.length);
}
}
No room: Index 3 out of bounds for length 3
Students: 3
new String[3] sets aside three slots, and that size is fixed the moment the array is created. The seats are 0, 1 and 2, so index 3 doesn't exist. Java throws an exception, the catch prints it, and the array still holds three.
A list just grows
package org.studyeasy.s04l01;
import java.util.ArrayList;
import java.util.List;
public class Ex02_ListGrows {
public static void main(String[] args) {
List<String> students = new ArrayList<>(); // starts empty
students.add("Asha");
students.add("Ravi");
students.add("Meera");
students.add("John"); // it just grows
System.out.println(students);
System.out.println("Students: " + students.size());
}
}
[Asha, Ravi, Meera, John]
Students: 4
List<String> is a list of Strings, and it starts empty. There's no size to choose. Each add puts a name at the end, and when John arrives the list makes room on its own. size() says 4.
The family: Collection, and Map beside it
Collection is an interface, and under it are three more: List, Set and Queue. Map is a data structure too, but it isn't a Collection, so it stands on its own beside the family. Each has its own job: a list grows and keeps order, a set doesn't allow repeats, a queue usually serves the oldest item first, and a map stores a key and a value. PriorityQueue is the exception among queues: it picks by priority, not by who came first. Don't worry about the details yet; each gets its own lesson.
Same idea, four behaviours
List<String> orders = new ArrayList<>(); // keeps order
orders.add("Pizza");
orders.add("Tea");
orders.add("Pizza");
System.out.println("List: " + orders);
Set<String> tags = new HashSet<>(); // no repeats
tags.add("java");
tags.add("java");
System.out.println("Set: " + tags);
Queue<String> line = new ArrayDeque<>(); // oldest first
line.add("Asha");
line.add("Ravi");
System.out.println("Queue: next is " + line.poll());
Map<String, Integer> marks = new HashMap<>(); // key -> value
marks.put("Asha", 91);
System.out.println("Map: Asha scored " + marks.get("Asha"));
List: [Pizza, Tea, Pizza]
Set: [java]
Queue: next is Asha
Map: Asha scored 91
The list keeps every item, so Pizza appears twice. The set turns the second "java" away, which makes it good for unique values. The queue's poll takes out whoever joined first, Asha, and Ravi stays in line. The map pairs the key Asha with the value 91, and get("Asha") goes straight to it, the way a student ID would. Keep everything, drop repeats, serve the oldest, or look up by key.
Code to the interface
package org.studyeasy.s04l01;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class Ex04_InterfaceType {
static void printRoll(List<String> names) { // any List works
System.out.println(names.size() + " on the roll: " + names);
}
public static void main(String[] args) {
List<String> morning = new ArrayList<>();
morning.add("Asha");
morning.add("Ravi");
List<String> evening = new LinkedList<>();
evening.add("Meera");
printRoll(morning);
printRoll(evening);
}
}
2 on the roll: [Asha, Ravi]
1 on the roll: [Meera]
The interface is on the left, the class on the right: List<String> morning = new ArrayList<>(). printRoll only asks for a List, so it takes an ArrayList and a LinkedList alike. It doesn't care which class built the list. That's the same idea as programming to an interface.
Recap
| Type | Behaviour | Classes you create |
|---|---|---|
List | keeps every item, in order | ArrayList, LinkedList |
Set | drops repeats | HashSet, TreeSet |
Queue | serves the oldest (PriorityQueue: by priority) | ArrayDeque, PriorityQueue |
Map | looks up a value by key; not a Collection | HashMap, TreeMap |
Declare with the interface, then new the class that fits. First up: ArrayList, after a short stop at wrapper classes, which explain why it's Integer and not int in the map above.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Java equals and hashCode: Why a HashSet Needs Both
== asks if it's the very same object, equals compares values, and hashCode puts equal objects in the same HashSet bucket. Three small programs show what breaks when hashCode is missing.
Java Wrapper Classes and Autoboxing
List<int> won't compile because a list holds objects only. Integer.valueOf puts 87 in a box and intValue takes it out, and add and get do it for you. Then two traps: 128 == 128 is false, and unboxing a null Integer throws a NullPointerException.
Java ArrayList: The Stretchy Array
add, add at an index, get, set, remove and contains on a list of courses and a cart, why Git at index 1 pushes SQL and Spring right, removing scores below 50 with an Iterator or removeIf, List.of that can't change, and the array inside that grows from 10 to 15.
Java LinkedList vs ArrayList: Nodes or an Array
An ArrayList is one block of cells; a LinkedList is a chain of nodes with next and prev links. Orders ORD-101 to ORD-106 show addFirst, getLast, removeFirst and remove(1), why get(2) walks two hops, and why a front insert moves three values in one and changes two links in the other.
