Class Collections


  • public class Collections
    extends java.lang.Object
    Various utilities to be used with collections.
    Author:
    Garret Wilson
    See Also:
    Collection
    • Constructor Summary

      Constructors 
      Constructor Description
      Collections()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean addAll​(java.util.Collection<? super java.lang.String> stringCollection, java.util.Collection<?> collection)
      Adds the string representation of the elements specified by the collection to the specified string collection.
      static <T> boolean addAll​(java.util.Collection<T> collection, java.lang.Iterable<? extends T> iterable)
      Adds all of the elements specified by the given iterable to the specified collection.
      static <T> boolean addAll​(java.util.Collection<T> collection, java.util.Iterator<? extends T> iterator)
      Adds all of the elements specified by the given iterator to the specified collection.
      static <T> boolean containsInstance​(java.util.Collection<T> collection, java.lang.Class<? extends T> objectClass)
      Determines if the given collection contains an object that is an instance of the given class.
      static <E> java.util.HashSet<E> createHashSet​(E... elements)
      Creates a hash set of the given generic type and initializes it with the given elements.
      static <T> T get​(java.lang.Iterable<T> iterable)
      Retrieves the first iterated object, if any, from the iterable.
      static <T> T get​(java.util.Collection<T> collection)
      Retrieves the first iterated object, if any, from the collection.
      static <T> boolean isEmpty​(java.lang.Iterable<T> iterable)
      Determines whether the iterable is empty.
      static <T> boolean removeAll​(java.util.Collection<T> collection, java.lang.Iterable<? extends T> iterable)
      Removes all of the elements specified by the given iterable from the specified collection.
      static <T> boolean removeAll​(java.util.Collection<T> collection, java.util.Iterator<? extends T> iterator)
      Removes all of the elements specified by the given iterator from the specified collection.
      static <T> boolean set​(java.util.Collection<T> collection, java.util.Collection<? extends T> newCollection)
      Sets the contents of the collection to the contents of the other given collection.
      static <T> boolean set​(java.util.Collection<T> collection, T... newElements)
      Sets the contents of the collection to the contents of the other given collection.
      static <T> java.util.Collection<T> toCollection​(java.lang.Iterable<T> iterable)
      Converts an iterable to a collection.
      static <T> java.lang.String toString​(java.util.Collection<T> collection)
      Converts a collection to a string by concatenating the string values of each member of the collection, separated by a comma.
      static <T> java.lang.String toString​(java.util.Collection<T> collection, char delimiter)
      Converts a collection to a string by concatenating the string values of each member of the collection, separated by the given delimiter character.
      static <T> java.lang.String toString​(java.util.Collection<T> collection, java.lang.String delimiter)
      Converts a collection to a string by concatenating the string values of each member of the collection, separated by the given delimiter.
      • 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

      • addAll

        public static boolean addAll​(java.util.Collection<? super java.lang.String> stringCollection,
                                     java.util.Collection<?> collection)
        Adds the string representation of the elements specified by the collection to the specified string collection.
        Parameters:
        stringCollection - The collection to which objects will be added.
        collection - The source of the added strings.
        Returns:
        true if the collection changed as a result of the call.
      • addAll

        public static <T> boolean addAll​(java.util.Collection<T> collection,
                                         java.lang.Iterable<? extends T> iterable)
        Adds all of the elements specified by the given iterable to the specified collection.
        Type Parameters:
        T - The type of the elements contained on the collection.
        Parameters:
        collection - The collection to which objects will be added.
        iterable - The source of the added objects.
        Returns:
        true if the collection changed as a result of the call.
      • addAll

        public static <T> boolean addAll​(java.util.Collection<T> collection,
                                         java.util.Iterator<? extends T> iterator)
        Adds all of the elements specified by the given iterator to the specified collection.
        Type Parameters:
        T - The type of the elements contained on the collection.
        Parameters:
        collection - The collection to which objects will be added.
        iterator - The source of the added objects.
        Returns:
        true if the collection changed as a result of the call.
      • removeAll

        public static <T> boolean removeAll​(java.util.Collection<T> collection,
                                            java.lang.Iterable<? extends T> iterable)
        Removes all of the elements specified by the given iterable from the specified collection.
        Type Parameters:
        T - The type of the elements contained on the collection.
        Parameters:
        collection - The collection from which objects will be removed.
        iterable - The source of the removed objects.
        Returns:
        true if the collection changed as a result of the call.
      • removeAll

        public static <T> boolean removeAll​(java.util.Collection<T> collection,
                                            java.util.Iterator<? extends T> iterator)
        Removes all of the elements specified by the given iterator from the specified collection.
        Type Parameters:
        T - The type of the elements contained on the collection.
        Parameters:
        collection - The collection from which objects will be removed.
        iterator - The source of the removed objects.
        Returns:
        true if the collection changed as a result of the call.
      • containsInstance

        public static <T> boolean containsInstance​(java.util.Collection<T> collection,
                                                   java.lang.Class<? extends T> objectClass)
        Determines if the given collection contains an object that is an instance of the given class.
        Type Parameters:
        T - The type of the elements contained on the collection.
        Parameters:
        collection - The collection to search.
        objectClass - The class for which to find an implementing object.
        Returns:
        true if the collection contains an object that implements the given class, else false.
      • createHashSet

        public static <E> java.util.HashSet<E> createHashSet​(E... elements)
        Creates a hash set of the given generic type and initializes it with the given elements.
        Type Parameters:
        E - The type of elements the hash set will contain.
        Parameters:
        elements - The elements with which to initialize the hash set.
        Returns:
        A new hash set containing the given elements.
      • get

        public static <T> T get​(java.util.Collection<T> collection)
        Retrieves the first iterated object, if any, from the collection.
        Type Parameters:
        T - The type of object stored in the collection.
        Parameters:
        collection - The collection from which the object should be retrieved.
        Returns:
        The first iterated object from the collection, or null if the collection is empty.
        Throws:
        java.lang.NullPointerException - if the given collection is null.
        See Also:
        Collection.isEmpty(), Collection.iterator()
      • get

        public static <T> T get​(java.lang.Iterable<T> iterable)
        Retrieves the first iterated object, if any, from the iterable.
        Type Parameters:
        T - The type of object stored in the iterable.
        Parameters:
        iterable - The iterable from which the object should be retrieved.
        Returns:
        The first iterated object from the iterable, or null if the iterable is empty.
        Throws:
        java.lang.NullPointerException - if the given iterable is null.
        See Also:
        Collection.isEmpty(), Iterable.iterator()
      • set

        public static <T> boolean set​(java.util.Collection<T> collection,
                                      java.util.Collection<? extends T> newCollection)
        Sets the contents of the collection to the contents of the other given collection. This is a convenience method for Collection.clear() followed by Collection.addAll(Collection).
        Type Parameters:
        T - The type of objects in the collection.
        Parameters:
        collection - The collection to set.
        newCollection - The elements to set in the collection.
        Returns:
        true if the collection changed as a result of the call.
      • set

        public static <T> boolean set​(java.util.Collection<T> collection,
                                      T... newElements)
        Sets the contents of the collection to the contents of the other given collection. This is a convenience method for Collection.clear() followed by Collections.addAll(Collection, Object...).
        Type Parameters:
        T - The type of objects in the collection.
        Parameters:
        collection - The collection to set.
        newElements - The elements to set in the collection.
        Returns:
        true if the collection changed as a result of the call.
      • isEmpty

        public static <T> boolean isEmpty​(java.lang.Iterable<T> iterable)
        Determines whether the iterable is empty.
        Type Parameters:
        T - The type of object stored in the iterable.
        Parameters:
        iterable - The iterable from which the object would be retrieved.
        Returns:
        true if an iterator returned from the iterable would return zero elements.
        Throws:
        java.lang.NullPointerException - if the given iterable is null.
        See Also:
        Collection.isEmpty(), Iterable.iterator()
      • toCollection

        public static <T> java.util.Collection<T> toCollection​(java.lang.Iterable<T> iterable)
        Converts an iterable to a collection. If the iterable is already a collection, it is returned as such. If the the iterable is not already a collection, a new collection is returned with the contents of the iterable.
        Type Parameters:
        T - The type of object in the iterable.
        Parameters:
        iterable - The iterable to convert.
        Returns:
        A collection with the current contents of the iterable.
      • toString

        public static <T> java.lang.String toString​(java.util.Collection<T> collection)
        Converts a collection to a string by concatenating the string values of each member of the collection, separated by a comma.
        Type Parameters:
        T - The type of the elements contained on the collection.
        Parameters:
        collection - The collection to convert to a string.
        Returns:
        A string representation of the collection of elements.
      • toString

        public static <T> java.lang.String toString​(java.util.Collection<T> collection,
                                                    char delimiter)
        Converts a collection to a string by concatenating the string values of each member of the collection, separated by the given delimiter character.
        Type Parameters:
        T - The type of the elements contained on the collection.
        Parameters:
        collection - The collection to convert to a string.
        delimiter - The character to place between elements.
        Returns:
        A string representation of the collection of elements.
      • toString

        public static <T> java.lang.String toString​(java.util.Collection<T> collection,
                                                    java.lang.String delimiter)
        Converts a collection to a string by concatenating the string values of each member of the collection, separated by the given delimiter.
        Type Parameters:
        T - The type of the elements contained on the collection.
        Parameters:
        collection - The collection to convert to a string.
        delimiter - The character sequence to place between elements.
        Returns:
        A string representation of the collection of elements.