Skip to main content

Lesson 8 of 10

Collections Utility Class & Immutable Collections

The java.util.Collections class provides static utility methods for sorting, searching, shuffling, reversing, and creating unmodifiable/singleton collections. Immutable collections (List.of, Map.of, Set.of from Java 9+) are preferred for constants and defensive copying.

10 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Collections & Immutable APIs

Collections & Immutable APIs
import java.util.*;

public class CollectionsUtils {
    public static void main(String[] args) {
        List<Integer> nums = new ArrayList<>(List.of(3, 1, 4, 1, 5, 9, 2, 6));

        // Collections utility methods
        Collections.sort(nums);                      // [1, 1, 2, 3, 4, 5, 6, 9]
        Collections.reverse(nums);                   // [9, 6, 5, 4, 3, 2, 1, 1]
        Collections.shuffle(nums);                   // random order
        Collections.shuffle(nums, new Random(42));   // seeded shuffle (reproducible)

        System.out.println(Collections.min(nums));   // minimum element
        System.out.println(Collections.max(nums));   // maximum element
        System.out.println(Collections.frequency(nums, 1));  // count of 1s: 2

        // Binary search (must be sorted first)
        Collections.sort(nums);
        int idx = Collections.binarySearch(nums, 5); // index of 5

        // Fill and copy
        List<String> filled = new ArrayList<>(Collections.nCopies(5, "★"));
        System.out.println(filled);  // [★, ★, ★, ★, ★]

        // Unmodifiable wrappers (Java 8 approach)
        List<String> mutable = new ArrayList<>(List.of("a", "b", "c"));
        List<String> unmod   = Collections.unmodifiableList(mutable);
        // unmod.add("d");  // throws UnsupportedOperationException

        // Java 9+ Immutable collections - preferred
        List<String>       immuList = List.of("A", "B", "C");  // no nulls, fixed size
        Set<String>        immuSet  = Set.of("X", "Y", "Z");   // no nulls, no dups, no order
        Map<String, Integer> immuMap = Map.of("one", 1, "two", 2, "three", 3);

        // Map.entry and Map.ofEntries for >10 entries
        Map<String, Integer> bigMap = Map.ofEntries(
            Map.entry("a", 1),
            Map.entry("b", 2),
            Map.entry("c", 3)
            // ... up to any number
        );

        // Defensive copy - receive external list, store immutable snapshot
        public static List<String> safeCopy(List<String> input) {
            return List.copyOf(input);  // creates an immutable copy
        }
    }
}

Quick Quiz

Tip

Tip

Practice Collections Utility Class Immutable Collections in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for Collections Utility Class & Immutable Collections

Practice Task

Note

Practice Task - (1) Write a working example of Collections Utility Class Immutable Collections from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with Collections Utility Class Immutable Collections is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready java code.

Topics in This Module

0/10 done