类 CollectionUtils

java.lang.Object
com.alibaba.nacos.common.utils.CollectionUtils

public final class CollectionUtils extends Object
Copy from org.apache.commons.collections.
作者:
liaochuntao
  • 方法概要

    修饰符和类型
    方法
    说明
    static <T> boolean
    contains(Collection<T> coll, T target)
    Whether contain item in collection.
    static Object
    get(Object object, int index)
    Returns the index-th value in object, throwing IndexOutOfBoundsException if there is no such element or IllegalArgumentException if object is not an instance of one of the supported types.
    static Map
    Returns a Map mapping each unique element in the given Collection to an Integer representing the number of occurrences of that element in the Collection.
    static <T> T
    getOnlyElement(Iterable<T> iterable)
    return the first element, if the iterator contains multiple elements, will throw IllegalArgumentException.
    static <T> T
    getOrDefault(Object obj, int index, T defaultValue)
    Returns the value to which the specified index , or defaultValue if this collection contains no value for the index.
    static boolean
    Null-safe check if the specified collection is empty.
    static boolean
    Returns true iff the given Collections contain exactly the same elements with exactly the same cardinalities.
    static boolean
    isMapEmpty(Map<?,?> map)
    Return true if the supplied Map is null or empty.
    static boolean
    Null-safe check if the specified collection is not empty.
    static <T> List<T>
    list(T... elements)
    return an arraylist containing all input parameters.
    static <T> Set<T>
    set(T... elements)
    Return a set containing all input parameters.
    static int
    size(Object object)
    Gets the size of the collection/iterator specified.
    static boolean
    Judge whether object is empty.

    从类继承的方法 java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 方法详细资料

    • get

      public static Object get(Object object, int index)
      Returns the index-th value in object, throwing IndexOutOfBoundsException if there is no such element or IllegalArgumentException if object is not an instance of one of the supported types.

      The supported types, and associated semantics are:

      • Map -- the value returned is the Map.Entry in position index in the map's entrySet iterator, if there is such an entry.
      • List -- this method is equivalent to the list's get method.
      • Array -- the index-th array entry is returned, if there is such an entry; otherwise an IndexOutOfBoundsException is thrown.
      • Collection -- the value returned is the index-th object returned by the collection's default iterator, if there is such an element.
      • Iterator or Enumeration -- the value returned is the index-th object in the Iterator/Enumeration, if there is such an element. The Iterator/Enumeration is advanced to index (or to the end, if index exceeds the number of entries) as a side effect of this method.
      参数:
      object - the object to get a value from
      index - the index to get
      返回:
      the object at the specified index
      抛出:
      IndexOutOfBoundsException - if the index is invalid
      IllegalArgumentException - if the object type is invalid
    • size

      public static int size(Object object)
      Gets the size of the collection/iterator specified.

      This method can handles objects as follows

      • Collection - the collection size
      • Map - the map size
      • Array - the array size
      • Iterator - the number of elements remaining in the iterator
      • Enumeration - the number of elements remaining in the enumeration
      参数:
      object - the object to get the size of
      返回:
      the size of the specified collection
      抛出:
      IllegalArgumentException - thrown if object is not recognised or null
      从以下版本开始:
      Commons Collections 3.1
    • sizeIsEmpty

      public static boolean sizeIsEmpty(Object object)
      Judge whether object is empty.
      参数:
      object - object
      返回:
      true if object is empty, otherwise false
      抛出:
      IllegalArgumentException - if object has no length or size
    • contains

      public static <T> boolean contains(Collection<T> coll, T target)
      Whether contain item in collection.
      类型参数:
      T - General Type
      参数:
      coll - collection
      target - target value
      返回:
      true if contain, otherwise false
    • isEmpty

      public static boolean isEmpty(Collection coll)
      Null-safe check if the specified collection is empty.

      Null returns true.

      参数:
      coll - the collection to check, may be null
      返回:
      true if empty or null
      从以下版本开始:
      Commons Collections 3.2
    • isNotEmpty

      public static boolean isNotEmpty(Collection coll)
      Null-safe check if the specified collection is not empty.

      Null returns false.

      参数:
      coll - the collection to check, may be null
      返回:
      true if non-null and non-empty
      从以下版本开始:
      Commons Collections 3.2
    • getOrDefault

      public static <T> T getOrDefault(Object obj, int index, T defaultValue)
      Returns the value to which the specified index , or defaultValue if this collection contains no value for the index.
      类型参数:
      T - General Type
      参数:
      obj - the object to get a value from
      index - the index to get
      defaultValue - default value
      返回:
      the value to which the specified index , or defaultValue if this collection contains no value for the index.
    • list

      public static <T> List<T> list(T... elements)
      return an arraylist containing all input parameters.
      参数:
      elements - element array
      返回:
      arraylist containing all input parameters
    • set

      public static <T> Set<T> set(T... elements)
      Return a set containing all input parameters.
      参数:
      elements - elements element array
      返回:
      set containing all input parameters
    • getOnlyElement

      public static <T> T getOnlyElement(Iterable<T> iterable)
      return the first element, if the iterator contains multiple elements, will throw IllegalArgumentException.
      抛出:
      NoSuchElementException - if the iterator is empty
      IllegalArgumentException - if the iterator contains multiple elements. The state of the iterator is unspecified.
    • isMapEmpty

      public static boolean isMapEmpty(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
    • getCardinalityMap

      public static Map getCardinalityMap(Collection coll)
      Returns a Map mapping each unique element in the given Collection to an Integer representing the number of occurrences of that element in the Collection.

      Only those elements present in the collection will appear as keys in the map.

      参数:
      coll - the collection to get the cardinality map for, must not be null
      返回:
      the populated cardinality map
    • isEqualCollection

      public static boolean isEqualCollection(Collection a, Collection b)
      Returns true iff the given Collections contain exactly the same elements with exactly the same cardinalities.

      That is, iff the cardinality of e in a is equal to the cardinality of e in b, for each element e in a or b.

      参数:
      a - the first collection, must not be null
      b - the second collection, must not be null
      返回:
      true iff the collections contain the same elements with the same cardinalities.