The Collections utility class has small helpers that replace hand-written loops and read better at a glance.
var votes = List.of("a", "b", "a", "c", "a");
System.out.println("frequency of a = " + Collections.frequency(votes, "a"));
System.out.println("disjoint from [x, y] = " + Collections.disjoint(votes, List.of("x", "y")));
System.out.println("nCopies = " + Collections.nCopies(3, "-"));
System.out.println("max = " + Collections.max(votes) + ", min = " + Collections.min(votes));
var toShuffle = new ArrayList<>(List.of(1, 2, 3, 4, 5));
Collections.reverse(toShuffle);
System.out.println("reversed = " + toShuffle);
frequency of a = 3
disjoint from [x, y] = true
nCopies = [-, -, -]
max = c, min = a
reversed = [5, 4, 3, 2, 1]
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-07-30