Class ComparatorUtil
java.lang.Object
cloud.opencode.base.collections.ComparatorUtil
ComparatorUtil - Comparator Utility Class
ComparatorUtil - 比较器工具类
Provides utility methods for creating and manipulating Comparators.
提供创建和操作 Comparator 的工具方法。
Features | 主要功能:
- Null handling comparators - 空值处理比较器
- Order checking methods - 顺序检查方法
- Lexicographical comparators - 字典序比较器
- Min/Max value operations - 最值操作
- Top K collectors - Top K 收集器
Usage Examples | 使用示例:
// Check if ordered - 检查是否有序
boolean ordered = ComparatorUtil.isInOrder(list, Comparator.naturalOrder());
// Lexicographical comparator - 字典序比较器
Comparator<Iterable<String>> lexical = ComparatorUtil.lexicographical();
// Get min/max - 获取最小/最大值
String min = ComparatorUtil.min("a", "b", Comparator.naturalOrder());
// Least K elements collector - 最小 K 个元素收集器
List<Integer> smallest = stream.collect(ComparatorUtil.least(5, Comparator.naturalOrder()));
Performance | 性能特性:
- isInOrder: O(n) - isInOrder: O(n)
- least/greatest: O(n log k) - least/greatest: O(n log k)
Security | 安全性:
- Thread-safe: Yes (stateless) - 线程安全: 是(无状态)
- Null-safe: Yes - 空值安全: 是
- Since:
- JDK 25, opencode-base-collections V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Comparator<T> emptiesFirst(Comparator<? super T> valueComparator) Return a comparator that treats empty as less than non-empty 空值优先的比较器static <T> Comparator<T> emptiesLast(Comparator<? super T> valueComparator) Return a comparator that treats empty as greater than non-empty 空值最后的比较器greatest(int k, Comparator<? super T> comparator) Return a collector that collects the k greatest elements 获取前 K 个最大值的 Collectorstatic <T> booleanisInOrder(Iterable<? extends T> iterable, Comparator<T> comparator) Check if the iterable is in non-strict ascending order 检查是否有序static <T> booleanisInStrictOrder(Iterable<? extends T> iterable, Comparator<T> comparator) Check if the iterable is in strict ascending order 检查是否严格有序least(int k, Comparator<? super T> comparator) Return a collector that collects the k smallest elements 获取前 K 个最小值的 Collectorstatic <T extends Comparable<? super T>>
Comparator<Iterable<T>> Return a lexicographical comparator for iterables Iterable 的字典序比较器static <T> Comparator<Iterable<T>> lexicographical(Comparator<? super T> comparator) Return a lexicographical comparator for iterables with custom element comparator 带自定义比较器的字典序static <T> Tmax(T a, T b, Comparator<? super T> comparator) Return the maximum of two values 返回最大值static <T> Tmin(T a, T b, Comparator<? super T> comparator) Return the minimum of two values 返回最小值static <T> Comparator<T> nullsFirst(Comparator<? super T> comparator) Return a comparator that treats null as less than all other values null 值最小的比较器static <T> Comparator<T> nullsLast(Comparator<? super T> comparator) Return a comparator that treats null as greater than all other values null 值最大的比较器
-
Method Details
-
nullsFirst
Return a comparator that treats null as less than all other values null 值最小的比较器- Type Parameters:
T- element type | 元素类型- Parameters:
comparator- the comparator for non-null values | 非空值的比较器- Returns:
- comparator with nulls first | null 优先的比较器
-
nullsLast
Return a comparator that treats null as greater than all other values null 值最大的比较器- Type Parameters:
T- element type | 元素类型- Parameters:
comparator- the comparator for non-null values | 非空值的比较器- Returns:
- comparator with nulls last | null 最后的比较器
-
isInOrder
Check if the iterable is in non-strict ascending order 检查是否有序- Type Parameters:
T- element type | 元素类型- Parameters:
iterable- the iterable | 可迭代对象comparator- the comparator | 比较器- Returns:
- true if in order | 如果有序则返回 true
-
isInStrictOrder
Check if the iterable is in strict ascending order 检查是否严格有序- Type Parameters:
T- element type | 元素类型- Parameters:
iterable- the iterable | 可迭代对象comparator- the comparator | 比较器- Returns:
- true if in strict order | 如果严格有序则返回 true
-
lexicographical
Return a lexicographical comparator for iterables Iterable 的字典序比较器- Type Parameters:
T- element type | 元素类型- Returns:
- lexicographical comparator | 字典序比较器
-
lexicographical
Return a lexicographical comparator for iterables with custom element comparator 带自定义比较器的字典序- Type Parameters:
T- element type | 元素类型- Parameters:
comparator- element comparator | 元素比较器- Returns:
- lexicographical comparator | 字典序比较器
-
min
Return the minimum of two values 返回最小值- Type Parameters:
T- element type | 元素类型- Parameters:
a- first value | 第一个值b- second value | 第二个值comparator- the comparator | 比较器- Returns:
- minimum value | 最小值
-
max
Return the maximum of two values 返回最大值- Type Parameters:
T- element type | 元素类型- Parameters:
a- first value | 第一个值b- second value | 第二个值comparator- the comparator | 比较器- Returns:
- maximum value | 最大值
-
least
Return a collector that collects the k smallest elements 获取前 K 个最小值的 Collector- Type Parameters:
T- element type | 元素类型- Parameters:
k- number of elements | 元素数量comparator- the comparator | 比较器- Returns:
- collector for k smallest elements | 最小 k 个元素的收集器
-
greatest
Return a collector that collects the k greatest elements 获取前 K 个最大值的 Collector- Type Parameters:
T- element type | 元素类型- Parameters:
k- number of elements | 元素数量comparator- the comparator | 比较器- Returns:
- collector for k greatest elements | 最大 k 个元素的收集器
-
emptiesFirst
Return a comparator that treats empty as less than non-empty 空值优先的比较器- Type Parameters:
T- element type | 元素类型- Parameters:
valueComparator- comparator for non-empty values | 非空值的比较器- Returns:
- comparator with empties first | 空值优先的比较器
-
emptiesLast
Return a comparator that treats empty as greater than non-empty 空值最后的比较器- Type Parameters:
T- element type | 元素类型- Parameters:
valueComparator- comparator for non-empty values | 非空值的比较器- Returns:
- comparator with empties last | 空值最后的比较器
-