Class Ordering<T>
java.lang.Object
cloud.opencode.base.core.Ordering<T>
- Type Parameters:
T- the type being compared - 被比较的类型
- All Implemented Interfaces:
Comparator<T>
Ordering - Fluent comparator builder
排序器 - 流式比较器构建器
Provides a fluent API for building complex comparators with null handling, reverse ordering, and chaining support.
提供用于构建复杂比较器的流式 API,支持空值处理、反向排序和链式调用。
Usage Examples | 使用示例:
// Natural ordering
Ordering<String> natural = Ordering.natural();
natural.compare("a", "b"); // -1
// Reverse ordering
Ordering<String> reversed = Ordering.<String>natural().reversed();
reversed.compare("a", "b"); // 1
// Nulls first/last
Ordering<String> nullsFirst = Ordering.<String>natural().nullsFirst();
nullsFirst.compare(null, "a"); // -1
// By key extraction
Ordering<Person> byAge = Ordering.from(Person::getAge);
Ordering<Person> byName = Ordering.from(Person::getName);
// Compound ordering
Ordering<Person> compound = byAge.thenComparing(byName);
// Min/Max operations
String min = Ordering.<String>natural().min("apple", "banana"); // "apple"
List<String> top3 = Ordering.<String>natural().leastOf(list, 3);
Features | 主要功能:
- Natural and custom comparator ordering - 自然和自定义比较器排序
- Null handling: nullsFirst/nullsLast - 空值处理: nullsFirst/nullsLast
- Compound ordering with thenComparing - 通过thenComparing复合排序
- Min/Max and top-k element selection - 最小/最大和Top-K元素选择
Security | 安全性:
- Thread-safe: Yes (immutable after creation) - 线程安全: 是(创建后不可变)
- Null-safe: Yes, with nullsFirst()/nullsLast() - 空值安全: 是,通过nullsFirst()/nullsLast()
- Since:
- JDK 25, opencode-base-core V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Ordering<T> allEqual()Returns an ordering that treats all values as equal.static <T> Ordering<T> Returns an ordering based on the iteration order of explicit values.static <T> Ordering<T> explicit(T... valuesInOrder) Returns an ordering based on the iteration order of explicit values.static <T> Ordering<T> from(Comparator<T> comparator) Creates an ordering from an existing comparator.static <T, U extends Comparable<? super U>>
Ordering<T> Creates an ordering by extracting a comparable key.static <T,U> Ordering <T> from(Function<? super T, ? extends U> keyExtractor, Comparator<? super U> keyComparator) Creates an ordering by extracting a key and using a comparator.greatestOf(Iterable<T> iterable, int k) Returns the k greatest elements in the given iterable.immutableSortedCopy(Iterable<T> iterable) Returns an immutable sorted copy of the given iterable.booleanReturns true if the iterable is sorted according to this ordering.booleanisStrictlyOrdered(Iterable<T> iterable) Returns true if the iterable is strictly sorted according to this ordering.Returns the k smallest elements in the given iterable.Returns the maximum value in an iterable.Returns the maximum of two values.final TReturns the maximum of the given values.Returns the minimum value in an iterable.Returns the minimum of two values.final TReturns the minimum of the given values.static <T extends Comparable<? super T>>
Ordering<T> natural()Returns an ordering that uses the natural order of the values.Returns an ordering that treats null as less than all other values.Returns an ordering that treats null as greater than all other values.<F> Ordering<F> onResultOf(Function<F, ? extends T> function) Returns an ordering that applies a function before comparing.reversed()Returns the reverse ordering.sortedCopy(Iterable<T> iterable) Returns a sorted copy of the given iterable.thenComparing(Comparator<? super T> secondary) Returns a compound ordering with a secondary comparator.<U extends Comparable<? super U>>
Ordering<T> thenComparing(Function<? super T, ? extends U> keyExtractor) Returns a compound ordering using a key extractor.Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Comparator
compare, equals, thenComparing, thenComparingDouble, thenComparingInt, thenComparingLong
-
Constructor Details
-
Ordering
public Ordering()
-
-
Method Details
-
natural
Returns an ordering that uses the natural order of the values. 返回使用值的自然顺序的排序器。- Type Parameters:
T- the comparable type - 可比较类型- Returns:
- the natural ordering - 自然排序器
-
from
Creates an ordering from an existing comparator. 从现有比较器创建排序器。- Type Parameters:
T- the type being compared - 被比较的类型- Parameters:
comparator- the comparator - 比较器- Returns:
- the ordering - 排序器
-
from
public static <T, U extends Comparable<? super U>> Ordering<T> from(Function<? super T, ? extends U> keyExtractor) Creates an ordering by extracting a comparable key. 通过提取可比较的键创建排序器。- Type Parameters:
T- the type being compared - 被比较的类型U- the key type - 键类型- Parameters:
keyExtractor- the key extractor - 键提取器- Returns:
- the ordering - 排序器
-
from
public static <T,U> Ordering<T> from(Function<? super T, ? extends U> keyExtractor, Comparator<? super U> keyComparator) Creates an ordering by extracting a key and using a comparator. 通过提取键并使用比较器创建排序器。- Type Parameters:
T- the type being compared - 被比较的类型U- the key type - 键类型- Parameters:
keyExtractor- the key extractor - 键提取器keyComparator- the key comparator - 键比较器- Returns:
- the ordering - 排序器
-
allEqual
Returns an ordering that treats all values as equal. 返回将所有值视为相等的排序器。- Type Parameters:
T- the type - 类型- Returns:
- the all-equal ordering - 全等排序器
-
explicit
Returns an ordering based on the iteration order of explicit values. 返回基于显式值迭代顺序的排序器。- Type Parameters:
T- the type - 类型- Parameters:
valuesInOrder- the values in order - 按顺序排列的值- Returns:
- the explicit ordering - 显式排序器
-
explicit
-
reversed
Returns the reverse ordering. 返回反向排序器。- Specified by:
reversedin interfaceComparator<T>- Returns:
- the reversed ordering - 反向排序器
-
nullsFirst
-
nullsLast
-
thenComparing
Returns a compound ordering with a secondary comparator. 返回具有次要比较器的复合排序器。- Specified by:
thenComparingin interfaceComparator<T>- Parameters:
secondary- the secondary comparator - 次要比较器- Returns:
- the compound ordering - 复合排序器
-
thenComparing
public <U extends Comparable<? super U>> Ordering<T> thenComparing(Function<? super T, ? extends U> keyExtractor) Returns a compound ordering using a key extractor. 使用键提取器返回复合排序器。- Specified by:
thenComparingin interfaceComparator<T>- Type Parameters:
U- the key type - 键类型- Parameters:
keyExtractor- the key extractor - 键提取器- Returns:
- the compound ordering - 复合排序器
-
onResultOf
-
min
-
min
Returns the minimum of the given values. 返回给定值中的最小值。 -
min
-
max
-
max
Returns the maximum of the given values. 返回给定值中的最大值。 -
max
-
leastOf
-
greatestOf
-
sortedCopy
-
immutableSortedCopy
-
isOrdered
-
isStrictlyOrdered
-