Package org.drasyl.util
Class SetUtil
- java.lang.Object
-
- org.drasyl.util.SetUtil
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <A,B>
Set<Pair<A,B>>cartesianProduct(Set<A> a, Set<B> b)
Returns the cartesian product of the two setsa
andb
.static <E> Set<E>
difference(Set<E> a, E... b)
Returns a set containing all elements from seta
that are notb
.static <E> Set<E>
difference(Set<E> a, Collection<E> b)
Returns a set containing all elements from seta
that are not in setb
.static <E> E
firstElement(Set<E> set)
Returns the first element from setset
.static <E> Set<E>
merge(Set<E> a, E... b)
Returns a set containing all elements froma
and the elementb
.static <E> Set<E>
merge(Set<E> a, Set<E> b)
Returns a set containing all elements from seta
and setb
.static <E> E
nthElement(Set<E> set, int n)
Returns then
-th element from setset
.
-
-
-
Method Detail
-
merge
public static <E> Set<E> merge(Set<E> a, Set<E> b)
Returns a set containing all elements from seta
and setb
. If there are duplicates in both sets, the elements from the seta
are favored.- Type Parameters:
E
- theSet
's element type- Parameters:
a
- set ab
- set b- Returns:
- a
Set
containing the specified elements
-
merge
@SafeVarargs public static <E> Set<E> merge(Set<E> a, E... b)
Returns a set containing all elements froma
and the elementb
.b
is ignored if an equal element is already contained in the seta
.- Type Parameters:
E
- theSet
's element type- Parameters:
a
- set ab
- set b- Returns:
- a
Set
containing the specified elements
-
difference
public static <E> Set<E> difference(Set<E> a, Collection<E> b)
Returns a set containing all elements from seta
that are not in setb
.- Type Parameters:
E
- theSet
's element type- Parameters:
a
- set ab
- set b- Returns:
- a
Set
containing the specified elements
-
difference
@SafeVarargs public static <E> Set<E> difference(Set<E> a, E... b)
Returns a set containing all elements from seta
that are notb
.- Type Parameters:
E
- theSet
's element type- Parameters:
a
- set ab
- set b- Returns:
- a
Set
containing the specified elements
-
nthElement
public static <E> E nthElement(Set<E> set, int n)
Returns then
-th element from setset
. Throws aIndexOutOfBoundsException
ifn
is negative or greater than the set's cardinality.- Type Parameters:
E
- theSet
's element type- Parameters:
set
- a setn
- specifies the element to be taken- Returns:
n
-th element from setset
- Throws:
IndexOutOfBoundsException
- ifn
is negative or greater than the set's cardinality
-
firstElement
public static <E> E firstElement(Set<E> set)
Returns the first element from setset
. Returnsnull
if set is empty.- Type Parameters:
E
- theSet
's element type- Parameters:
set
- a set- Returns:
- first element from set
set
-
cartesianProduct
public static <A,B> Set<Pair<A,B>> cartesianProduct(Set<A> a, Set<B> b)
Returns the cartesian product of the two setsa
andb
. This is the set of all orderedPair
s(x,y)
wherex
is ina
andy
is inb
.- Type Parameters:
A
- the type of the elements in seta
B
- the type of the elements in setb
- Parameters:
a
- first set for the cartesian productb
- second set for the cartesian product- Returns:
- the cartesian product of the two sets
a
andb
-
-