Class ComparatorUtil

java.lang.Object
cloud.opencode.base.collections.ComparatorUtil

public final class ComparatorUtil extends Object
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 Type
    Method
    Description
    static <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 空值最后的比较器
    static <T> Collector<T,?,List<T>>
    greatest(int k, Comparator<? super T> comparator)
    Return a collector that collects the k greatest elements 获取前 K 个最大值的 Collector
    static <T> boolean
    isInOrder(Iterable<? extends T> iterable, Comparator<T> comparator)
    Check if the iterable is in non-strict ascending order 检查是否有序
    static <T> boolean
    isInStrictOrder(Iterable<? extends T> iterable, Comparator<T> comparator)
    Check if the iterable is in strict ascending order 检查是否严格有序
    static <T> Collector<T,?,List<T>>
    least(int k, Comparator<? super T> comparator)
    Return a collector that collects the k smallest elements 获取前 K 个最小值的 Collector
    static <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> T
    max(T a, T b, Comparator<? super T> comparator)
    Return the maximum of two values 返回最大值
    static <T> T
    min(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 值最大的比较器

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • nullsFirst

      public static <T> Comparator<T> nullsFirst(Comparator<? super T> comparator)
      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

      public static <T> Comparator<T> nullsLast(Comparator<? super T> comparator)
      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

      public static <T> boolean isInOrder(Iterable<? extends T> iterable, Comparator<T> comparator)
      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

      public static <T> boolean isInStrictOrder(Iterable<? extends T> iterable, Comparator<T> comparator)
      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

      public static <T extends Comparable<? super T>> Comparator<Iterable<T>> lexicographical()
      Return a lexicographical comparator for iterables Iterable 的字典序比较器
      Type Parameters:
      T - element type | 元素类型
      Returns:
      lexicographical comparator | 字典序比较器
    • lexicographical

      public static <T> Comparator<Iterable<T>> lexicographical(Comparator<? super T> comparator)
      Return a lexicographical comparator for iterables with custom element comparator 带自定义比较器的字典序
      Type Parameters:
      T - element type | 元素类型
      Parameters:
      comparator - element comparator | 元素比较器
      Returns:
      lexicographical comparator | 字典序比较器
    • min

      public static <T> T min(T a, T b, Comparator<? super T> comparator)
      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

      public static <T> T max(T a, T b, Comparator<? super T> comparator)
      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

      public static <T> Collector<T,?,List<T>> least(int k, Comparator<? super T> comparator)
      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

      public static <T> Collector<T,?,List<T>> greatest(int k, Comparator<? super T> comparator)
      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

      public static <T> Comparator<T> emptiesFirst(Comparator<? super T> valueComparator)
      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

      public static <T> Comparator<T> emptiesLast(Comparator<? super T> valueComparator)
      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 | 空值最后的比较器