程序包 cn.wjybxx.base

类 CollectionUtils

java.lang.Object
cn.wjybxx.base.CollectionUtils

public class CollectionUtils extends Object
作者:
wjybxx date 2023/3/31
  • 字段详细资料

    • INDEX_NOT_FOUND

      public static final int INDEX_NOT_FOUND
      另请参阅:
  • 方法详细资料

    • getOrDefault

      public static <E> E getOrDefault(List<E> elements, int index, E def)
      注意:如果list包含null,且def也是null,则返回Null时无法判断是否来自集合。
    • firstOrDefault

      public static <E> E firstOrDefault(List<E> elements, E def)
    • lastOrDefault

      public static <E> E lastOrDefault(List<E> elements, E def)
    • removeFirstN

      public static void removeFirstN(List<?> list, int n)
      删除list的前n个元素
    • removeLastN

      public static void removeLastN(List<?> list, int n)
      删除list的后n个元素
    • removeAt

      public static <E> E removeAt(List<E> list, int index, boolean ordered)
      删除指定位置的元素,可以选择是否保持列表中元素的顺序,当不需要保持顺序时可以对删除性能进行优化 注意:应当小心使用该特性,能够使用该特性的场景不多,应当慎之又慎。
      参数:
      ordered - 是否保持之前的顺序。
      返回:
      删除的元素
    • unorderedRemoveIf

      public static <E> int unorderedRemoveIf(List<E> list, Predicate<? super E> filter)
      删除满足条件的元素,且不保持列表中元素的顺序 -- 慎用该方法。
      返回:
      删除的元素个数
    • indexOf

      public static int indexOf(List<?> list, Object element)
      查找对象引用在数组中的索引
    • lastIndexOf

      public static int lastIndexOf(List<?> list, Object element)
      反向查找对象引用在List中的索引
    • indexOf

      public static int indexOf(List<?> list, Object element, int start, int end)
      反向查找对象引用在List中的索引
      参数:
      list - 数组
      element - 要查找的元素
      start - 数组的有效区间起始下标(inclusive)
      end - 数组的有效区间结束下标(exclusive)
    • lastIndexOf

      public static int lastIndexOf(List<?> list, Object element, int start, int end)
      反向查找对象引用在List中的索引
      参数:
      list - 数组
      element - 要查找的元素
      start - 数组的有效区间起始下标(inclusive)
      end - 数组的有效区间结束下标(exclusive)
    • containsRef

      public static boolean containsRef(List<?> list, Object element)
    • indexOfRef

      public static int indexOfRef(List<?> list, Object element)
      查找对象引用在数组中的索引
    • lastIndexOfRef

      public static int lastIndexOfRef(List<?> list, Object element)
      反向查找对象引用在List中的索引
    • indexOfRef

      public static int indexOfRef(List<?> list, Object element, int start, int end)
      反向查找对象引用在List中的索引
      参数:
      list - 数组
      element - 要查找的元素
      start - 数组的有效区间起始下标(inclusive)
      end - 数组的有效区间结束下标(exclusive)
    • lastIndexOfRef

      public static int lastIndexOfRef(List<?> list, Object element, int start, int end)
      反向查找对象引用在List中的索引
      参数:
      list - 数组
      element - 要查找的元素
      start - 数组的有效区间起始下标(inclusive)
      end - 数组的有效区间结束下标(exclusive)
    • removeRef

      public static boolean removeRef(List<?> list, Object element)
      使用“==”删除对象
    • removeRef

      public static boolean removeRef(List<?> list, Object element, boolean ordered)
      使用“==”删除对象
    • containsCustom

      public static <E> boolean containsCustom(List<E> list, Predicate<? super E> indexFunc)
      参数:
      list - 最好为数组列表
    • indexOfCustom

      public static <E> int indexOfCustom(List<E> list, Predicate<? super E> indexFunc)
      参数:
      list - 最好为数组列表
    • lastIndexOfCustom

      public static <E> int lastIndexOfCustom(List<E> list, Predicate<? super E> indexFunc)
      参数:
      list - 最好为数组列表
    • indexOfCustom

      public static <E> int indexOfCustom(List<E> list, Predicate<? super E> indexFunc, int start, int end)
      参数:
      list - 最好为数组列表
    • lastIndexOfCustom

      public static <E> int lastIndexOfCustom(List<E> list, Predicate<? super E> indexFunc, int start, int end)
      参数:
      list - 最好为数组列表
    • binarySearch

      public static <T> int binarySearch(List<T> array, T key, Comparator<? super T> c)
      如果元素存在,则返回元素对应的下标; 如果元素不存在,则返回(-(insertion point) - 1) 即: (index + 1) * -1 可得应当插入的下标。
      参数:
      array - 数组
      key - 要查找的元素
      c - 比较器
      返回:
      元素下标或插入下标
    • binarySearch

      public static <T> int binarySearch(List<T> array, T key, int fromIndex, int toIndex, Comparator<? super T> c)
      如果元素存在,则返回元素对应的下标; 如果元素不存在,则返回(-(insertion point) - 1) 即: (index + 1) * -1 可得应当插入的下标。
      参数:
      array - 数组
      key - 要查找的元素
      fromIndex - 开始索引
      toIndex - 结束索引
      c - 比较器
      返回:
      元素下标或插入下标
    • binarySearch

      public static <T> int binarySearch(List<T> array, ToIntFunction<? super T> c)
      如果元素存在,则返回元素对应的下标; 如果元素不存在,则返回(-(insertion point) - 1) 即: (index + 1) * -1 可得应当插入的下标。

      如果comparer返回的是 mid.CompareTo(any),适用递增数组; 如果comparer返回的是 any.CompareTo(mid),适用递减数组

      参数:
      array - 数组
      c - 比较器
      返回:
      元素下标或插入下标
    • binarySearch

      public static <T> int binarySearch(List<T> array, int fromIndex, int toIndex, ToIntFunction<? super T> c)
      如果元素存在,则返回元素对应的下标; 如果元素不存在,则返回(-(insertion point) - 1) 即: (index + 1) * -1 可得应当插入的下标。

      如果comparer返回的是 mid.CompareTo(any),适用递增数组; 如果comparer返回的是 any.CompareTo(mid),适用递减数组

      参数:
      array - 数组
      fromIndex - 开始索引
      toIndex - 结束索引
      c - 比较器
      返回:
      元素下标或插入下标
    • newArrayList

      public static <E> ArrayList<E> newArrayList()
    • newArrayList

      public static <E> ArrayList<E> newArrayList(E a)
    • newArrayList

      public static <E> ArrayList<E> newArrayList(E a, E b)
    • newArrayList

      @SafeVarargs public static <E> ArrayList<E> newArrayList(E... array)
    • addAll

      public static <E> boolean addAll(ArrayList<E> self, Collection<? extends E> other)
    • removeAll

      public static <E> boolean removeAll(ArrayList<E> self, Collection<? extends E> other)
    • union

      public static <E> List<E> union(List<E> first, List<? extends E> second)
      连接列表
    • union

      @SafeVarargs public static <E> List<E> union(List<E> first, List<? extends E> second, List<E>... more)
      连接多个列表
    • toImmutableList

      @Nonnull public static <E> List<E> toImmutableList(@Nullable Collection<E> src)
    • toImmutableList

      @Nonnull public static <E> List<E> toImmutableList(@Nullable Collection<E> src, Comparator<? super E> comparator)
      参数:
      comparator - 在转换前进行一次排序
    • nullToArrayList

      public static <E> List<E> nullToArrayList(@Nullable List<E> src)
      如果列表为Null,则返回空数组列表
    • nullToEmptyList

      public static <E> List<E> nullToEmptyList(@Nullable List<E> src)
      如果列表为Null,则返回空列表
    • sequenceEqual

      public static <E> boolean sequenceEqual(List<E> first, List<E> second)
      比较List的相等性 -- 按序相等
    • hashcode

      public static int hashcode(List<?> list)
      获取List的hashcode
    • newHashSet

      public static <E> HashSet<E> newHashSet(int size)
      另请参阅:
    • newLinkedHashSet

      public static <E> LinkedHashSet<E> newLinkedHashSet(int size)
      另请参阅:
    • newIdentityHashSet

      public static <E> Set<E> newIdentityHashSet(int size)
    • toImmutableSet

      @Nonnull public static <E> Set<E> toImmutableSet(@Nullable Collection<E> src)
    • toImmutableLinkedHashSet

      public static <E> Set<E> toImmutableLinkedHashSet(@Nullable Collection<E> src)
      转换为不可变的LinkedHashSet,用于需要保持元素顺序的场景
    • newHashMap

      public static <K, V> HashMap<K,V> newHashMap(int size)
      创建一个能存储指定元素数量的HashMap
      另请参阅:
    • newHashMap

      public static <K, V> HashMap<K,V> newHashMap(K k, V v)
      创建一个包含初始kv的HashMap
    • newHashMap

      public static <K, V> HashMap<K,V> newHashMap(K k, V v, K k2, V v2)
    • newLinkedHashMap

      public static <K, V> LinkedHashMap<K,V> newLinkedHashMap(int size)
      创建一个能存储指定元素数量的LinkedHashMap
      另请参阅:
    • newLinkedHashMap

      public static <K, V> LinkedHashMap<K,V> newLinkedHashMap(K k, V v)
      创建一个包含初始kv的LinkedHashMap
    • newLinkedHashMap

      public static <K, V> LinkedHashMap<K,V> newLinkedHashMap(K k, V v, K k2, V v2)
    • newIdentityHashMap

      public static <K, V> IdentityHashMap<K,V> newIdentityHashMap(int size)
    • getOrThrow

      public static <K, V> V getOrThrow(Map<K,V> map, K key)
      如果给定键不存在则抛出异常
    • getOrThrow

      public static <K, V> V getOrThrow(Map<K,V> map, K key, String property)
    • firstKey

      public static <K> K firstKey(Map<K,?> map)
      抛出:
      NoSuchElementException - 如果map为空
    • firstEntry

      public static <K, V> Map.Entry<K,V> firstEntry(Map<K,V> map)
      抛出:
      NoSuchElementException - 如果map为空
    • inverseMap

      public static <K, V> Map<V,K> inverseMap(Map<K,V> src)
    • inverseMap

      public static <K, V> Map<V,K> inverseMap(Map<K,V> src, Map<V,K> out)
    • toImmutableMap

      @Nonnull public static <K, V> Map<K,V> toImmutableMap(@Nullable Map<K,V> src)
      参数:
      src - 不支持key或value为null
    • toImmutableMultiMap

      @Nonnull public static <K, V> Map<K,List<V>> toImmutableMultiMap(@Nullable Map<K,? extends Collection<V>> src)
      参数:
      src - 不支持key或value为null
    • toImmutableLinkedHashMap

      public static <K, V> Map<K,V> toImmutableLinkedHashMap(@Nullable Map<K,V> src)
      转换为不可变的LinkedHashMap,通常用于需要保留Key的顺序的场景
    • toImmutableMultiLinkedHashMap

      @Nonnull public static <K, V> Map<K,List<V>> toImmutableMultiLinkedHashMap(@Nullable Map<K,? extends Collection<V>> src)
      转换为不可变的LinkedHashMap,通常用于需要保留Key的顺序的场景
    • isEmpty

      public static boolean isEmpty(Map<?,?> map)
    • isEmpty

      public static boolean isEmpty(Collection<?> collection)
    • clear

      public static void clear(@Nullable Collection<?> collection)
    • clear

      public static void clear(@Nullable Map<?,?> map)
    • joint

      public static boolean joint(Collection<?> source, Collection<?> candidates)
      如果两个集合存在公共元素,则返回true
    • disjoint

      public static boolean disjoint(Collection<?> source, Collection<?> candidates)
      如果两个集合没有任何公共元素,则返回true
    • first

      public static <E> E first(Collection<E> elements)
      获取集合的首个元素
    • removeFirstMatch

      public static <E> boolean removeFirstMatch(Collection<E> collection, Predicate<? super E> predicate)
      移除集合中第一个匹配的元素
      类型参数:
      E - 集合中的元素类型
      参数:
      collection - 可修改的集合
      predicate - 删除条件,为true的删除。
      返回:
      是否成功删除了一个元素
    • removeRef

      public static boolean removeRef(Collection<?> collection, Object element)
      使用“==”删除第一个匹配的元素
    • streamOf

      public static <T> Stream<T> streamOf(Iterator<T> iterator)
      Converts iterator to a stream.
    • streamOf

      public static <T> Stream<T> streamOf(Iterable<T> iterable)
      Converts interable to a non-parallel stream.
    • capacity

      public static int capacity(int numMappings)
      计算hash结构的默认数组大小