Java Collections Utilities
The Collections class is a toolbox of static methods that work on lists. Its methods find extremes, count repeats, reorder a list in place and search a sorted list.
Extremes and counts
Four scores go into a list. Three toolbox calls read the extremes and count the repeats.
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> scores = new ArrayList<>();
scores.add(72);
scores.add(95);
scores.add(88);
scores.add(95);
System.out.println(Collections.max(scores));
System.out.println(Collections.min(scores));
System.out.println(Collections.frequency(scores, 95));
}
}Reverse, swap and fill
Two calls reorder one list in place. A second list shows fill overwriting every slot.
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.add("Ana");
names.add("Ben");
names.add("Cid");
names.add("Dee");
Collections.reverse(names);
System.out.println(names);
Collections.swap(names, 0, 3);
System.out.println(names);
ArrayList<String> slots = new ArrayList<>();
slots.add("free");
slots.add("free");
slots.add("free");
Collections.fill(slots, "taken");
System.out.println(slots);
}
}Search a sorted list
The list is sorted first. binarySearch then answers the index of each value it is asked for.
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> scores = new ArrayList<>();
scores.add(88);
scores.add(72);
scores.add(95);
scores.add(85);
Collections.sort(scores);
System.out.println(scores);
System.out.println(Collections.binarySearch(scores, 88));
System.out.println(Collections.binarySearch(scores, 90));
}
}A read-only view refuses changes
unmodifiableList wraps a list as a read-only view. Reading through the view works, but the add call throws.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> names = new ArrayList<>();
names.add("Ana");
names.add("Ben");
List<String> view = Collections.unmodifiableList(names);
System.out.println(view.size());
view.add("Cid");
}
}Example
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> scores = new ArrayList<>();
scores.add(40);
scores.add(70);
scores.add(55);
scores.add(70);
scores.add(90);
System.out.println(Collections.max(scores));
System.out.println(Collections.frequency(scores, 70));
Collections.reverse(scores);
System.out.println(scores);
Collections.sort(scores);
System.out.println(scores);
System.out.println(Collections.binarySearch(scores, 55));
}
}Key points
- max answers the largest element in a list and min answers the smallest.
- frequency counts how many elements in a list equal a given value.
- reverse flips a list in place and swap trades the elements at two indexes.
- fill overwrites every element in a list with a single value.
- binarySearch answers the index of a value in a sorted list, or a negative number when the value is absent.
- An unmodifiable list is a read-only view, and adding through it throws UnsupportedOperationException.
In this example, max answers 90 and frequency counts the two 70s. reverse flips the list in place, sort restores the order, and binarySearch finds 55 at index 1.
Note
binarySearch only works on a sorted list, so sort the list before searching it.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
A list holds [30, 10, 20] and Collections.sort runs on it. What does Collections.binarySearch(list, 30) answer?