Class Ordering<T>
java.lang.Object
cloud.opencode.base.collections.Ordering<T>
- Type Parameters:
T- the type to compare | 要比较的类型
- All Implemented Interfaces:
Serializable, Comparator<T>
Ordering - Fluent Comparator Builder
Ordering - 流式比较器构建器
A fluent API for building complex comparators with method chaining. Provides natural ordering, null handling, reverse, compound, and transform operations.
用于通过方法链构建复杂比较器的流式 API。提供自然排序、空值处理、反转、复合和转换操作。
Features | 主要功能:
- Natural ordering - 自然排序
- Null handling (nullsFirst, nullsLast) - 空值处理
- Reverse ordering - 反转排序
- Compound ordering - 复合排序
- Transform-based ordering - 基于转换的排序
- Min/max operations - 最小/最大操作
Usage Examples | 使用示例:
// Natural ordering - 自然排序
Ordering<String> natural = Ordering.natural();
// Reverse ordering - 反转排序
Ordering<String> reversed = Ordering.natural().reverse();
// Null-safe ordering - 空值安全排序
Ordering<String> nullsFirst = Ordering.natural().nullsFirst();
// Compound ordering - 复合排序
Ordering<Person> byAge = Ordering.natural().onResultOf(Person::getAge);
Ordering<Person> byName = Ordering.natural().onResultOf(Person::getName);
Ordering<Person> compound = byAge.compound(byName);
// Sort collection - 排序集合
List<String> sorted = Ordering.natural().sortedCopy(strings);
// Min/max - 最小/最大
String min = Ordering.natural().min(strings);
String max = Ordering.natural().max(strings);
Performance | 性能特性:
- compare: O(1) to O(k) where k is compound depth - compare: O(1) 到 O(k),k 是复合深度
- sortedCopy: O(n log n) - sortedCopy: O(n log n)
- min/max: O(n) - min/max: O(n)
Security | 安全性:
- Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
- Null-safe: Configurable - 空值安全: 可配置
- Since:
- JDK 25, opencode-base-collections V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Ordering<T> allEqual()Return ordering that considers all values equal.Return arbitrary but consistent ordering based on identity hash code.intbinarySearch(List<? extends T> sortedList, T key) Perform binary search on a sorted list.compound(Comparator<? super T> secondary) Return compound ordering.static <T> Ordering<T> Return explicit ordering based on provided order of elements.static <T> Ordering<T> explicit(T... valuesInOrder) Return explicit ordering based on provided order of elements.static <T> Ordering<T> from(Comparator<T> comparator) Return ordering based on a Comparator.greatestOf(Iterable<T> iterable, int k) Return the greatest k elements.greatestOf(Iterator<T> iterator, int k) Return the greatest k elements from iterator.immutableSortedCopy(Iterable<T> elements) Return an immutable sorted copy of the elements.booleanCheck if elements are in sorted order.booleanisStrictlyOrdered(Iterable<? extends T> iterable) Check if elements are in strict sorted order.Return the least k elements.Return the least k elements from iterator.Return the maximum element.Return the maximum element from iterator.Return the maximum of two values.final TReturn the maximum of multiple values.Return the minimum element.Return the minimum element from iterator.Return the minimum of two values.final TReturn the minimum of multiple values.static <C extends Comparable<? super C>>
Ordering<C> natural()Return natural ordering for Comparable types.Return ordering that places nulls first.Return ordering that places nulls last.<F> Ordering<F> onResultOf(Function<F, ? extends T> function) Return ordering based on a function result.reverse()Return reverse ordering.sortedCopy(Iterable<T> elements) Return a sorted copy of the elements.Return ordering that compares using toString().Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Comparator
compare, equals, reversed, thenComparing, thenComparing, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
-
Constructor Details
-
Ordering
public Ordering()
-
-
Method Details
-
natural
Return natural ordering for Comparable types. 返回 Comparable 类型的自然排序。- Type Parameters:
C- the Comparable type | Comparable 类型- Returns:
- natural ordering | 自然排序
-
from
Return ordering based on a Comparator. 返回基于 Comparator 的排序。- Type Parameters:
T- the type | 类型- Parameters:
comparator- the comparator | 比较器- Returns:
- ordering | 排序
-
explicit
Return explicit ordering based on provided order of elements. 返回基于提供的元素顺序的显式排序。- Type Parameters:
T- the type | 类型- Parameters:
valuesInOrder- values in desired order | 期望顺序的值- Returns:
- explicit ordering | 显式排序
-
explicit
-
allEqual
Return ordering that considers all values equal. 返回将所有值视为相等的排序。- Type Parameters:
T- the type | 类型- Returns:
- all-equal ordering | 全相等排序
-
arbitrary
-
usingToString
-
reverse
-
nullsFirst
-
nullsLast
-
compound
Return compound ordering. 返回复合排序。- Parameters:
secondary- secondary ordering | 次要排序- Returns:
- compound ordering | 复合排序
-
onResultOf
-
sortedCopy
-
immutableSortedCopy
Return an immutable sorted copy of the elements. 返回元素的不可变排序副本。- Parameters:
elements- elements to sort | 要排序的元素- Returns:
- immutable sorted list | 不可变排序列表
-
isOrdered
-
isStrictlyOrdered
-
min
Return the minimum element from iterator. 从迭代器返回最小元素。- Parameters:
iterator- elements | 元素- Returns:
- minimum element | 最小元素
- Throws:
NoSuchElementException- if empty | 如果为空
-
min
Return the minimum element. 返回最小元素。- Parameters:
iterable- elements | 元素- Returns:
- minimum element | 最小元素
- Throws:
NoSuchElementException- if empty | 如果为空
-
min
-
min
-
max
Return the maximum element from iterator. 从迭代器返回最大元素。- Parameters:
iterator- elements | 元素- Returns:
- maximum element | 最大元素
- Throws:
NoSuchElementException- if empty | 如果为空
-
max
Return the maximum element. 返回最大元素。- Parameters:
iterable- elements | 元素- Returns:
- maximum element | 最大元素
- Throws:
NoSuchElementException- if empty | 如果为空
-
max
-
max
-
leastOf
-
leastOf
-
greatestOf
-
greatestOf
-
binarySearch
-