Class SetUtils
java.lang.Object
org.apache.jackrabbit.oak.commons.collections.SetUtils
Utility methods for
Set conversions.-
Method Summary
Modifier and TypeMethodDescriptionstatic <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>Creates a new, emptySetwhich is backed byConcurrentHashMapto allow concurrent access.static <K> @NotNull Set<K>newConcurrentHashSet(@NotNull Iterable<? extends K> elements) Creates a newSetwith given values which is backed byConcurrentHashMapto allow concurrent access.static <K> @NotNull Set<K>newHashSet(int capacity) Creates a new, empty HashSet with expected capacity.static <E> @NotNull Set<E>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>Convert an iterable to a set.static <T> @NotNull Set<T>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>Convert an iterable to aTreeSet.static <T> @NotNull Set<T>Returns a new set containing the union of the two specified sets.
-
Method Details
-
toSet
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
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
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
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:
-
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
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:
-
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
Creates a new, emptySetwhich is backed byConcurrentHashMapto allow concurrent access. Returning Set doesn't allow null keys and values.- Returns:
- a new, empty
Setwhich is backed byConcurrentHashMap. - See Also:
-
newConcurrentHashSet
@NotNull public static <K> @NotNull Set<K> newConcurrentHashSet(@NotNull @NotNull Iterable<? extends K> elements) Creates a newSetwith given values which is backed byConcurrentHashMapto allow concurrent access. Returning Set doesn't allow null keys and values.- Returns:
- a new, empty
Setwhich is backed byConcurrentHashMap. - Throws:
NullPointerException- if any element of the iterable is null- See Also:
-
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
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
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
-