Class SetUtil

java.lang.Object
com.sure.tool.collection.SetUtil

public class SetUtil extends Object
Set 工具类,参考 Guava Sets 与 Hutool 的 SetUtil 设计。
Since:
1.17.0
Author:
suretool
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T,U> List<List<Object>>
    cartesianProduct(Set<T> left, Set<U> right)
    笛卡尔积:左元素序 × 右元素序的二元组列表。
    static <T> Set<Set<T>>
    combinations(Set<T> set, int k)
    k 元素组合(对齐 Guava Sets.combinations)。
    static <T> boolean
    containsAny(Set<T> left, Set<T> right)
    是否与任一右集合元素相交。
    static <T> boolean
    disjoint(Set<T> left, Set<T> right)
    两集合是否无交集。
    static <T> Set<T>
    emptyIfNull(Set<T> set)
    null 返回空 Set。
    static <T> Set<T>
    filter(Set<T> set, Predicate<T> predicate)
    过滤:仅保留满足谓词的元素(保序)。
    static <T> Set<T>
    intersection(Set<T> left, Set<T> right)
    交集(保序,按左集合出现顺序)。
    static <T> boolean
    isSubset(Set<T> subset, Set<T> superset)
    前者是否为后者子集(含相等)。
    static <T> boolean
    isSuperset(Set<T> superset, Set<T> subset)
    前者是否为后者超集(含相等)。
    static <T> String
    join(Set<T> set, CharSequence delimiter)
    元素拼接为字符串。
    static <T,R> Set<R>
    map(Set<T> set, Function<T,R> mapper)
    映射:对每个元素应用函数生成新 Set(保序,结果自动去重)。
    static <T> Set<T>
    newHashSet(T... values)
    创建 HashSet。
    static <T> Set<T>
    newLinkedHashSet(T... values)
    创建 LinkedHashSet(保插入序)。
    static <T extends Comparable<? super T>>
    Set<T>
    newTreeSet(T... values)
    创建 TreeSet(自然序)。
    static <T> Set<T>
    of(T... values)
    创建不可变 Set(去重保序,LinkedHashSet 拷贝后包装为不可变;null 元素保留)。
    static <T> Set<Set<T>>
    powerSet(Set<T> set)
    幂集:所有子集(含空集),元素序保持,共 2^n 个。
    static <T> Set<T>
    reverse(Set<T> set)
    反序新 Set(逆序拷贝)。
    static int
    size(Set<?> set)
    安全取大小(null 返回 0)。
    static <T> Set<T>
    subtract(Set<T> left, Set<T> right)
    差集(保序):左集合中不在右集合的元素。
    static <T> Set<T>
    symmetricDifference(Set<T> left, Set<T> right)
    对称差(保序):两个集合互斥元素(左右各自独有)。
    static <T> Set<T>
    union(Set<T> left, Set<T> right)
    并集(保序去重)。

    Methods inherited from class Object

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

    • newHashSet

      @SafeVarargs public static <T> Set<T> newHashSet(T... values)
      创建 HashSet。
      Type Parameters:
      T - 元素类型
      Parameters:
      values - 元素
      Returns:
      HashSet
    • newLinkedHashSet

      @SafeVarargs public static <T> Set<T> newLinkedHashSet(T... values)
      创建 LinkedHashSet(保插入序)。
      Type Parameters:
      T - 元素类型
      Parameters:
      values - 元素
      Returns:
      LinkedHashSet
    • newTreeSet

      @SafeVarargs public static <T extends Comparable<? super T>> Set<T> newTreeSet(T... values)
      创建 TreeSet(自然序)。
      Type Parameters:
      T - 元素类型
      Parameters:
      values - 元素
      Returns:
      TreeSet
    • of

      @SafeVarargs public static <T> Set<T> of(T... values)
      创建不可变 Set(去重保序,LinkedHashSet 拷贝后包装为不可变;null 元素保留)。
      Type Parameters:
      T - 元素类型
      Parameters:
      values - 元素(可为 null,表示空 Set)
      Returns:
      不可变 Set
      Since:
      1.17.0
    • union

      public static <T> Set<T> union(Set<T> left, Set<T> right)
      并集(保序去重)。
      Type Parameters:
      T - 元素类型
      Parameters:
      left - 左集合(null 按空处理)
      right - 右集合(null 按空处理)
      Returns:
      并集新 Set(LinkedHashSet)
      Since:
      1.17.0
    • intersection

      public static <T> Set<T> intersection(Set<T> left, Set<T> right)
      交集(保序,按左集合出现顺序)。
      Type Parameters:
      T - 元素类型
      Parameters:
      left - 左集合(null 按空处理)
      right - 右集合(null 按空处理)
      Returns:
      交集新 Set
      Since:
      1.17.0
    • subtract

      public static <T> Set<T> subtract(Set<T> left, Set<T> right)
      差集(保序):左集合中不在右集合的元素。
      Type Parameters:
      T - 元素类型
      Parameters:
      left - 左集合(null 按空处理)
      right - 右集合(null 按空处理)
      Returns:
      差集新 Set
      Since:
      1.17.0
    • symmetricDifference

      public static <T> Set<T> symmetricDifference(Set<T> left, Set<T> right)
      对称差(保序):两个集合互斥元素(左右各自独有)。
      Type Parameters:
      T - 元素类型
      Parameters:
      left - 左集合(null 按空处理)
      right - 右集合(null 按空处理)
      Returns:
      对称差新 Set
      Since:
      1.17.0
    • filter

      public static <T> Set<T> filter(Set<T> set, Predicate<T> predicate)
      过滤:仅保留满足谓词的元素(保序)。
      Type Parameters:
      T - 元素类型
      Parameters:
      set - 集合(null 返回空 Set)
      predicate - 过滤谓词(null 时抛 NullPointerException)
      Returns:
      过滤后新 Set
      Since:
      1.17.0
    • map

      public static <T,R> Set<R> map(Set<T> set, Function<T,R> mapper)
      映射:对每个元素应用函数生成新 Set(保序,结果自动去重)。
      Type Parameters:
      T - 元素类型
      R - 结果类型
      Parameters:
      set - 集合(null 返回空 Set)
      mapper - 映射函数(null 时抛 NullPointerException)
      Returns:
      映射后新 Set
      Since:
      1.17.0
    • cartesianProduct

      public static <T,U> List<List<Object>> cartesianProduct(Set<T> left, Set<U> right)
      笛卡尔积:左元素序 × 右元素序的二元组列表。
      Type Parameters:
      T - 左元素类型
      U - 右元素类型
      Parameters:
      left - 左集合(null 按空处理)
      right - 右集合(null 按空处理)
      Returns:
      笛卡尔积(List.of 不可变对)
      Since:
      1.17.0
    • powerSet

      public static <T> Set<Set<T>> powerSet(Set<T> set)
      幂集:所有子集(含空集),元素序保持,共 2^n 个。
      Type Parameters:
      T - 元素类型
      Parameters:
      set - 集合(null 返回含空集的集合)
      Returns:
      幂集
      Since:
      1.17.0
    • isSubset

      public static <T> boolean isSubset(Set<T> subset, Set<T> superset)
      前者是否为后者子集(含相等)。
      Type Parameters:
      T - 元素类型
      Parameters:
      subset - 候选子集(null 视为空集,恒为子集)
      superset - 超集(null 视为空集)
      Returns:
      是否子集
      Since:
      1.17.0
    • isSuperset

      public static <T> boolean isSuperset(Set<T> superset, Set<T> subset)
      前者是否为后者超集(含相等)。
      Type Parameters:
      T - 元素类型
      Parameters:
      superset - 候选超集(null 视为空集)
      subset - 子集(null 视为空集)
      Returns:
      是否超集
      Since:
      1.17.0
    • disjoint

      public static <T> boolean disjoint(Set<T> left, Set<T> right)
      两集合是否无交集。
      Type Parameters:
      T - 元素类型
      Parameters:
      left - 左集合(null 视为空集)
      right - 右集合(null 视为空集)
      Returns:
      无交集返回 true
      Since:
      1.17.0
    • containsAny

      public static <T> boolean containsAny(Set<T> left, Set<T> right)
      是否与任一右集合元素相交。
      Type Parameters:
      T - 元素类型
      Parameters:
      left - 左集合(null 视为空)
      right - 右集合(null 视为空)
      Returns:
      有交集返回 true
      Since:
      1.17.0
    • join

      public static <T> String join(Set<T> set, CharSequence delimiter)
      元素拼接为字符串。
      Type Parameters:
      T - 元素类型
      Parameters:
      set - 集合(null 返回空串)
      delimiter - 分隔符
      Returns:
      拼接结果
      Since:
      1.17.0
    • emptyIfNull

      public static <T> Set<T> emptyIfNull(Set<T> set)
      null 返回空 Set。
      Type Parameters:
      T - 元素类型
      Parameters:
      set - 集合(可为 null)
      Returns:
      原集合或空 LinkedHashSet
      Since:
      1.17.0
    • reverse

      public static <T> Set<T> reverse(Set<T> set)
      反序新 Set(逆序拷贝)。
      Type Parameters:
      T - 元素类型
      Parameters:
      set - 集合(null 返回空 Set)
      Returns:
      反序新 Set
      Since:
      1.17.0
    • size

      public static int size(Set<?> set)
      安全取大小(null 返回 0)。
      Parameters:
      set - 集合(可为 null)
      Returns:
      元素个数
      Since:
      1.17.0
    • combinations

      public static <T> Set<Set<T>> combinations(Set<T> set, int k)
      k 元素组合(对齐 Guava Sets.combinations)。

      combinations({1,2,3}, 2) -> {{1,2},{1,3},{2,3}};k=0 返回含空集合; k=n 返回原集合。k 越界(<0 或 >n)抛 IllegalArgumentException。

      Type Parameters:
      T - 元素类型
      Parameters:
      set - 集合,可为 null
      k - 组合大小
      Returns:
      组合集合;null 返回 null
      Since:
      1.19.0