@Private
public final class Sets
extends java.lang.Object
Set instances.
This class is Hadoop's internal use alternative to Guava's Sets
utility class.
Javadocs for majority of APIs in this class are taken from Guava's Sets
class from Guava release version 27.0-jre.| Modifier and Type | Method | Description |
|---|---|---|
static <E> java.util.Set<E> |
difference(java.util.Set<E> set1,
java.util.Set<E> set2) |
Returns the difference of two sets as an unmodifiable set.
|
static <E> java.util.Set<E> |
differenceInTreeSets(java.util.Set<E> set1,
java.util.Set<E> set2) |
Returns the difference of two sets as an unmodifiable set.
|
static <E> java.util.Set<E> |
intersection(java.util.Set<E> set1,
java.util.Set<E> set2) |
Returns the intersection of two sets as an unmodifiable set.
|
static <E> java.util.Set<E> |
newConcurrentHashSet() |
Creates a thread-safe set backed by a hash map.
|
static <E> java.util.HashSet<E> |
newHashSet() |
Creates a mutable, initially empty
HashSet instance. |
static <E> java.util.HashSet<E> |
newHashSet(E... elements) |
Creates a mutable
HashSet instance initially containing
the given elements. |
static <E> java.util.HashSet<E> |
newHashSet(java.lang.Iterable<? extends E> elements) |
Creates a mutable
HashSet instance containing the given
elements. |
static <E> java.util.HashSet<E> |
newHashSet(java.util.Iterator<? extends E> elements) |
Creates a mutable
HashSet instance containing the given
elements. |
static <E> java.util.HashSet<E> |
newHashSetWithExpectedSize(int expectedSize) |
Returns a new hash set using the smallest initial table size that can hold
expectedSize elements without resizing. |
static <E extends java.lang.Comparable> |
newTreeSet() |
Creates a mutable, empty
TreeSet instance sorted by the
natural sort ordering of its elements. |
static <E extends java.lang.Comparable> |
newTreeSet(java.lang.Iterable<? extends E> elements) |
Creates a mutable
TreeSet instance containing the given
elements sorted by their natural ordering. |
static <E> java.util.Set<E> |
symmetricDifference(java.util.Set<E> set1,
java.util.Set<E> set2) |
Returns the symmetric difference of two sets as an unmodifiable set.
|
static <E> java.util.Set<E> |
union(java.util.Set<E> set1,
java.util.Set<E> set2) |
Returns the union of two sets as an unmodifiable set.
|
public static <E> java.util.HashSet<E> newHashSet()
HashSet instance.
Note: if mutability is not required, use ImmutableSet#of()
instead. If E is an Enum type, use EnumSet.noneOf(java.lang.Class<E>)
instead. Otherwise, strongly consider using a LinkedHashSet
instead, at the cost of increased memory footprint, to get
deterministic iteration behavior.
E - Generics Type E.TreeSetpublic static <E extends java.lang.Comparable> java.util.TreeSet<E> newTreeSet()
TreeSet instance sorted by the
natural sort ordering of its elements.
Note: if mutability is not required, use ImmutableSortedSet#of() instead.
E - Generics Type ETreeSet@SafeVarargs public static <E> java.util.HashSet<E> newHashSet(E... elements)
HashSet instance initially containing
the given elements.
Note: if elements are non-null and won't be added or removed
after this point, use ImmutableSet#of() or ImmutableSet#copyOf(Object[])
instead. If E is an Enum type, use
EnumSet.of(Enum, Enum[]) instead. Otherwise, strongly consider
using a LinkedHashSet instead, at the cost of increased memory
footprint, to get deterministic iteration behavior.
This method is just a small convenience, either for
newHashSet(Arrays.asList(T...)(...)), or for creating an
empty set then calling Collections.addAll(java.util.Collection<? super T>, T...).
E - Generics Type E.elements - the elements that the set should contain.Setpublic static <E> java.util.HashSet<E> newHashSet(java.lang.Iterable<? extends E> elements)
HashSet instance containing the given
elements. A very thin convenience for creating an empty set then calling
Collection.addAll(java.util.Collection<? extends E>) or Iterables#addAll.
Note: if mutability is not required and the elements are
non-null, use ImmutableSet#copyOf(Iterable) instead. (Or, change
elements to be a FluentIterable and call elements.toSet().)
Note: if E is an Enum type, use
newEnumSet(Iterable, Class) instead.
E - Generics Type E.elements - the elements that the set should contain.Set.public static <E extends java.lang.Comparable> java.util.TreeSet<E> newTreeSet(java.lang.Iterable<? extends E> elements)
TreeSet instance containing the given
elements sorted by their natural ordering.
Note: if mutability is not required, use ImmutableSortedSet#copyOf(Iterable) instead.
Note: If elements is a SortedSet with an
explicit comparator, this method has different behavior than
TreeSet(SortedSet), which returns a TreeSet
with that comparator.
Note for Java 7 and later: this method is now unnecessary and
should be treated as deprecated. Instead, use the TreeSet
constructor directly, taking advantage of the new
"diamond" syntax.
This method is just a small convenience for creating an empty set and then calling Iterables#addAll. This method is not very useful and will likely be deprecated in the future.
E - Generics Type E.elements - the elements that the set should containTreeSet containing those elements (minus duplicates)public static <E> java.util.HashSet<E> newHashSet(java.util.Iterator<? extends E> elements)
HashSet instance containing the given
elements. A very thin convenience for creating an empty set and then
calling Iterators#addAll.
Note: if mutability is not required and the elements are non-null, use ImmutableSet#copyOf(Iterator) instead.
Note: if E is an Enum type, you should create
an EnumSet instead.
Overall, this method is not very useful and will likely be deprecated in the future.
E - Generics Type E.elements - elements.Set.public static <E> java.util.HashSet<E> newHashSetWithExpectedSize(int expectedSize)
expectedSize elements without resizing. Note that this is not what
HashSet(int) does, but it is what most users want and
expect it to do.
This behavior can't be broadly guaranteed, but has been tested with OpenJDK 1.7 and 1.8.
E - Generics Type E.expectedSize - the number of elements you expect to add to the
returned setexpectedSize elements without resizingjava.lang.IllegalArgumentException - if expectedSize is negativepublic static <E> java.util.Set<E> intersection(java.util.Set<E> set1,
java.util.Set<E> set2)
Results are undefined if set1 and set2 are sets based
on different equivalence relations (as HashSet, TreeSet,
and the keySet of an IdentityHashMap all are).
E - Generics Type E.set1 - set1.set2 - set2.Set.public static <E> java.util.Set<E> union(java.util.Set<E> set1,
java.util.Set<E> set2)
Results are undefined if set1 and set2 are sets
based on different equivalence relations (as HashSet,
TreeSet, and the Map.keySet() of an
IdentityHashMap all are).
E - Generics Type E.set1 - set1.set2 - set2.Set.public static <E> java.util.Set<E> difference(java.util.Set<E> set1,
java.util.Set<E> set2)
set1
and not contained by set2.
Results are undefined if set1 and set2 are sets based
on different equivalence relations (as HashSet, TreeSet,
and the keySet of an IdentityHashMap all are).
This method is used to find difference for HashSets. For TreeSets with
strict order requirement, recommended method is
differenceInTreeSets(Set, Set).
E - Generics Type E.set1 - set1.set2 - set2.Set.public static <E> java.util.Set<E> differenceInTreeSets(java.util.Set<E> set1,
java.util.Set<E> set2)
set1
and not contained by set2.
Results are undefined if set1 and set2 are sets based
on different equivalence relations (as HashSet, TreeSet,
and the keySet of an IdentityHashMap all are).
This method is used to find difference for TreeSets. For HashSets,
recommended method is difference(Set, Set).
E - Generics Type E.set1 - set1.set2 - set2.Set.public static <E> java.util.Set<E> symmetricDifference(java.util.Set<E> set1,
java.util.Set<E> set2)
set1 or set2 but not in both. The iteration order of the
returned set is undefined.
Results are undefined if set1 and set2 are sets based
on different equivalence relations (as HashSet, TreeSet,
and the keySet of an IdentityHashMap all are).
E - Generics Type E.set1 - set1.set2 - set2.Set.public static <E> java.util.Set<E> newConcurrentHashSet()
ConcurrentHashMap instance, and thus carries the same concurrency
guarantees.
Unlike HashSet, this class does NOT allow null to be
used as an element. The set is serializable.
E - Generics Type.SetCopyright © 2008–2025 Apache Software Foundation. All rights reserved.