类 CollectionUtils

java.lang.Object
cn.taketoday.util.CollectionUtils

public abstract class CollectionUtils extends Object
作者:
TODAY 2019-12-29 23:39
  • 构造器详细资料

    • CollectionUtils

      public CollectionUtils()
  • 方法详细资料

    • isEmpty

      public static boolean isEmpty(@Nullable Collection<?> collection)
      Return true if the supplied Collection is null or empty. Otherwise, return false.
      参数:
      collection - the Collection to check
      返回:
      whether the given Collection is empty
    • isNotEmpty

      public static boolean isNotEmpty(@Nullable Collection<?> collection)
      从以下版本开始:
      4.0
    • isNotEmpty

      public static boolean isNotEmpty(@Nullable ArrayHolder<?> holder)
      Return true if the supplied Collection is not null and empty. Otherwise, return false.
      参数:
      holder - the ArrayHolder to check
      返回:
      whether the given ArrayHolder is not empty
      从以下版本开始:
      4.0
    • isEmpty

      public static boolean isEmpty(@Nullable ArrayHolder<?> holder)
      Return true if the supplied Collection is null or empty. Otherwise, return false.
      参数:
      holder - the ArrayHolder to check
      返回:
      whether the given ArrayHolder is empty
      从以下版本开始:
      4.0
    • isEmpty

      public static boolean isEmpty(@Nullable Map<?,?> map)
      Return true if the supplied Map is null or empty. Otherwise, return false.
      参数:
      map - the Map to check
      返回:
      whether the given Map is empty
    • isNotEmpty

      public static boolean isNotEmpty(@Nullable Map<?,?> map)
      从以下版本开始:
      4.0
    • isCollection

      public static boolean isCollection(Class<?> cls)
    • newHashSet

      @SafeVarargs public static <E> HashSet<E> newHashSet(@Nullable E... elements)
      Create a hash set
      参数:
      elements - Elements instance
    • newLinkedHashSet

      @SafeVarargs public static <E> LinkedHashSet<E> newLinkedHashSet(@Nullable E... elements)
      Instantiate a new LinkedHashSet with an initial elements that can accommodate the specified number of elements without any immediate resize/rehash operations to be expected.
      参数:
      elements - the expected number of elements (with a corresponding capacity to be derived so that no resize/rehash operations are needed)
      从以下版本开始:
      4.0
      另请参阅:
    • newArrayList

      @NonNull @SafeVarargs public static <E> ArrayList<E> newArrayList(@Nullable E... elements)
      类型参数:
      E - Element type
      参数:
      elements - elements array
      返回:
      ArrayLost of input elements
      从以下版本开始:
      4.0
    • isApproximableCollectionType

      public static boolean isApproximableCollectionType(@Nullable Class<?> collectionType)
      Determine whether the given collection type is an approximable type, i.e. a type that createApproximateCollection(java.lang.Object, int) can approximate.
      参数:
      collectionType - the collection type to check
      返回:
      true if the type is approximable
      从以下版本开始:
      3.0
    • createApproximateCollection

      public static <E> Collection<E> createApproximateCollection(Object collection, int capacity)
      Create the most approximate collection for the given collection.

      Warning: Since the parameterized type E is not bound to the type of elements contained in the supplied collection, type safety cannot be guaranteed if the supplied collection is an EnumSet. In such scenarios, the caller is responsible for ensuring that the element type for the supplied collection is an enum type matching type E. As an alternative, the caller may wish to treat the return value as a raw collection or collection of Object.

      参数:
      collection - the original collection object, potentially null
      capacity - the initial capacity
      返回:
      a new, empty collection instance
      从以下版本开始:
      3.0
      另请参阅:
    • createCollection

      public static <E> Collection<E> createCollection(Class<?> collectionType)
      Create the most appropriate collection for the given collection type.

      Delegates to createCollection(Class, Class, int) with a null element type, and Constant.ZERO.

      参数:
      collectionType - the desired type of the target collection (never null)
      返回:
      a new collection instance
      抛出:
      IllegalArgumentException - if the supplied collectionType is null or of type EnumSet
      从以下版本开始:
      3.0
    • createCollection

      public static <E> Collection<E> createCollection(Class<?> collectionType, int capacity)
      Create the most appropriate collection for the given collection type.

      Delegates to createCollection(Class, Class, int) with a null element type.

      参数:
      collectionType - the desired type of the target collection (never null)
      capacity - the initial capacity
      返回:
      a new collection instance
      抛出:
      IllegalArgumentException - if the supplied collectionType is null or of type EnumSet
      从以下版本开始:
      3.0
    • createCollection

      public static <E> Collection<E> createCollection(Class<?> collectionType, Class<?> elementType, int capacity)
      Create the most appropriate collection for the given collection type.

      Warning: Since the parameterized type E is not bound to the supplied elementType, type safety cannot be guaranteed if the desired collectionType is EnumSet. In such scenarios, the caller is responsible for ensuring that the supplied elementType is an enum type matching type E. As an alternative, the caller may wish to treat the return value as a raw collection or collection of Object.

      参数:
      collectionType - the desired type of the target collection (never null)
      elementType - the collection's element type, or null if unknown (note: only relevant for EnumSet creation)
      capacity - the initial capacity
      返回:
      a new collection instance
      抛出:
      IllegalArgumentException - if the supplied collectionType is null; or if the desired collectionType is EnumSet and the supplied elementType is not a subtype of Enum
      从以下版本开始:
      3.0
      另请参阅:
    • isApproximableMapType

      public static boolean isApproximableMapType(@Nullable Class<?> mapType)
      Determine whether the given map type is an approximable type, i.e. a type that createApproximateMap(java.lang.Object) can approximate.
      参数:
      mapType - the map type to check
      返回:
      true if the type is approximable
      从以下版本开始:
      3.0
    • createApproximateMap

      public static <K, V> Map<K,V> createApproximateMap(@Nullable Object map)
      Create the most approximate map for the given map.

      Warning: Since the parameterized type K is not bound to the type of keys contained in the supplied map, type safety cannot be guaranteed if the supplied map is an EnumMap. In such scenarios, the caller is responsible for ensuring that the key type in the supplied map is an enum type matching type K. As an alternative, the caller may wish to treat the return value as a raw map or map keyed by Object.

      use default capacity Constant.ZERO.

      参数:
      map - the original map object, potentially null
      返回:
      a new, empty map instance
      从以下版本开始:
      3.0
      另请参阅:
    • createApproximateMap

      public static <K, V> Map<K,V> createApproximateMap(@Nullable Object map, int capacity)
      Create the most approximate map for the given map.

      Warning: Since the parameterized type K is not bound to the type of keys contained in the supplied map, type safety cannot be guaranteed if the supplied map is an EnumMap. In such scenarios, the caller is responsible for ensuring that the key type in the supplied map is an enum type matching type K. As an alternative, the caller may wish to treat the return value as a raw map or map keyed by Object.

      参数:
      map - the original map object, potentially null
      capacity - the initial capacity
      返回:
      a new, empty map instance
      从以下版本开始:
      3.0
      另请参阅:
    • createMap

      public static <K, V> Map<K,V> createMap(@NonNull Class<?> mapType)
      Create the most appropriate map for the given map type.

      Delegates to createMap(Class, Class, int) with a null key type, and default capacity Constant.ZERO.

      参数:
      mapType - the desired type of the target map
      返回:
      a new map instance
      抛出:
      IllegalArgumentException - if the supplied mapType is null or of type EnumMap
      从以下版本开始:
      3.0
    • createMap

      public static <K, V> Map<K,V> createMap(Class<?> mapType, int capacity)
      Create the most appropriate map for the given map type.

      Delegates to createMap(Class, Class, int) with a null key type.

      参数:
      mapType - the desired type of the target map
      capacity - the initial capacity
      返回:
      a new map instance
      抛出:
      IllegalArgumentException - if the supplied mapType is null or of type EnumMap
      从以下版本开始:
      3.0
    • createMap

      public static <K, V> Map<K,V> createMap(Class<?> mapType, Class<?> keyType, int capacity)
      Create the most appropriate map for the given map type.

      Warning: Since the parameterized type K is not bound to the supplied keyType, type safety cannot be guaranteed if the desired mapType is EnumMap. In such scenarios, the caller is responsible for ensuring that the keyType is an enum type matching type K. As an alternative, the caller may wish to treat the return value as a raw map or map keyed by Object. Similarly, type safety cannot be enforced if the desired mapType is MultiValueMap.

      参数:
      mapType - the desired type of the target map (never null)
      keyType - the map's key type, or null if unknown (note: only relevant for EnumMap creation)
      capacity - the initial capacity
      返回:
      a new map instance
      抛出:
      IllegalArgumentException - if the supplied mapType is null; or if the desired mapType is EnumMap and the supplied keyType is not a subtype of Enum
      从以下版本开始:
      3.0
      另请参阅:
    • createStringAdaptingProperties

      public static Properties createStringAdaptingProperties()
      Create a variant of Properties that automatically adapts non-String values to String representations in Properties.getProperty(java.lang.String).

      In addition, the returned Properties instance sorts properties alphanumerically based on their keys.

      返回:
      a new Properties instance
      从以下版本开始:
      3.0
      另请参阅:
    • createSortedProperties

      public static Properties createSortedProperties(boolean omitComments)
      Create a variant of Properties that sorts properties alphanumerically based on their keys.

      This can be useful when storing the Properties instance in a properties file, since it allows such files to be generated in a repeatable manner with consistent ordering of properties. Comments in generated properties files can also be optionally omitted.

      参数:
      omitComments - true if comments should be omitted when storing properties in a file
      返回:
      a new Properties instance
      从以下版本开始:
      3.0
      另请参阅:
    • createSortedProperties

      public static Properties createSortedProperties(Properties properties, boolean omitComments)
      Create a variant of Properties that sorts properties alphanumerically based on their keys.

      This can be useful when storing the Properties instance in a properties file, since it allows such files to be generated in a repeatable manner with consistent ordering of properties. Comments in generated properties files can also be optionally omitted.

      The returned Properties instance will be populated with properties from the supplied properties object, but default properties from the supplied properties object will not be copied.

      参数:
      properties - the Properties object from which to copy the initial properties
      omitComments - true if comments should be omitted when storing properties in a file
      返回:
      a new Properties instance
      从以下版本开始:
      3.0
      另请参阅:
    • setValue

      public static void setValue(List list, int index, Object element)
      Replaces the element at the specified position in this list with the specified element (optional operation).
      参数:
      list - target list
      index - new element's index
      element - element object
      抛出:
      IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
      NullPointerException - if the specified element is null and this list does not permit null elements
      从以下版本开始:
      3.0
      另请参阅:
    • getElement

      @Nullable public static <T> T getElement(@Nullable List<T> list, int index)
      Returns the element at the specified position in this list.

      list can be null, then returns null

      参数:
      index - index of the element to return
      返回:
      the element at the specified position in this list
      从以下版本开始:
      4.0
      另请参阅:
    • getElement

      @Nullable public static <T> T getElement(@Nullable T[] array, int index)
      Returns the element at the specified position in this list.

      list can be null, then returns null

      参数:
      index - index of the element to return
      返回:
      the element at the specified position in this list
      从以下版本开始:
      4.0
      另请参阅:
    • addAll

      public static void addAll(Collection c, @Nullable Object[] elements)
      Adds all of the specified elements to the specified collection. Elements to be added may be specified individually or as an array. The behavior of this convenience method is identical to that of c.addAll(Arrays.asList(elements)), but this method is likely to run significantly faster under most implementations.

      When elements are specified individually, this method provides a convenient way to add a few elements to an existing collection:

           CollectionUtils.addAll(flavors, "Peaches 'n Plutonium", "Rocky Racoon");
           CollectionUtils.addAll(flavors, null); // add nothing element can be null
       
      类型参数:
      T - the class of the elements to add and of the collection
      参数:
      c - the collection into which elements are to be inserted
      elements - the elements to insert into c
      抛出:
      UnsupportedOperationException - if c does not support the add operation
      NullPointerException - if elements contains one or more null values and c does not permit null elements, or if c or elements are null
      IllegalArgumentException - if some property of a value in elements prevents it from being added to c
      从以下版本开始:
      4.0
      另请参阅:
    • addAll

      public static void addAll(Collection c, @Nullable Enumeration values)
      Adds all of the specified elements to the specified collection. Elements to be added may be specified individually or as Enumeration.
      类型参数:
      T - the class of the elements to add and of the collection
      参数:
      c - the collection into which elements are to be inserted
      values - the elements to insert into c
      抛出:
      UnsupportedOperationException - if c does not support the add operation
      NullPointerException - if elements contains one or more null values and c does not permit null elements, or if c or elements are null
      IllegalArgumentException - if some property of a value in elements prevents it from being added to c
      从以下版本开始:
      4.0
      另请参阅:
    • addAll

      public static void addAll(Collection c, @Nullable Collection elements)
      Adds all of the specified elements to the specified collection. Elements to be added may be specified individually or as an array. The behavior of this convenience method is identical to that of c.addAll(Arrays.asList(elements)), but this method is likely to run significantly faster under most implementations.

      When elements are specified individually, this method provides a convenient way to add a few elements to an existing collection:

           CollectionUtils.addAll(flavors, list);
           CollectionUtils.addAll(flavors, null); // add nothing element can be null
       
      类型参数:
      T - the class of the elements to add and of the collection
      参数:
      c - the collection into which elements are to be inserted
      elements - the elements to insert into c
      抛出:
      UnsupportedOperationException - if c does not support the add operation
      NullPointerException - if elements contains one or more null values and c does not permit null elements, or if c or elements are null
      IllegalArgumentException - if some property of a value in elements prevents it from being added to c
      从以下版本开始:
      4.0
      另请参阅:
    • addAll

      public static void addAll(ArrayHolder c, @Nullable Object[] elements)
      从以下版本开始:
      4.0
    • addAll

      public static void addAll(ArrayHolder c, @Nullable Collection elements)
      从以下版本开始:
      4.0
    • putAll

      public static void putAll(Map target, @Nullable Map mappings)
      Copies all of the mappings from the specified map to target map (optional operation). The effect of this call is equivalent to that of calling put(k, v) on this map once for each mapping from key k to value v in the specified map. The behavior of this operation is undefined if the specified map is modified while the operation is in progress.
      参数:
      mappings - mappings to be stored in target map
      抛出:
      UnsupportedOperationException - if the putAll operation is not supported by this map
      ClassCastException - if the class of a key or value in the specified map prevents it from being stored in this map
      NullPointerException - if the specified map is null, or if this map does not permit null keys or values, and the specified map contains null keys or values
      IllegalArgumentException - if some property of a key or value in the specified map prevents it from being stored in this map
    • iterate

      public static <T> void iterate(@Nullable Enumeration<T> enumeration, Consumer<T> consumer)
      从以下版本开始:
      4.0
    • iterate

      public static <T> void iterate(@Nullable Iterator<T> iterator, Consumer<T> consumer)
      从以下版本开始:
      4.0
    • trimToSize

      public static void trimToSize(@Nullable Object list)
      Trims the capacity of ArrayList instance to be the list's current size. An application can use this operation to minimize the storage of an ArrayList instance.
      从以下版本开始:
      4.0
      另请参阅:
    • reverse

      public static <K, V> void reverse(Map<K,V> source, Map<V,K> target)
      reverse source map key-value to target map value-key
      从以下版本开始:
      4.0
    • filter

      public static <T> void filter(Collection<T> collection, Predicate<? super T> predicate)
      remove the elements of this collection that match the given predicate.
      参数:
      predicate - a predicate to apply to each element to determine if it should be removed
      从以下版本开始:
      4.0
      另请参阅:
    • transform

      public static <T, R> List<R> transform(Collection<? extends T> c, Function<T,R> transformer)
      transform T to R
      类型参数:
      T - value type
      R - transformed value type
      参数:
      c - collection
      transformer - transformer
      从以下版本开始:
      4.0
    • buckets

      public static <K, V> MultiValueMap<K,V> buckets(V[] c, Function<V,K> transformer)
    • buckets

      public static <K, V> MultiValueMap<K,V> buckets(Iterable<V> c, Function<V,K> transformer)
    • firstElement

      @Nullable public static <T> T firstElement(@Nullable Iterable<T> iterable)
      Retrieve the first element of the given Iterable, using SortedSet.first() or otherwise using the iterator.
      参数:
      iterable - the iterable to check (may be null or empty)
      返回:
      the first element, or null if none
      从以下版本开始:
      4.0
      另请参阅:
    • firstElement

      @Nullable public static <T> T firstElement(@Nullable List<T> list)
      Retrieve the first element of the given List, accessing the zero index.
      参数:
      list - the List to check (may be null or empty)
      返回:
      the first element, or null if none
      从以下版本开始:
      4.0
    • firstElement

      @Nullable public static <T> T firstElement(@Nullable T[] array)
      Retrieve the first element of the given Array, accessing the zero index.
      参数:
      array - the array to check (may be null or empty)
      返回:
      the first element, or null if none
      从以下版本开始:
      4.0
    • lastElement

      @Nullable public static <T> T lastElement(@Nullable List<T> list)
      Retrieve the last element of the given List, accessing the highest index.
      参数:
      list - the List to check (may be null or empty)
      返回:
      the last element, or null if none
      从以下版本开始:
      4.0
    • lastElement

      public static <T> T lastElement(@Nullable T[] array)
      Retrieve the last element of the given array, accessing the highest index.
      参数:
      array - the array to check (may be null or empty)
      返回:
      the last element, or null if none
      从以下版本开始:
      4.0
    • lastElement

      @Nullable public static <T> T lastElement(@Nullable Set<T> set)
      Retrieve the last element of the given Set, using SortedSet.last() or otherwise iterating over all elements (assuming a linked set).
      参数:
      set - the Set to check (may be null or empty)
      返回:
      the last element, or null if none
      从以下版本开始:
      4.0
      另请参阅:
    • toArray

      public static <A, E extends A> A[] toArray(@Nullable Enumeration<E> enumeration, A[] array)
      Marshal the elements from the given enumeration into an array of the given type. Enumeration elements must be assignable to the type of the given array. The array returned will be a different instance than the array given.
      从以下版本开始:
      4.0
      另请参阅:
    • toArray

      public static <A, E extends A> A[] toArray(@Nullable Iterator<E> iterator, A[] array)
      Marshal the elements from the given iterator into an array of the given type. Iterator elements must be assignable to the type of the given array. The array returned will be a different instance than the array given.
      从以下版本开始:
      4.0
      另请参阅:
    • toIterator

      public static <E> Iterator<E> toIterator(@Nullable Enumeration<E> enumeration)
      Adapt an Enumeration to an Iterator.
      参数:
      enumeration - the original Enumeration
      返回:
      the adapted Iterator
      从以下版本开始:
      4.0
    • singletonIterator

      public static <E> Iterator<E> singletonIterator(E e)
      从以下版本开始:
      4.0
    • newHashMap

      public static <K, V> HashMap<K,V> newHashMap(int expectedSize)
      Instantiate a new HashMap with an initial capacity that can accommodate the specified number of elements without any immediate resize/rehash operations to be expected.

      This differs from the regular HashMap constructor which takes an initial capacity relative to a load factor but is effectively aligned with the JDK's ConcurrentHashMap(int).

      参数:
      expectedSize - the expected number of elements (with a corresponding capacity to be derived so that no resize/rehash operations are needed)
      从以下版本开始:
      4.0
      另请参阅:
    • newLinkedHashMap

      public static <K, V> LinkedHashMap<K,V> newLinkedHashMap(int expectedSize)
      Instantiate a new LinkedHashMap with an initial capacity that can accommodate the specified number of elements without any immediate resize/rehash operations to be expected.
      参数:
      expectedSize - the expected number of elements (with a corresponding capacity to be derived so that no resize/rehash operations are needed)
      从以下版本开始:
      4.0
      另请参阅:
    • arrayToList

      public static List<?> arrayToList(@Nullable Object source)
      Convert the supplied array into a List. A primitive array gets converted into a List of the appropriate wrapper type.

      NOTE: Generally prefer the standard Arrays.asList(T...) method. This arrayToList method is just meant to deal with an incoming Object value that might be an Object[] or a primitive array at runtime.

      A null source value will be converted to an empty List.

      参数:
      source - the (potentially primitive) array
      返回:
      the converted List result
      从以下版本开始:
      4.0
      另请参阅:
    • mergeArrayIntoCollection

      public static <E> void mergeArrayIntoCollection(@Nullable Object array, Collection<E> collection)
      Merge the given array into the given Collection.
      参数:
      array - the array to merge (may be null)
      collection - the target Collection to merge the array into
      从以下版本开始:
      4.0
    • mergePropertiesIntoMap

      public static <K, V> void mergePropertiesIntoMap(@Nullable Properties props, Map<K,V> map)
      Merge the given Properties instance into the given Map, copying all properties (key-value pairs) over.

      Uses Properties.propertyNames() to even catch default properties linked into the original Properties instance.

      参数:
      props - the Properties instance to merge (may be null)
      map - the target Map to merge the properties into
      从以下版本开始:
      4.0
    • contains

      public static boolean contains(@Nullable Iterator<?> iterator, Object element)
      Check whether the given Iterator contains the given element.
      参数:
      iterator - the Iterator to check
      element - the element to look for
      返回:
      true if found, false otherwise
      从以下版本开始:
      4.0
    • contains

      public static boolean contains(@Nullable Enumeration<?> enumeration, Object element)
      Check whether the given Enumeration contains the given element.
      参数:
      enumeration - the Enumeration to check
      element - the element to look for
      返回:
      true if found, false otherwise
      从以下版本开始:
      4.0
    • contains

      public static boolean contains(@Nullable Iterable<?> iterable, Object element)
      Check whether the given Iterable contains the given element.
      参数:
      iterable - the Iterable to check
      element - the element to look for
      返回:
      true if found, false otherwise
      从以下版本开始:
      4.0
    • containsInstance

      public static boolean containsInstance(@Nullable Collection<?> collection, Object element)
      Check whether the given Collection contains the given element instance.

      Enforces the given instance to be present, rather than returning true for an equal element as well.

      参数:
      collection - the Collection to check
      element - the element to look for
      返回:
      true if found, false otherwise
      从以下版本开始:
      4.0
    • containsAny

      public static boolean containsAny(Collection<?> source, Collection<?> candidates)
      Return true if any element in 'candidates' is contained in 'source'; otherwise returns false.
      参数:
      source - the source Collection
      candidates - the candidates to search for
      返回:
      whether any of the candidates has been found
      从以下版本开始:
      4.0
    • findFirstMatch

      @Nullable public static <E> E findFirstMatch(Collection<?> source, Collection<E> candidates)
      Return the first element in 'candidates' that is contained in 'source'. If no element in 'candidates' is present in 'source' returns null. Iteration order is Collection implementation specific.
      参数:
      source - the source Collection
      candidates - the candidates to search for
      返回:
      the first present object, or null if not found
      从以下版本开始:
      4.0
    • hasUniqueObject

      public static boolean hasUniqueObject(Collection<?> collection)
      Determine whether the given Collection only contains a single unique object.
      参数:
      collection - the Collection to check
      返回:
      true if the collection contains a single reference or multiple references to the same instance, false otherwise
      从以下版本开始:
      4.0
    • findValueOfType

      @Nullable public static <T> T findValueOfType(Collection<?> collection, @Nullable Class<T> type)
      Find a single value of the given type in the given Collection.
      参数:
      collection - the Collection to search
      type - the type to look for
      返回:
      a value of the given type found if there is a clear match, or null if none or more than one such value found
      从以下版本开始:
      4.0
    • findValueOfType

      @Nullable public static Object findValueOfType(Collection<?> collection, Class<?>[] types)
      Find a single value of one of the given types in the given Collection: searching the Collection for a value of the first type, then searching for a value of the second type, etc.
      参数:
      collection - the collection to search
      types - the types to look for, in prioritized order
      返回:
      a value of one of the given types found if there is a clear match, or null if none or more than one such value found
      从以下版本开始:
      4.0