Java Tutorial
Java Collections
The Collections Framework gives you List, Set, and Map — shared interfaces with several implementations. Learn the interface first, then pick the class.
Interfaces first
List is ordered and allows duplicates. Set rejects duplicates.Map stores key → value pairs. You program to the interface and construct a concrete class (ArrayList, HashSet, HashMap, …).
Example
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List names = new ArrayList<>();
names.add("Ada");
names.add("Ada");
System.out.println(names.size()); // 2 — List keeps duplicates
}
} Common List operations
add, get, set, remove, contains, andsize cover most day-to-day work. Iteration order is insertion order forArrayList.
Example
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List nums = new ArrayList<>();
nums.add(10);
nums.add(20);
nums.add(30);
nums.set(1, 25);
System.out.println(nums.contains(25));
System.out.println(nums);
}
} Utility helpers
java.util.Collections offers static helpers: sort, reverse, shuffle, frequency. They work on any compatible List.
Example
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
List nums = new ArrayList<>();
nums.add(9);
nums.add(2);
nums.add(5);
Collections.sort(nums);
System.out.println(nums); // [2, 5, 9]
}
} Pick the structure by need: List for sequence, Set for uniqueness, Map for lookup by key. The next lessons dig into Map and Set in detail.
Iterate safely
for-each is the default. When you must remove while iterating, use removeIf or anIterator — not list.remove inside a plain for-each.
Example
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List words = new ArrayList<>();
words.add("a");
words.add("bb");
words.add("ccc");
words.removeIf(w -> w.length() < 2);
System.out.println(words); // [bb, ccc]
}
} List.of / Set.of / Map.of create immutable collections. Mutating them throws UnsupportedOperationException.
Try It Yourself
Exercise: Create an ArrayList<Integer> with 4, 1, 3, sort it with Collections.sort, and print the result.
Show solution
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
List nums = new ArrayList<>();
nums.add(4);
nums.add(1);
nums.add(3);
Collections.sort(nums);
System.out.println(nums); Collections.sort sorts the list in place using natural order for Integer.
Key Takeaways
- Program to
List/Set/Map; construct a concrete implementation. - List keeps order and duplicates; Set and Map serve uniqueness and keyed lookup.
Collectionshelpers cover sort, reverse, and more.- Prefer
removeIfover removing inside a for-each loop.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.
Statistics
Sort then summarise
Collections.sort puts marks in order. After that, first and last are min and max without a second scan.
Example
import java.util.*;
public class Main {
public static void main(String[] args) {
List<Integer> marks = new ArrayList<>(List.of(72, 91, 88));
Collections.sort(marks);
System.out.println(marks.get(0) + " " + marks.get(marks.size() - 1));
}
}