Java provides a rich set of polymorphic algorithms—methods that provide useful functionality on various collections. Most of these reside in the Collections and Arrays classes, operating on Lists, Sets, and Arrays using high-performance logic like TimSort and Binary Search.
Sorting is the most fundamental algorithm. Java uses TimSort, a hybrid algorithm derived from Merge Sort and Insertion Sort. It is designed to perform exceptionally well on real-world data that often contains "runs" of already sorted elements.
Collections.sort(list) or list.sort(comparator).If you need to find an element in a massive list, a linear scan ($O(n)$) is too slow. Java provides binarySearch(), which uses the "Divide and Conquer" strategy to find elements in $O(\log n)$ time.
binarySearch(). If it isn't, the result is undefined.
int index = Collections.binarySearch(sortedList, "Target");
In many applications, like card games or randomized testing, you need to destroy order. The shuffle() algorithm uses the **Fisher-Yates** shuffle to ensure that every possible permutation of the list is equally likely.
Collections.shuffle(myList); // Randomizes the order in place
Java makes it trivial to find the boundaries of your data or modify its composition across the entire collection.
| Algorithm | Method | Description |
|---|---|---|
| Min/Max | Collections.max(coll) |
Finds the largest element based on natural order. |
| Frequency | Collections.frequency(coll, obj) |
Counts how many times an object appears in the collection. |
| Disjoint | Collections.disjoint(c1, c2) |
Returns true if two collections have no common elements. |
| Fill | Collections.fill(list, obj) |
Replaces every element in a list with the specified object. |
This example demonstrates a pipeline: sorting data, searching for an item, and then reversing the entire dataset.
Java Collections are not thread-safe by default. However, the Collections class provides "Wrapper" algorithms that transform any collection into a thread-safe version.
List<String> syncList = Collections.synchronizedList(new ArrayList<>());
Note: While these wrappers make the methods thread-safe, you still need to manually synchronize when iterating over them.
In modern Java development, immutability is a key defensive programming technique. You can use algorithms to create read-only views of your data.
Collections.unmodifiableList(list): Returns a view that throws an exception if you try to add() or remove().List.of() (Java 9+): Creates a truly immutable list that is more memory-efficient than the wrapper.Q: What is the time complexity of Collections.shuffle()?
A: $O(n)$. It traverses the list once, swapping each element with a randomly chosen one.
Q: What happens if you call binarySearch() on an unsorted list?
A: The result is unpredictable. It might return a negative number (indicating the item wasn't found) even if the item is actually in the list, because the algorithm's assumptions were violated.
Q: How do you swap two elements in a List?
A: Use Collections.swap(list, index1, index2). It handles the temp-variable logic for you.
Mastering Java Algorithms means knowing not to reinvent the wheel. Before writing a loop to find a maximum value or a custom sort routine, check java.util.Collections. These algorithms are the result of years of engineering, optimized for the JVM's memory model and execution engine. They make your code faster, safer, and much more readable.