java.lang.Object
no.mnemonic.commons.utilities.collections.SetUtils

public class SetUtils extends Object
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> Set<T>
    addToSet(Set<T> set, T element)
    Adds an element to a set unless the element is null.
    static <T> Set<T>
    difference(Set<T> a, Set<T> b)
    Subtracts every item from @param a that is also present in @param b.
    static <T> Set<T>
    ifEmpty(Set<T> values, T defaultValue)
    Returns the provided set, or a set with the default value if the provided set is empty/null
    static <T> boolean
    in(T value, T... values)
    Returns true if 'value' is among the argument list of 'values'.
    static <T> Set<T>
    intersection(Set<T>... sets)
    Returns the intersection of multiple sets.
    static <T> boolean
    intersects(Set<T>... sets)
    Tests if multiple sets have common values, i.e.
    static <T> Set<T>
    modifySet(Set<T> originSet, Set<T> addSet, Set<T> removeSet)
    Applies add/remove modifications to a set.
    static <T> Set<T>
    set(Collection<T> collection)
    Creates a set from another collection.
    static <T, V> Set<V>
    set(Collection<T> collection, Function<T,V> mapping)
    Creates a set from another collection using a mapping function converting all values.
    static <T, V> Set<V>
    set(Function<T,V> mapping, T... values)
    Creates a set from its arguments using a mapping function converting all values.
    static <T> Set<T>
    set(Iterator<T> iterator)
    Creates a set from an iterator.
    static <T, V> Set<V>
    set(Iterator<T> iterator, Function<T,V> mapping)
    Creates a set from an iterator using a mapping function converting all values.
    static <T> Set<T>
    set(T... values)
    Creates a set from its arguments.
    static <T> Set<T>
    union(Set<T>... sets)
    Returns the union of multiple sets.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • set

      @SafeVarargs public static <T> Set<T> set(T... values)
      Creates a set from its arguments.
      Type Parameters:
      T - Type of value parameters.
      Parameters:
      values - Values to be added to the set.
      Returns:
      A set containing all values.
    • set

      public static <T> Set<T> set(Iterator<T> iterator)
      Creates a set from an iterator.
      Type Parameters:
      T - Type of iterator values.
      Parameters:
      iterator - An iterator which values are added to the set.
      Returns:
      A set containing all values supplied by the given iterator.
    • set

      public static <T> Set<T> set(Collection<T> collection)
      Creates a set from another collection.
      Type Parameters:
      T - Type of collection values.
      Parameters:
      collection - A collection which values are added to the set.
      Returns:
      A set containing all values contained in the given collection, omitting null elements.
    • set

      @SafeVarargs public static <T, V> Set<V> set(Function<T,V> mapping, T... values)
      Creates a set from its arguments using a mapping function converting all values.
      Type Parameters:
      T - Type of value parameters before conversion.
      V - Type of values in the returned set after conversion.
      Parameters:
      mapping - A mapping function applied to all values.
      values - Values to be added to the set.
      Returns:
      A set containing all values converted using the mapping function.
    • set

      public static <T, V> Set<V> set(Iterator<T> iterator, Function<T,V> mapping)
      Creates a set from an iterator using a mapping function converting all values.
      Type Parameters:
      T - Type of iterator values before conversion.
      V - Type of values in the returned set after conversion.
      Parameters:
      iterator - An iterator which values are added to the set.
      mapping - A mapping function applied to all values.
      Returns:
      A set containing all values supplied by the given iterator and converted using the mapping function.
    • set

      public static <T, V> Set<V> set(Collection<T> collection, Function<T,V> mapping)
      Creates a set from another collection using a mapping function converting all values.
      Type Parameters:
      T - Type of collection values before conversion.
      V - Type of values in the returned set after conversion.
      Parameters:
      collection - A collection which values are added to the set.
      mapping - A mapping function applied to all values.
      Returns:
      A set containing all values contained in the given collection and converted using the mapping function, omitting null elements.
    • addToSet

      public static <T> Set<T> addToSet(Set<T> set, T element)
      Adds an element to a set unless the element is null.

      A new set is created if the provided set is null.

      Type Parameters:
      T - Type of elements.
      Parameters:
      set - Set to which the element will be added.
      element - Element to add.
      Returns:
      Set including added element.
    • in

      @SafeVarargs public static <T> boolean in(T value, T... values)
      Returns true if 'value' is among the argument list of 'values'.
      Type Parameters:
      T - Type of values.
      Parameters:
      value - Value to find.
      values - Values to search through.
      Returns:
      True if 'value' is present in the argument list.
    • intersection

      @SafeVarargs public static <T> Set<T> intersection(Set<T>... sets)
      Returns the intersection of multiple sets.

      The original sets are not altered.

      Type Parameters:
      T - Type of values.
      Parameters:
      sets - Multiple sets which will be intersected.
      Returns:
      A set forming the intersection of the given sets.
    • intersects

      @SafeVarargs public static <T> boolean intersects(Set<T>... sets)
      Tests if multiple sets have common values, i.e. their intersection is not empty.
      Type Parameters:
      T - Type of values.
      Parameters:
      sets - Multiple sets to test for common values.
      Returns:
      True if the given sets intersect.
    • union

      @SafeVarargs public static <T> Set<T> union(Set<T>... sets)
      Returns the union of multiple sets.

      The original sets are not altered.

      Type Parameters:
      T - Type of values.
      Parameters:
      sets - Multiple sets which will be united.
      Returns:
      A set forming the union of the given sets.
    • modifySet

      public static <T> Set<T> modifySet(Set<T> originSet, Set<T> addSet, Set<T> removeSet)
      Applies add/remove modifications to a set.

      This operation permits adding to and removing from one set in a single operation.

      Type Parameters:
      T - Type of values.
      Parameters:
      originSet - Set to be modified.
      addSet - Set containing elements to be added.
      removeSet - Set containing elements to be removed.
      Returns:
      Same instance of 'originSet' passed in after modification.
    • difference

      public static <T> Set<T> difference(Set<T> a, Set<T> b)
      Subtracts every item from @param a that is also present in @param b. Items present in @param b, but not in @param a are ignored.
      Type Parameters:
      T - The type of values
      Parameters:
      a - The set to subtract items from
      b - The set to compare with
      Returns:
      A new set containing every item in @param a that is not present in @param b.
    • ifEmpty

      public static <T> Set<T> ifEmpty(Set<T> values, T defaultValue)
      Returns the provided set, or a set with the default value if the provided set is empty/null
      Type Parameters:
      T - value type
      Parameters:
      values - the values to return
      defaultValue - the default value to return if values is null/empty
      Returns:
      values if set is not empty, or set(defaultValue) if values is empty or null