Class Sets
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.-
Method Summary
Modifier and TypeMethodDescriptionstatic <E> Set<E>difference(Set<E> set1, Set<E> set2) Returns the difference of two sets as an unmodifiable set.static <E> Set<E>differenceInTreeSets(Set<E> set1, Set<E> set2) Returns the difference of two sets as an unmodifiable set.static <E> Set<E>intersection(Set<E> set1, Set<E> set2) Returns the intersection of two sets as an unmodifiable set.static <E> Set<E>Creates a thread-safe set backed by a hash map.static <E> HashSet<E>Creates a mutable, initially emptyHashSetinstance.static <E> HashSet<E>newHashSet(E... elements) Creates a mutableHashSetinstance initially containing the given elements.static <E> HashSet<E>newHashSet(Iterable<? extends E> elements) Creates a mutableHashSetinstance containing the given elements.static <E> HashSet<E>newHashSet(Iterator<? extends E> elements) Creates a mutableHashSetinstance containing the given elements.static <E> HashSet<E>newHashSetWithExpectedSize(int expectedSize) Returns a new hash set using the smallest initial table size that can holdexpectedSizeelements without resizing.static <E extends Comparable>
TreeSet<E>Creates a mutable, emptyTreeSetinstance sorted by the natural sort ordering of its elements.static <E extends Comparable>
TreeSet<E>newTreeSet(Iterable<? extends E> elements) Creates a mutableTreeSetinstance containing the given elements sorted by their natural ordering.static <E> Set<E>symmetricDifference(Set<E> set1, Set<E> set2) Returns the symmetric difference of two sets as an unmodifiable set.static <E> Set<E>Returns the union of two sets as an unmodifiable set.
-
Method Details
-
newHashSet
Creates a mutable, initially emptyHashSetinstance.Note: if mutability is not required, use ImmutableSet#of() instead. If
Eis anEnumtype, useEnumSet.noneOf(java.lang.Class<E>)instead. Otherwise, strongly consider using aLinkedHashSetinstead, at the cost of increased memory footprint, to get deterministic iteration behavior.- Type Parameters:
E- Generics Type E.- Returns:
- a new, empty
TreeSet
-
newTreeSet
Creates a mutable, emptyTreeSetinstance sorted by the natural sort ordering of its elements.Note: if mutability is not required, use ImmutableSortedSet#of() instead.
- Type Parameters:
E- Generics Type E- Returns:
- a new, empty
TreeSet
-
newHashSet
Creates a mutableHashSetinstance 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
Eis anEnumtype, useEnumSet.of(Enum, Enum[])instead. Otherwise, strongly consider using aLinkedHashSetinstead, 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 callingCollections.addAll(java.util.Collection<? super T>, T...).- Type Parameters:
E- Generics Type E.- Parameters:
elements- the elements that the set should contain.- Returns:
- a new, empty thread-safe
Set
-
newHashSet
Creates a mutableHashSetinstance containing the given elements. A very thin convenience for creating an empty set then callingCollection.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
elementsto be a FluentIterable and callelements.toSet().)Note: if
Eis anEnumtype, use newEnumSet(Iterable, Class) instead.- Type Parameters:
E- Generics Type E.- Parameters:
elements- the elements that the set should contain.- Returns:
- a new, empty thread-safe
Set.
-
newTreeSet
Creates a mutableTreeSetinstance containing the given elements sorted by their natural ordering.Note: if mutability is not required, use ImmutableSortedSet#copyOf(Iterable) instead.
Note: If
elementsis aSortedSetwith an explicit comparator, this method has different behavior thanTreeSet(SortedSet), which returns aTreeSetwith that comparator.Note for Java 7 and later: this method is now unnecessary and should be treated as deprecated. Instead, use the
TreeSetconstructor 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.
- Type Parameters:
E- Generics Type E.- Parameters:
elements- the elements that the set should contain- Returns:
- a new
TreeSetcontaining those elements (minus duplicates)
-
newHashSet
Creates a mutableHashSetinstance 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
Eis anEnumtype, you should create anEnumSetinstead.Overall, this method is not very useful and will likely be deprecated in the future.
- Type Parameters:
E- Generics Type E.- Parameters:
elements- elements.- Returns:
- a new, empty thread-safe
Set.
-
newHashSetWithExpectedSize
Returns a new hash set using the smallest initial table size that can holdexpectedSizeelements without resizing. Note that this is not whatHashSet(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.
- Type Parameters:
E- Generics Type E.- Parameters:
expectedSize- the number of elements you expect to add to the returned set- Returns:
- a new, empty hash set with enough capacity to hold
expectedSizeelements without resizing - Throws:
IllegalArgumentException- ifexpectedSizeis negative
-
intersection
Returns the intersection of two sets as an unmodifiable set. The returned set contains all elements that are contained by both backing sets.Results are undefined if
set1andset2are sets based on different equivalence relations (asHashSet,TreeSet, and the keySet of anIdentityHashMapall are).- Type Parameters:
E- Generics Type E.- Parameters:
set1- set1.set2- set2.- Returns:
- a new, empty thread-safe
Set.
-
union
Returns the union of two sets as an unmodifiable set. The returned set contains all elements that are contained in either backing set.Results are undefined if
set1andset2are sets based on different equivalence relations (asHashSet,TreeSet, and theMap.keySet()of anIdentityHashMapall are).- Type Parameters:
E- Generics Type E.- Parameters:
set1- set1.set2- set2.- Returns:
- a new, empty thread-safe
Set.
-
difference
Returns the difference of two sets as an unmodifiable set. The returned set contains all elements that are contained byset1and not contained byset2.Results are undefined if
set1andset2are sets based on different equivalence relations (asHashSet,TreeSet, and the keySet of anIdentityHashMapall are). This method is used to find difference for HashSets. For TreeSets with strict order requirement, recommended method isdifferenceInTreeSets(Set, Set).- Type Parameters:
E- Generics Type E.- Parameters:
set1- set1.set2- set2.- Returns:
- a new, empty thread-safe
Set.
-
differenceInTreeSets
Returns the difference of two sets as an unmodifiable set. The returned set contains all elements that are contained byset1and not contained byset2.Results are undefined if
set1andset2are sets based on different equivalence relations (asHashSet,TreeSet, and the keySet of anIdentityHashMapall are). This method is used to find difference for TreeSets. For HashSets, recommended method isdifference(Set, Set).- Type Parameters:
E- Generics Type E.- Parameters:
set1- set1.set2- set2.- Returns:
- a new, empty thread-safe
Set.
-
symmetricDifference
Returns the symmetric difference of two sets as an unmodifiable set. The returned set contains all elements that are contained in eitherset1orset2but not in both. The iteration order of the returned set is undefined.Results are undefined if
set1andset2are sets based on different equivalence relations (asHashSet,TreeSet, and the keySet of anIdentityHashMapall are).- Type Parameters:
E- Generics Type E.- Parameters:
set1- set1.set2- set2.- Returns:
- a new, empty thread-safe
Set.
-
newConcurrentHashSet
Creates a thread-safe set backed by a hash map. The set is backed by aConcurrentHashMapinstance, and thus carries the same concurrency guarantees.Unlike
HashSet, this class does NOT allownullto be used as an element. The set is serializable.- Type Parameters:
E- Generics Type.- Returns:
- a new, empty thread-safe
Set
-