java.lang.Object
cn.wjybxx.base.CollectionUtils
- 作者:
- wjybxx date 2023/3/31
-
字段概要
字段 -
方法概要
修饰符和类型方法说明static <E> booleanaddAll(ArrayList<E> self, Collection<? extends E> other) static <T> intbinarySearch(List<T> array, int fromIndex, int toIndex, ToIntFunction<? super T> c) 如果元素存在,则返回元素对应的下标; 如果元素不存在,则返回(-(insertion point) - 1) 即: (index + 1) * -1 可得应当插入的下标。static <T> intbinarySearch(List<T> array, ToIntFunction<? super T> c) 如果元素存在,则返回元素对应的下标; 如果元素不存在,则返回(-(insertion point) - 1) 即: (index + 1) * -1 可得应当插入的下标。static <T> intbinarySearch(List<T> array, T key, int fromIndex, int toIndex, Comparator<? super T> c) 如果元素存在,则返回元素对应的下标; 如果元素不存在,则返回(-(insertion point) - 1) 即: (index + 1) * -1 可得应当插入的下标。static <T> intbinarySearch(List<T> array, T key, Comparator<? super T> c) 如果元素存在,则返回元素对应的下标; 如果元素不存在,则返回(-(insertion point) - 1) 即: (index + 1) * -1 可得应当插入的下标。static intcapacity(int numMappings) 计算hash结构的默认数组大小static voidclear(Collection<?> collection) static voidstatic <E> booleancontainsCustom(List<E> list, Predicate<? super E> indexFunc) static booleancontainsRef(List<?> list, Object element) static booleandisjoint(Collection<?> source, Collection<?> candidates) 如果两个集合没有任何公共元素,则返回truestatic <E> Efirst(Collection<E> elements) 获取集合的首个元素static <K,V> Map.Entry <K, V> firstEntry(Map<K, V> map) static <K> Kstatic <E> EfirstOrDefault(List<E> elements, E def) static <E> EgetOrDefault(List<E> elements, int index, E def) 注意:如果list包含null,且def也是null,则返回Null时无法判断是否来自集合。static <K,V> V getOrThrow(Map<K, V> map, K key) 如果给定键不存在则抛出异常static <K,V> V getOrThrow(Map<K, V> map, K key, String property) static int查找对象引用在数组中的索引static int反向查找对象引用在List中的索引static <E> intindexOfCustom(List<E> list, Predicate<? super E> indexFunc) static <E> intindexOfCustom(List<E> list, Predicate<? super E> indexFunc, int start, int end) static intindexOfRef(List<?> list, Object element) 查找对象引用在数组中的索引static intindexOfRef(List<?> list, Object element, int start, int end) 反向查找对象引用在List中的索引static <K,V> Map <V, K> inverseMap(Map<K, V> src) static <K,V> Map <V, K> inverseMap(Map<K, V> src, Map<V, K> out) static booleanisEmpty(Collection<?> collection) static booleanstatic booleanjoint(Collection<?> source, Collection<?> candidates) 如果两个集合存在公共元素,则返回truestatic intlastIndexOf(List<?> list, Object element) 反向查找对象引用在List中的索引static intlastIndexOf(List<?> list, Object element, int start, int end) 反向查找对象引用在List中的索引static <E> intlastIndexOfCustom(List<E> list, Predicate<? super E> indexFunc) static <E> intlastIndexOfCustom(List<E> list, Predicate<? super E> indexFunc, int start, int end) static intlastIndexOfRef(List<?> list, Object element) 反向查找对象引用在List中的索引static intlastIndexOfRef(List<?> list, Object element, int start, int end) 反向查找对象引用在List中的索引static <E> ElastOrDefault(List<E> elements, E def) static <E> ArrayList<E> static <E> ArrayList<E> newArrayList(E a) static <E> ArrayList<E> newArrayList(E... array) static <E> ArrayList<E> newArrayList(E a, E b) static <K,V> HashMap <K, V> newHashMap(int size) 创建一个能存储指定元素数量的HashMapstatic <K,V> HashMap <K, V> newHashMap(K k, V v) 创建一个包含初始kv的HashMapstatic <K,V> HashMap <K, V> newHashMap(K k, V v, K k2, V v2) static <E> HashSet<E> newHashSet(int size) static <K,V> IdentityHashMap <K, V> newIdentityHashMap(int size) static <E> Set<E> newIdentityHashSet(int size) static <K,V> LinkedHashMap <K, V> newLinkedHashMap(int size) 创建一个能存储指定元素数量的LinkedHashMapstatic <K,V> LinkedHashMap <K, V> newLinkedHashMap(K k, V v) 创建一个包含初始kv的LinkedHashMapstatic <K,V> LinkedHashMap <K, V> newLinkedHashMap(K k, V v, K k2, V v2) static <E> LinkedHashSet<E> newLinkedHashSet(int size) static <E> List<E> nullToArrayList(List<E> src) 如果列表为Null,则返回空数组列表static <E> List<E> nullToEmptyList(List<E> src) 如果列表为Null,则返回空列表static <E> booleanremoveAll(ArrayList<E> self, Collection<? extends E> other) static <E> E删除指定位置的元素,可以选择是否保持列表中元素的顺序,当不需要保持顺序时可以对删除性能进行优化 注意:应当小心使用该特性,能够使用该特性的场景不多,应当慎之又慎。static <E> booleanremoveFirstMatch(Collection<E> collection, Predicate<? super E> predicate) 移除集合中第一个匹配的元素static voidremoveFirstN(List<?> list, int n) 删除list的前n个元素static voidremoveLastN(List<?> list, int n) 删除list的后n个元素static booleanremoveRef(Collection<?> collection, Object element) 使用“==”删除第一个匹配的元素static boolean使用“==”删除对象static boolean使用“==”删除对象static <T> Stream<T> Converts interable to a non-parallel stream.static <T> Stream<T> Converts iterator to a stream.toImmutableEnumMap(Map<K, V> src, Class<K> keyType) 转换为不可变的EnumMap,用于需要保持高效查询的场景toImmutableEnumSet(Collection<E> src, Class<E> keyType) 转换为不可变的EnumSet,用户需要保持高效查询效率的场景static <K,V> Map <K, V> toImmutableLinkedHashMap(Map<K, V> src) 转换为不可变的LinkedHashMap,通常用于需要保留Key的顺序的场景static <E> Set<E> toImmutableLinkedHashSet(Collection<E> src) 转换为不可变的LinkedHashSet,用于需要保持元素顺序的场景static <E> List<E> toImmutableList(Collection<E> src) static <E> List<E> toImmutableList(Collection<E> src, Comparator<? super E> comparator) static <K,V> Map <K, V> toImmutableMap(Map<K, V> src) toImmutableMultiEnumMap(Map<K, ? extends Collection<V>> src, Class<K> keyType) 转换为不可变的EnumMap,用于需要保持高效查询的场景toImmutableMultiLinkedHashMap(Map<K, ? extends Collection<V>> src) 转换为不可变的LinkedHashMap,通常用于需要保留Key的顺序的场景toImmutableMultiMap(Map<K, ? extends Collection<V>> src) static <E> Set<E> toImmutableSet(Collection<E> src) static <E> List<E> 连接列表static <E> List<E> 连接多个列表static <E> intunorderedRemoveIf(List<E> list, Predicate<? super E> filter) 删除满足条件的元素,且不保持列表中元素的顺序 -- 慎用该方法。
-
字段详细资料
-
INDEX_NOT_FOUND
public static final int INDEX_NOT_FOUND- 另请参阅:
-
-
方法详细资料
-
getOrDefault
注意:如果list包含null,且def也是null,则返回Null时无法判断是否来自集合。 -
firstOrDefault
-
lastOrDefault
-
removeFirstN
删除list的前n个元素 -
removeLastN
删除list的后n个元素 -
removeAt
删除指定位置的元素,可以选择是否保持列表中元素的顺序,当不需要保持顺序时可以对删除性能进行优化 注意:应当小心使用该特性,能够使用该特性的场景不多,应当慎之又慎。- 参数:
ordered- 是否保持之前的顺序。- 返回:
- 删除的元素
-
unorderedRemoveIf
删除满足条件的元素,且不保持列表中元素的顺序 -- 慎用该方法。- 返回:
- 删除的元素个数
-
indexOf
查找对象引用在数组中的索引 -
lastIndexOf
反向查找对象引用在List中的索引 -
indexOf
反向查找对象引用在List中的索引- 参数:
list- 数组element- 要查找的元素start- 数组的有效区间起始下标(inclusive)end- 数组的有效区间结束下标(exclusive)
-
lastIndexOf
反向查找对象引用在List中的索引- 参数:
list- 数组element- 要查找的元素start- 数组的有效区间起始下标(inclusive)end- 数组的有效区间结束下标(exclusive)
-
containsRef
-
indexOfRef
查找对象引用在数组中的索引 -
lastIndexOfRef
反向查找对象引用在List中的索引 -
indexOfRef
反向查找对象引用在List中的索引- 参数:
list- 数组element- 要查找的元素start- 数组的有效区间起始下标(inclusive)end- 数组的有效区间结束下标(exclusive)
-
lastIndexOfRef
反向查找对象引用在List中的索引- 参数:
list- 数组element- 要查找的元素start- 数组的有效区间起始下标(inclusive)end- 数组的有效区间结束下标(exclusive)
-
removeRef
使用“==”删除对象 -
removeRef
使用“==”删除对象 -
containsCustom
- 参数:
list- 最好为数组列表
-
indexOfCustom
- 参数:
list- 最好为数组列表
-
lastIndexOfCustom
- 参数:
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
如果元素存在,则返回元素对应的下标; 如果元素不存在,则返回(-(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
如果元素存在,则返回元素对应的下标; 如果元素不存在,则返回(-(insertion point) - 1) 即: (index + 1) * -1 可得应当插入的下标。- 参数:
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 可得应当插入的下标。- 参数:
array- 数组fromIndex- 开始索引toIndex- 结束索引c- 比较器- 返回:
- 元素下标或插入下标
-
newArrayList
-
newArrayList
-
newArrayList
-
newArrayList
-
addAll
-
removeAll
-
union
连接列表 -
union
@SafeVarargs public static <E> List<E> union(List<E> first, List<? extends E> second, List<E>... more) 连接多个列表 -
toImmutableList
-
toImmutableList
@Nonnull public static <E> List<E> toImmutableList(@Nullable Collection<E> src, Comparator<? super E> comparator) - 参数:
comparator- 在转换前进行一次排序
-
nullToArrayList
如果列表为Null,则返回空数组列表 -
nullToEmptyList
如果列表为Null,则返回空列表 -
newHashSet
- 另请参阅:
-
newLinkedHashSet
- 另请参阅:
-
newIdentityHashSet
-
toImmutableSet
-
toImmutableLinkedHashSet
转换为不可变的LinkedHashSet,用于需要保持元素顺序的场景 -
toImmutableEnumSet
public static <E extends Enum<E>> Set<E> toImmutableEnumSet(@Nullable Collection<E> src, Class<E> keyType) 转换为不可变的EnumSet,用户需要保持高效查询效率的场景 -
newHashMap
创建一个能存储指定元素数量的HashMap- 另请参阅:
-
newHashMap
创建一个包含初始kv的HashMap -
newHashMap
-
newLinkedHashMap
创建一个能存储指定元素数量的LinkedHashMap- 另请参阅:
-
newLinkedHashMap
创建一个包含初始kv的LinkedHashMap -
newLinkedHashMap
-
newIdentityHashMap
-
getOrThrow
如果给定键不存在则抛出异常 -
getOrThrow
-
firstKey
- 抛出:
NoSuchElementException- 如果map为空
-
firstEntry
- 抛出:
NoSuchElementException- 如果map为空
-
inverseMap
-
inverseMap
-
toImmutableMap
- 参数:
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
转换为不可变的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
-
isEmpty
-
clear
-
clear
-
joint
如果两个集合存在公共元素,则返回true -
disjoint
如果两个集合没有任何公共元素,则返回true -
first
获取集合的首个元素 -
removeFirstMatch
public static <E> boolean removeFirstMatch(Collection<E> collection, Predicate<? super E> predicate) 移除集合中第一个匹配的元素- 类型参数:
E- 集合中的元素类型- 参数:
collection- 可修改的集合predicate- 删除条件,为true的删除。- 返回:
- 是否成功删除了一个元素
-
removeRef
使用“==”删除第一个匹配的元素 -
streamOf
Converts iterator to a stream. -
streamOf
Converts interable to a non-parallel stream. -
capacity
public static int capacity(int numMappings) 计算hash结构的默认数组大小
-