Quick revision
The short answer
Collections.sort sorts a list in place with a stable sort by calling list.sort, and binarySearch finds an element quickly but only in a list already sorted by the same order, otherwise its result is undefined. unmodifiableList returns a read-only view, synchronizedList wraps a list with a lock that we must still hold ourselves while iterating, emptyList returns one shared immutable empty list, and nCopies returns an immutable list of n references to the same object. frequency counts elements equal to a given one, and shuffle puts a list in random order, optionally using a Random we supply.
What to remember
In this answer, we went through eight Collections methods. sort is an in-place stable sort, and binarySearch only works on a list sorted by the same order. unmodifiableList is a read-only view that still shows changes to its source, and synchronizedList locks each call but needs our own lock while iterating. emptyList returns one shared immutable list, nCopies repeats one reference n times, frequency counts equal elements, and shuffle randomises a list in place, repeatably when we pass a seeded Random.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Collections.sort sorts a list in place with a stable sort by calling list.sort, and binarySearch finds an element quickly but only in a list already sorted by the same order, otherwise its result is undefined. unmodifiableList returns a read-only view, synchronizedList wraps a list with a lock that we must still hold ourselves while iterating, emptyList returns one shared immutable empty list, and nCopies returns an immutable list of n references to the same object. frequency counts elements equal to a given one, and shuffle puts a list in random order, optionally using a Random we supply.
Why this question is asked
java.util.Collections, with an s, is a class full of static helpers that we use without thinking. Most of them are simple, but almost every one has a detail that causes a bug when we forget it: a search that returns nonsense, a wrapper that is not as safe as it looks, or a list where every element is the same object. Interviewers ask about them to see whether we know these details. In this answer, we will go through the eight methods in three groups: sorting and searching, wrappers and special lists, and counting and shuffling, with the result of each call shown in the code.
1A Quick Recap of the Collections Class
Collections is a utility class: it has no instances, only static methods that work on the collection interfaces. It should not be confused with the Collection interface, which is the parent of List, Set and Queue. Many of its methods return wrappers or special-purpose lists, and those return values follow the same interfaces as any other list, so the type alone does not tell us whether a list can be modified or is safe to share between threads. That is why knowing each method's contract matters.
2Sort and binarySearch
Collections.sort(list) sorts the list in place, using natural order or a Comparator we pass. Since Java 8 it simply calls list.sort(comparator). The algorithm is a stable merge sort called TimSort, which means elements that compare as equal keep their original relative order, and it runs in O(n log n) time, faster on data that is already partly sorted.
binarySearch finds an element in O(log n) time by repeatedly halving the search range. That only works if the list is sorted by the same order we search with. On an unsorted list, or one sorted by a different comparator, the result is undefined: it may return a wrong index or claim the element is missing. When the element is not found, it returns a negative number that encodes where the element would go: minus the insertion point, minus one.
Notice the last line. The list is sorted case-insensitively, but the search uses natural order, so the halving steps can go the wrong way. The method does not check, because checking would take linear time and defeat its purpose. Stability matters when we sort by one key after another: sorting employees by name and then stably by department leaves each department's employees in name order.
3UnmodifiableList and synchronizedList
Collections.unmodifiableList(list) returns a view that throws UnsupportedOperationException for every change. It is a view, not a copy: whoever still holds the original list can change it, and the view shows those changes. It protects against changes through the reference we hand out, not against changes at the source. For a real snapshot, List.copyOf is the right tool.
| Method | Returns | Watch out for |
|---|---|---|
| sort | nothing; sorts in place | Stable; the list must be modifiable |
binarySearch | index, or -(insertion point) - 1 | List must be sorted by the same order |
unmodifiableList | read-only view | Changes to the source still show through |
synchronizedList | locked wrapper | Hold its lock while iterating |
emptyList | shared immutable empty list | add throws |
nCopies | immutable list, one element repeated | All n slots are the same object |
| frequency | count of equal elements | Uses equals; null is allowed |
| shuffle | nothing; reorders in place | Pass a seeded Random for repeatable tests |
Collections.synchronizedList wraps a list so that every method call takes a lock on the wrapper. Each single call is thread-safe, but iteration is many calls to the iterator, and other threads can change the list between them. The Javadoc therefore requires us to hold the list's own lock during the whole iteration, as in the synchronized block above. The same applies to check-then-act sequences such as if (!list.contains(x)) list.add(x). For many concurrent uses, CopyOnWriteArrayList or a concurrent queue fits better.
4EmptyList and nCopies
Collections.emptyList() returns an immutable empty list. It is a single shared instance, so returning it costs no allocation, which makes it a good return value for a method with nothing to return. Like List.of(), any attempt to add to it throws UnsupportedOperationException, so callers that expect to add to the result need a new ArrayList.
Collections.nCopies(n, obj) returns an immutable list of size n where every position holds the same reference. It does not store n references; it stores the object once and the count, so nCopies(1_000_000, "x") is tiny. It is handy for creating a list filled with a default value, often copied into an ArrayList: new ArrayList<>(Collections.nCopies(10, 0)). The trap is with mutable objects.
Notice the last example. We expected three separate builders, but there is one builder appearing three times, so changing it through any index changes them all. For mutable elements we create each one separately, for example with Stream.generate(StringBuilder::new).limit(3).toList().
5Frequency and shuffle
Collections.frequency(collection, obj) counts the elements equal to obj, using equals, and it accepts null to count null elements. It walks the whole collection, so it is O(n) each time; to count many different values, grouping into a map once is far cheaper.
Collections.shuffle(list) puts the elements in random order in place, giving every permutation an equal chance. Passing our own Random with a fixed seed makes the order repeatable, which is useful in tests and simulations.
6Follow-up questions
What is the difference between Collections.sort(list) and list.sort(null)?
There is none in behaviour since Java 8: Collections.sort calls list.sort, and null means natural order. list.sort is the more modern form, and specific lists such as ArrayList override it to sort their internal array directly.
Why does Collections.emptyList() not allocate?
Because an empty immutable list carries no state, one instance can safely be shared by the whole program. The method returns that single constant cast to the right element type.
Can I shuffle an unmodifiable list?
No. shuffle swaps elements in place, so it throws UnsupportedOperationException on List.of or an unmodifiable view. Copy it into an ArrayList first.
7Conclusion
In this answer, we went through eight Collections methods. sort is an in-place stable sort, and binarySearch only works on a list sorted by the same order. unmodifiableList is a read-only view that still shows changes to its source, and synchronizedList locks each call but needs our own lock while iterating. emptyList returns one shared immutable list, nCopies repeats one reference n times, frequency counts equal elements, and shuffle randomises a list in place, repeatably when we pass a seeded Random.
Collections.sort sorts a list in place and is stable, so equal elements keep their order; since Java 8 it just calls list.sort, which uses TimSort. binarySearch is O(log n), but only valid if the list is sorted by the same order I search with; otherwise the result is undefined, and if the element is missing it returns minus the insertion point minus one. unmodifiableList gives a read-only view, so writes through it throw, but changes to the original list still show through; for a real snapshot I use List.copyOf. synchronizedList wraps every call in a lock on the wrapper, but iteration is many calls, so I have to hold synchronized on the list for the whole loop, and the same for check-then-act. emptyList returns one shared immutable empty list, so it costs nothing, but callers cannot add to it. nCopies returns an immutable list with the same reference repeated n times, which is great for filling a list with zeros, but a trap with mutable objects, because changing one changes them all. frequency counts elements equal to a value using equals, and it allows null, but it is linear each time. shuffle randomises the order in place, and if I pass a Random with a fixed seed, the order is repeatable, which I use in tests.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 8
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Collections Framework, overview, java.util, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What do these java.util.Collections methods do and what should you know about each...": https://scrivio-interview-library.netlify.app/java/collections-overview/98ae840a3f/