0Pricing
Java Academy · Lesson

Collections Utilities

Use helpful utilities from java.util.Collections: sort, reverse, shuffle, min/max, frequency, addAll, and unmodifiable views.

Collections Utilities is a free Java Academy lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Overview

The Collections helper class provides small tools that save time: sort, reverse, shuffle, min/max, frequency, addAll, and unmodifiableList. These are in java.util.Collections.

In-place operations

In-place methods (sort, reverse, shuffle) modify the given List. For Sets, copy to a List before sorting. For readability, print before/after when you mutate.

Code: sort/reverse/shuffle

sort uses natural order; reverse flips the list; shuffle randomizes. All change the same List instance.

public class Main {
  public static void main(String[] args) {
    java.util.List<String> names = new java.util.ArrayList<>();
    names.add("Bob"); names.add("Ada"); names.add("Cyd");

    System.out.println("orig=" + names);
    java.util.Collections.sort(names);          // natural order (A–Z)
    System.out.println("sorted=" + names);
    java.util.Collections.reverse(names);       // reverse order
    System.out.println("reversed=" + names);
    java.util.Collections.shuffle(names);       // random order
    System.out.println("shuffled=" + names);
  }
}

Min/Max & frequency

min and max select elements by natural order. frequency(list, x) counts how many times an element appears. Great for small stats and tests.

Code: min/max/frequency

Utilities return values without changing the List. The numbers above are computed using the natural order of Integer.

public class Main {
  public static void main(String[] args) {
    java.util.List<Integer> nums = new java.util.ArrayList<>();
    nums.add(7); nums.add(3); nums.add(7); nums.add(1);

    int min = java.util.Collections.min(nums);
    int max = java.util.Collections.max(nums);
    int freq7 = java.util.Collections.frequency(nums, 7);

    System.out.println("min=" + min + ", max=" + max + ", count(7)=" + freq7);
  }
}

Code: addAll & unmodifiableList

addAll is handy for building lists. unmodifiableList creates a read-only view to prevent accidental changes.

public class Main {
  public static void main(String[] args) {
    java.util.List<String> a = new java.util.ArrayList<>();
    java.util.Collections.addAll(a, "red", "green");

    java.util.List<String> b = new java.util.ArrayList<>();
    java.util.Collections.addAll(b, "blue", "green");

    // merge b into a
    a.addAll(b);
    System.out.println("merged=" + a); // [red, green, blue, green]

    // create a read-only view
    java.util.List<String> readOnly = java.util.Collections.unmodifiableList(a);
    System.out.println("readonly size=" + readOnly.size());
    // readOnly.add("x"); // would throw UnsupportedOperationException if uncommented
  }
}

Collections utilities check

Quick check: What does Collections.sort(list) do by default?

Recap

Recap: Use Collections for quick wins: sort/reverse/shuffle for order, min/max/frequency for small stats, addAll to merge, and unmodifiableList for read-only views.

Frequently asked questions

Is the “Collections Utilities” lesson free?

Yes — the full text of “Collections Utilities” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Collections Utilities”?

Use helpful utilities from java.util.Collections: sort, reverse, shuffle, min/max, frequency, addAll, and unmodifiable views. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Collections Utilities” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. List & Set Overview
  2. Iterators & Enhanced for
  3. Collections Utilities
← Back to Java Academy