Class Collections

java.lang.Object
com.globalmentor.collections.Collections

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

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static boolean
    addAll(Collection<? super String> stringCollection, Collection<?> collection)
    Adds the string representation of the elements specified by the collection to the specified string collection.
    static <T> boolean
    addAll(Collection<T> collection, Iterable<? extends T> iterable)
    Adds all of the elements specified by the given iterable to the specified collection.
    static <T> boolean
    addAll(Collection<T> collection, Iterator<? extends T> iterator)
    Adds all of the elements specified by the given iterator to the specified collection.
    static <T> boolean
    containsInstance(Collection<T> collection, Class<? extends T> objectClass)
    Determines if the given collection contains an object that is an instance of the given class.
    static <E> 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(Iterable<T> iterable)
    Retrieves the first iterated object, if any, from the iterable.
    static <T> T
    get(Collection<T> collection)
    Retrieves the first iterated object, if any, from the collection.
    static <T> boolean
    isEmpty(Iterable<T> iterable)
    Determines whether the iterable is empty.
    static <T> boolean
    removeAll(Collection<T> collection, Iterable<? extends T> iterable)
    Removes all of the elements specified by the given iterable from the specified collection.
    static <T> boolean
    removeAll(Collection<T> collection, Iterator<? extends T> iterator)
    Removes all of the elements specified by the given iterator from the specified collection.
    static <T> boolean
    set(Collection<T> collection, Collection<? extends T> newCollection)
    Sets the contents of the collection to the contents of the other given collection.
    static <T> boolean
    set(Collection<T> collection, T... newElements)
    Sets the contents of the collection to the contents of the other given collection.
    static <T> Collection<T>
    toCollection(Iterable<T> iterable)
    Converts an iterable to a collection.
    static <T> String
    toString(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> String
    toString(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> String
    toString(Collection<T> collection, 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 Details

    • Collections

      public Collections()
  • Method Details

    • addAll

      public static boolean addAll(Collection<? super String> stringCollection, 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(Collection<T> collection, 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(Collection<T> collection, 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(Collection<T> collection, 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(Collection<T> collection, 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(Collection<T> collection, 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> 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(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:
      NullPointerException - if the given collection is null.
      See Also:
    • get

      public static <T> T get(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:
      NullPointerException - if the given iterable is null.
      See Also:
    • set

      public static <T> boolean set(Collection<T> collection, 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(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(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:
      NullPointerException - if the given iterable is null.
      See Also:
    • toCollection

      public static <T> Collection<T> toCollection(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> String toString(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> String toString(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> String toString(Collection<T> collection, 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.