Class Collections


  • public abstract class Collections
    extends java.lang.Object
    • Constructor Summary

      Constructors 
      Constructor Description
      Collections()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.List arrayToList​(java.lang.Object source)
      Convert the supplied array into a List.
      static boolean contains​(java.util.Enumeration enumeration, java.lang.Object element)
      Check whether the given Enumeration contains the given element.
      static boolean contains​(java.util.Iterator iterator, java.lang.Object element)
      Check whether the given Iterator contains the given element.
      static boolean containsAny​(java.util.Collection source, java.util.Collection candidates)
      Return true if any element in 'candidates' is contained in 'source'; otherwise returns false.
      static boolean containsInstance​(java.util.Collection collection, java.lang.Object element)
      Check whether the given Collection contains the given element instance.
      static java.lang.Class<?> findCommonElementType​(java.util.Collection collection)
      Find the common element type of the given Collection, if any.
      static java.lang.Object findFirstMatch​(java.util.Collection source, java.util.Collection candidates)
      Return the first element in 'candidates' that is contained in 'source'.
      static <T> T findFirstValueOfType​(java.util.Collection<?> collection, java.lang.Class<T> type)
      Find the first single value of the given type in the given Collection or null if none was found
      static java.lang.Object findValueOfType​(java.util.Collection<?> collection, java.lang.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.
      static boolean hasUniqueObject​(java.util.Collection collection)
      Determine whether the given Collection only contains a single unique object.
      static boolean isEmpty​(java.util.Collection collection)
      Return true if the supplied Collection is null or empty.
      static boolean isEmpty​(java.util.Map map)
      Return true if the supplied Map is null or empty.
      static void mergeArrayIntoCollection​(java.lang.Object array, java.util.Collection collection)
      Merge the given array into the given Collection.
      static void mergePropertiesIntoMap​(java.util.Properties props, java.util.Map map)
      Merge the given Properties instance into the given Map, copying all properties (key-value pairs) over.
      static int size​(java.util.Collection collection)
      Returns the collection's size or 0 if the collection is null.
      static int size​(java.util.Map map)
      Returns the map's size or 0 if the map is null.
      static <T> T[] toArray​(java.util.Collection<T> c, java.lang.Class<T> type)  
      static <A,​E extends A>
      A[]
      toArray​(java.util.Enumeration<E> enumeration, A[] array)
      Marshal the elements from the given enumeration into an array of the given type.
      static <E> java.util.Iterator<E> toIterator​(java.util.Enumeration<E> enumeration)
      Adapt an enumeration to an iterator.
      static <T> java.util.List<T> toList​(java.util.Collection<T> elements)
      a new List that contains the specified elements or an empty collection if the elements are null or empty.
      static <T> java.util.List<T> toList​(T... elements)
      a new List that contains the specified elements or an empty collection if the elements are null or empty.
      static <E> java.util.Set<E> toSet​(E... elements)
      Returns a new Set that contains the specified elements or an empty Set if the elements are null or empty.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Collections

        public Collections()
    • Method Detail

      • isEmpty

        public static boolean isEmpty​(java.util.Collection collection)
        Return true if the supplied Collection is null or empty. Otherwise, return false.
        Parameters:
        collection - the Collection to check
        Returns:
        whether the given Collection is empty
      • size

        public static int size​(java.util.Collection collection)
        Returns the collection's size or 0 if the collection is null.
        Parameters:
        collection - the collection to check.
        Returns:
        the collection's size or 0 if the collection is null.
      • size

        public static int size​(java.util.Map map)
        Returns the map's size or 0 if the map is null.
        Parameters:
        map - the map to check
        Returns:
        the map's size or 0 if the map is null.
      • isEmpty

        public static boolean isEmpty​(java.util.Map map)
        Return true if the supplied Map is null or empty. Otherwise, return false.
        Parameters:
        map - the Map to check
        Returns:
        whether the given Map is empty
      • arrayToList

        public static java.util.List arrayToList​(java.lang.Object source)
        Convert the supplied array into a List. A primitive array gets converted into a List of the appropriate wrapper type.

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

        Parameters:
        source - the (potentially primitive) array
        Returns:
        the converted List result
        See Also:
        Objects.toObjectArray(Object)
      • toList

        public static <T> java.util.List<T> toList​(T... elements)
        a new List that contains the specified elements or an empty collection if the elements are null or empty.
        Type Parameters:
        T - the type of elements in the collection
        Parameters:
        elements - the elements to put in the list.
        Returns:
        a new List that contains the specified elements or an empty collection if the elements are null or empty.
      • toList

        public static <T> java.util.List<T> toList​(java.util.Collection<T> elements)
        a new List that contains the specified elements or an empty collection if the elements are null or empty.
        Type Parameters:
        T - the type of elements in the collection
        Parameters:
        elements - the elements to put in the list.
        Returns:
        a new List that contains the specified elements or an empty collection if the elements are null or empty.
      • toSet

        public static <E> java.util.Set<E> toSet​(E... elements)
        Returns a new Set that contains the specified elements or an empty Set if the elements are null or empty.
        Type Parameters:
        E - the type of elements in the set
        Parameters:
        elements - elements to add to the new set
        Returns:
        a new Set that contains the specified elements or an empty Set if the elements are null or empty.
      • mergeArrayIntoCollection

        public static void mergeArrayIntoCollection​(java.lang.Object array,
                                                    java.util.Collection collection)
        Merge the given array into the given Collection.
        Parameters:
        array - the array to merge (may be null)
        collection - the target Collection to merge the array into
      • mergePropertiesIntoMap

        public static void mergePropertiesIntoMap​(java.util.Properties props,
                                                  java.util.Map 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.

        Parameters:
        props - the Properties instance to merge (may be null)
        map - the target Map to merge the properties into
      • contains

        public static boolean contains​(java.util.Iterator iterator,
                                       java.lang.Object element)
        Check whether the given Iterator contains the given element.
        Parameters:
        iterator - the Iterator to check
        element - the element to look for
        Returns:
        true if found, false else
      • contains

        public static boolean contains​(java.util.Enumeration enumeration,
                                       java.lang.Object element)
        Check whether the given Enumeration contains the given element.
        Parameters:
        enumeration - the Enumeration to check
        element - the element to look for
        Returns:
        true if found, false else
      • containsInstance

        public static boolean containsInstance​(java.util.Collection collection,
                                               java.lang.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.

        Parameters:
        collection - the Collection to check
        element - the element to look for
        Returns:
        true if found, false else
      • containsAny

        public static boolean containsAny​(java.util.Collection source,
                                          java.util.Collection candidates)
        Return true if any element in 'candidates' is contained in 'source'; otherwise returns false.
        Parameters:
        source - the source Collection
        candidates - the candidates to search for
        Returns:
        whether any of the candidates has been found
      • findFirstMatch

        public static java.lang.Object findFirstMatch​(java.util.Collection source,
                                                      java.util.Collection 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.
        Parameters:
        source - the source Collection
        candidates - the candidates to search for
        Returns:
        the first present object, or null if not found
      • findFirstValueOfType

        public static <T> T findFirstValueOfType​(java.util.Collection<?> collection,
                                                 java.lang.Class<T> type)
        Find the first single value of the given type in the given Collection or null if none was found
        Type Parameters:
        T - type type of object to find
        Parameters:
        collection - the Collection to search
        type - the type to look for
        Returns:
        a value of the given type found or null if none found.
      • findValueOfType

        public static java.lang.Object findValueOfType​(java.util.Collection<?> collection,
                                                       java.lang.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.
        Parameters:
        collection - the collection to search
        types - the types to look for, in prioritized order
        Returns:
        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
      • hasUniqueObject

        public static boolean hasUniqueObject​(java.util.Collection collection)
        Determine whether the given Collection only contains a single unique object.
        Parameters:
        collection - the Collection to check
        Returns:
        true if the collection contains a single reference or multiple references to the same instance, false else
      • findCommonElementType

        public static java.lang.Class<?> findCommonElementType​(java.util.Collection collection)
        Find the common element type of the given Collection, if any.
        Parameters:
        collection - the Collection to check
        Returns:
        the common element type, or null if no clear common type has been found (or the collection was empty)
      • toArray

        public static <A,​E extends A> A[] toArray​(java.util.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.
        Type Parameters:
        A - type of array
        E - type of enumeration
        Parameters:
        enumeration - source enumeration
        array - the array into which the elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
        Returns:
        an array with the contents of the enumeration
      • toArray

        public static <T> T[] toArray​(java.util.Collection<T> c,
                                      java.lang.Class<T> type)
      • toIterator

        public static <E> java.util.Iterator<E> toIterator​(java.util.Enumeration<E> enumeration)
        Adapt an enumeration to an iterator.
        Type Parameters:
        E - type of enumeration
        Parameters:
        enumeration - the enumeration
        Returns:
        the iterator