Class SetUtils
- java.lang.Object
-
- org.apache.jackrabbit.oak.commons.collections.SetUtils
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> Set<T>
difference(Set<T> s1, Set<T> s2)
Returns a new set containing the difference of the two specified sets.static <T> @NotNull Set<T>
intersection(@NotNull Set<T> s1, @NotNull Set<T> s2)
Returns a new set containing the intersection of the two specified sets.static <K> @NotNull Set<K>
newConcurrentHashSet()
Creates a new, emptySet
which is backed byConcurrentHashMap
to allow concurrent access.static <K> @NotNull Set<K>
newConcurrentHashSet(@NotNull Iterable<? extends K> elements)
Creates a newSet
with given values which is backed byConcurrentHashMap
to allow concurrent access.static <K> @NotNull Set<K>
newHashSet(int capacity)
Creates a new, empty HashSet with expected capacity.static <E> @NotNull Set<E>
newIdentityHashSet()
Creates a new, empty IdentityHashSet with default size.static <K> @NotNull Set<K>
newLinkedHashSet(int capacity)
Creates a new, empty LinkedHashSet with expected capacity.static <T> Set<T>
symmetricDifference(Set<T> s1, Set<T> s2)
Returns a new set containing the symmetric difference of the two specified sets.static <T> @NotNull Set<T>
toLinkedSet(@NotNull Iterable<? extends T> iterable)
Convert an iterable to aLinkedHashSet
.static <T> @NotNull Set<T>
toLinkedSet(@NotNull T... elements)
Convert a vararg list of items to a set.static <T> @NotNull Set<T>
toSet(@NotNull Iterable<? extends T> iterable)
Convert an iterable to a set.static <T> @NotNull Set<T>
toSet(@NotNull Iterator<T> iterator)
Convert an iterator to a set.static <T> @NotNull Set<T>
toSet(@NotNull T... elements)
Convert a vararg list of items to a set.static <T extends Comparable>
@NotNull TreeSet<T>toTreeSet(@NotNull Iterable<? extends T> iterable)
Convert an iterable to aTreeSet
.static <T> @NotNull Set<T>
union(@NotNull Set<T> s1, @NotNull Set<T> s2)
Returns a new set containing the union of the two specified sets.
-
-
-
Method Detail
-
toSet
@NotNull public static <T> @NotNull Set<T> toSet(@NotNull @NotNull Iterable<? extends T> iterable)
Convert an iterable to a set. The returning set is mutable and supports all optional operations.- Type Parameters:
T
- the type of the elements- Parameters:
iterable
- the iterable to convert- Returns:
- the set
-
toSet
@NotNull public static <T> @NotNull Set<T> toSet(@NotNull @NotNull Iterator<T> iterator)
Convert an iterator to a set. The returning set is mutable and supports all optional operations.- Type Parameters:
T
- the type of the elements- Parameters:
iterator
- the iterator to convert- Returns:
- the set
-
toSet
@SafeVarargs @NotNull public static <T> @NotNull Set<T> toSet(@NotNull @NotNull T... elements)
Convert a vararg list of items to a set. The returning set is mutable and supports all optional operations.- Type Parameters:
T
- the type of the elements- Parameters:
elements
- elements to convert- Returns:
- the set
-
newHashSet
@NotNull public static <K> @NotNull Set<K> newHashSet(int capacity)
Creates a new, empty HashSet with expected capacity.The returned set is large enough to add expected no. of elements without resizing.
- Parameters:
capacity
- the expected number of elements- Throws:
IllegalArgumentException
- if capacity is negative- See Also:
MapUtils.newHashMap(int)
,newLinkedHashSet(int)
-
newIdentityHashSet
@NotNull public static <E> @NotNull Set<E> newIdentityHashSet()
Creates a new, empty IdentityHashSet with default size.
-
toLinkedSet
@NotNull public static <T> @NotNull Set<T> toLinkedSet(@NotNull @NotNull Iterable<? extends T> iterable)
Convert an iterable to aLinkedHashSet
. The returning set is mutable and supports all optional operations.- Type Parameters:
T
- the type of the elements- Parameters:
iterable
- the iterable to convert- Returns:
- the linkedHashSet
-
toLinkedSet
@SafeVarargs @NotNull public static <T> @NotNull Set<T> toLinkedSet(@NotNull @NotNull T... elements)
Convert a vararg list of items to a set. The returning set is mutable and supports all optional operations.- Type Parameters:
T
- the type of the elements- Parameters:
elements
- elements to convert- Returns:
- the set
-
newLinkedHashSet
@NotNull public static <K> @NotNull Set<K> newLinkedHashSet(int capacity)
Creates a new, empty LinkedHashSet with expected capacity.The returned set is large enough to add expected no. of elements without resizing.
- Parameters:
capacity
- the expected number of elements- Throws:
IllegalArgumentException
- if capacity is negative- See Also:
MapUtils.newHashMap(int)
,newHashSet(int)
-
toTreeSet
@NotNull public static <T extends Comparable> @NotNull TreeSet<T> toTreeSet(@NotNull @NotNull Iterable<? extends T> iterable)
Convert an iterable to aTreeSet
. The returning set is mutable and supports all optional operations.- Type Parameters:
T
- the type of the elements- Parameters:
iterable
- the iterable to convert- Returns:
- the treeSet
- Throws:
ClassCastException
- if the specified object cannot be compared with the elements currently in this setNullPointerException
- if the specified element is null and this set uses natural ordering, or its comparator does not permit null elements
-
newConcurrentHashSet
@NotNull public static <K> @NotNull Set<K> newConcurrentHashSet()
Creates a new, emptySet
which is backed byConcurrentHashMap
to allow concurrent access. Returning Set doesn't allow null keys and values.- Returns:
- a new, empty
Set
which is backed byConcurrentHashMap
. - See Also:
MapUtils.newHashMap(int)
,newLinkedHashSet(int)
,(int)
-
newConcurrentHashSet
@NotNull public static <K> @NotNull Set<K> newConcurrentHashSet(@NotNull @NotNull Iterable<? extends K> elements)
Creates a newSet
with given values which is backed byConcurrentHashMap
to allow concurrent access. Returning Set doesn't allow null keys and values.- Returns:
- a new, empty
Set
which is backed byConcurrentHashMap
. - Throws:
NullPointerException
- if any element of the iterable is null- See Also:
MapUtils.newHashMap(int)
,newLinkedHashSet(int)
,newConcurrentHashSet()
-
union
@NotNull public static <T> @NotNull Set<T> union(@NotNull @NotNull Set<T> s1, @NotNull @NotNull Set<T> s2)
Returns a new set containing the union of the two specified sets. The union of two sets is a set containing all the elements of both sets.- Type Parameters:
T
- the type of elements in the sets- Parameters:
s1
- the first set, must not be nulls2
- the second set, must not be null- Returns:
- a new set containing the union of the two specified sets
- Throws:
NullPointerException
- if either of the sets is null
-
intersection
@NotNull public static <T> @NotNull Set<T> intersection(@NotNull @NotNull Set<T> s1, @NotNull @NotNull Set<T> s2)
Returns a new set containing the intersection of the two specified sets. The intersection of two sets is a set containing only the elements that are present in both sets.- Type Parameters:
T
- the type of elements in the sets- Parameters:
s1
- the first set, must not be nulls2
- the second set, must not be null- Returns:
- a new set containing the intersection of the two specified sets
- Throws:
NullPointerException
- if either of the sets is null
-
symmetricDifference
public static <T> Set<T> symmetricDifference(Set<T> s1, Set<T> s2)
Returns a new set containing the symmetric difference of the two specified sets. The symmetric difference of two sets is a set containing elements that are in either of the sets, but not in their intersection.- Type Parameters:
T
- the type of elements in the sets- Parameters:
s1
- the first set, must not be nulls2
- the second set, must not be null- Returns:
- a new set containing the symmetric difference of the two specified sets
- Throws:
NullPointerException
- if either of the sets is null
-
difference
public static <T> Set<T> difference(Set<T> s1, Set<T> s2)
Returns a new set containing the difference of the two specified sets. The difference of two sets is a set containing elements that are in the first set but not in the second set.- Type Parameters:
T
- the type of elements in the sets- Parameters:
s1
- the first set, must not be nulls2
- the second set, must not be null- Returns:
- a new set containing the difference of the two specified sets
- Throws:
NullPointerException
- if either of the sets is null
-
-