Web / Apache Commons Collections Interview questions
What is the purpose of ComparatorUtils in Apache Commons Collections?
ComparatorUtils supplies static helpers for building and combining Comparator instances without writing a new class for each variation.
Comparator<String> byLength = ComparatorUtils.naturalComparator(); Comparator<String> reversed = ComparatorUtils.reversedComparator(byLength); String min = ComparatorUtils.min("pear", "kiwi", byLength);
It offers naturalComparator() for Comparable types, reversedComparator() to invert an existing Comparator, chainedComparator() to apply several comparators as tie-breakers in order, and nullLowComparator()/nullHighComparator() to control whether nulls sort first or last.
These are especially useful for building multi-field sort logic (e.g., sort by last name, then by first name) without hand-writing the fallback comparison logic each time.
More Related questions...