程序包 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
      另请参阅:
    • EMPTY_BYTE_ARRAY

      public static final byte[] EMPTY_BYTE_ARRAY
    • EMPTY_INT_ARRAY

      public static final int[] EMPTY_INT_ARRAY
    • EMPTY_LONG_ARRAY

      public static final long[] EMPTY_LONG_ARRAY
    • EMPTY_FLOAT_ARRAY

      public static final float[] EMPTY_FLOAT_ARRAY
    • EMPTY_DOUBLE_ARRAY

      public static final double[] EMPTY_DOUBLE_ARRAY
    • EMPTY_BOOLEAN_ARRAY

      public static final boolean[] EMPTY_BOOLEAN_ARRAY
    • EMPTY_STRING_ARRAY

      public static final String[] EMPTY_STRING_ARRAY
    • EMPTY_OBJECT_ARRAY

      public static final Object[] EMPTY_OBJECT_ARRAY
    • EMPTY_CLASS_ARRAY

      public static final Class<?>[] EMPTY_CLASS_ARRAY
  • 方法详细资料

    • newDelayedCompressList

      public static <E> DelayedCompressList<E> newDelayedCompressList()
    • newDelayedCompressList

      public static <E> DelayedCompressList<E> newDelayedCompressList(int initCapacity)
    • newDelayedCompressList

      public static <E> DelayedCompressList<E> newDelayedCompressList(Collection<? extends E> src)
    • 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个元素
    • removeFirstMatch

      public static <E> boolean removeFirstMatch(List<E> list, Predicate<? super E> predicate)
      移除list中第一个匹配的元素 -- 最好是数组列表
    • removeLastMatch

      public static <E> boolean removeLastMatch(List<E> list, Predicate<? super E> predicate)
      移除List中最后一个匹配的元素 -- 最好是数组列表
    • 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)
      删除满足条件的元素,且不保持列表中元素的顺序 -- 慎用该方法。
      返回:
      删除的元素个数
    • 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 - 最好为数组列表
    • containsCustom

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

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

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

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

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

      public static int indexOfRef(List<?> list, Object element, int startIndex)
      查找对象引用在List中的索引
      参数:
      element - 要查找的元素
      startIndex - 开始下标
    • lastIndexOfRef

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

      public static int lastIndexOfRef(List<?> list, Object element, int startIndex)
      反向查找对象引用在List中的索引
      参数:
      element - 要查找的元素
      startIndex - 开始下标
    • removeRef

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

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

      public static <T> boolean containsRef(T[] list, Object element)
      判断是否存在给定元素的引用
    • indexOfRef

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

      public static <T> int indexOfRef(T[] list, Object element, int startIndex)
      查找对象引用在数组中的索引
      参数:
      element - 要查找的元素
      startIndex - 开始下标
    • lastIndexOfRef

      public static <T> int lastIndexOfRef(T[] list, Object element)
      反向查找对象引用在数组中的索引
    • lastIndexOfRef

      public static <T> int lastIndexOfRef(T[] list, Object element, int startIndex)
      反向查找对象引用在数组中的索引
      参数:
      element - 要查找的元素
      startIndex - 开始下标
    • newArrayList

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

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

      public static <E> ArrayList<E> newArrayList(E a, E b, E c)
    • 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)
    • toList

      public static <E> List<E> toList(E[] array)
    • toList

      public static <E> List<E> toList(E[] array, int offset, int length)
    • toArrayList

      public static <E> ArrayList<E> toArrayList(E[] array)
    • toArrayList

      public static <E> ArrayList<E> toArrayList(E[] array, int offset, int length)
    • 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,则返回空列表
    • 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,用于需要保持元素顺序的场景
    • toImmutableEnumSet

      public static <E extends Enum<E>> Set<E> toImmutableEnumSet(@Nullable Collection<E> src, Class<E> keyType)
      转换为不可变的EnumSet,用户需要保持高效查询效率的场景
    • 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的顺序的场景
    • toImmutableEnumMap

      @Nonnull public static <K extends Enum<K>, V> Map<K,V> toImmutableEnumMap(@Nullable Map<K,V> src, Class<K> keyType)
      转换为不可变的EnumMap,用于需要保持高效查询的场景
    • toImmutableMultiEnumMap

      @Nonnull public static <K extends Enum<K>, V> Map<K,List<V>> toImmutableMultiEnumMap(@Nullable Map<K,? extends Collection<V>> src, Class<K> keyType)
      转换为不可变的EnumMap,用于需要保持高效查询的场景
    • 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)
      获取集合的首个元素
    • firstOrDefault

      @Nullable public static <E> E firstOrDefault(Collection<E> collection, E def)
      如果集合不为空,则返回第一个元素,否则返回默认值
    • 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结构的默认数组大小