Java Collections Class
In this page:
Sorting Collections
Collections.sort() reorders a List in place according to natural ordering or a supplied Comparator, saving you from implementing a sorting algorithm yourself for everyday use cases.
Example: Sorting Collections
import java.util.Collections;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<>(java.util.List.of(3, 1, 2));
Collections.sort(nums);
System.out.println(nums);
}
}
Login to try C/C++/Java/PHP code in the editor
Reversing and Shuffling
Collections.reverse() flips a list's order in place, and Collections.shuffle() randomizes it — both operate directly on the list you pass in rather than returning a new one.
Example: Reversing and Shuffling
import java.util.Collections;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<>(java.util.List.of(1, 2, 3));
Collections.reverse(nums);
System.out.println(nums);
}
}
Login to try C/C++/Java/PHP code in the editor
Searching Collections
Collections.binarySearch() finds an element's index in a *sorted* list in O(log n) time — calling it on an unsorted list gives undefined, unreliable results, so always sort first.
Example: Searching Collections
import java.util.Collections;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<>(java.util.List.of(1, 3, 5, 7));
int index = Collections.binarySearch(nums, 5); // list must already be sorted
System.out.println(index);
}
}
Login to try C/C++/Java/PHP code in the editor
Modifying Lists
Methods like Collections.max(), min(), frequency(), and swap() let you query or rearrange list contents without writing manual loops for these very common operations.
Example: Modifying Lists
import java.util.Collections;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<>(java.util.List.of(3, 1, 4, 1));
System.out.println(Collections.max(nums));
System.out.println(Collections.frequency(nums, 1));
Collections.swap(nums, 0, 1);
System.out.println(nums);
}
}
Login to try C/C++/Java/PHP code in the editor
Unmodifiable Collections
Collections.unmodifiableList() (and its Set/Map equivalents) wraps a collection so any attempted modification throws UnsupportedOperationException, letting you safely hand out a read-only view of internal data.
Example: Unmodifiable Collections
import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<Integer> readOnly = Collections.unmodifiableList(new ArrayList<>(java.util.List.of(1, 2)));
try {
readOnly.add(3);
} catch (UnsupportedOperationException e) {
System.out.println("Cannot modify: " + e.getClass().getSimpleName());
}
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 17 topics to unlock
0/17 topics done
Complete these topics first:
- Java Data Structures
- Java Collections Introduction
- Java List Interface
- Java ArrayList
- Java LinkedList
- Java Map Interface
- Java HashMap
- Java Set Interface
- Java TreeMap
- Java LinkedHashMap
- Java HashSet
- Java TreeSet
- Java LinkedHashSet
- Java Stack & Queue
- Java Iterator
- Java Collections Class
- Java List Sorting