Class SetUtils
java.lang.Object
no.mnemonic.commons.utilities.collections.SetUtils
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Set<T>
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>
Returns the provided set, or a set with the default value if the provided set is empty/nullstatic <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>
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> Creates a set from its arguments using a mapping function converting all values.static <T> Set<T>
Creates a set from an iterator.static <T,
V> Set<V> 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>
Returns the union of multiple sets.
-
Method Details
-
set
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
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
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
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
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
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
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
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
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
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
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
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
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 fromb
- The set to compare with- Returns:
- A new set containing every item in @param a that is not present in @param b.
-
ifEmpty
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 returndefaultValue
- 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
-