public final class

Sets

extends Object
java.lang.Object
   ↳ com.facebook.common.internal.Sets

Class Overview

Static utility methods pertaining to Set instances.

Summary

Public Methods
static <E> CopyOnWriteArraySet<E> newCopyOnWriteArraySet()
Creates an empty CopyOnWriteArraySet instance.
static <E> HashSet<E> newHashSet(Iterator<? extends E> elements)
Creates a mutable HashSet instance containing the given elements in unspecified order.
static <E> HashSet<E> newHashSet(E... elements)
Creates a mutable HashSet instance containing the given elements in unspecified order.
static <E> HashSet<E> newHashSet()
Creates a mutable, empty HashSet instance.
static <E> HashSet<E> newHashSet(Iterable<? extends E> elements)
Creates a mutable HashSet instance containing the given elements in unspecified order.
static <E> HashSet<E> newHashSetWithCapacity(int capacity)
Creates a HashSet instance, with a high enough "initial capacity" that it should hold expectedSize elements without growth.
static <E> Set<E> newIdentityHashSet()
Creates an empty Set that uses identity to determine equality.
static <E> LinkedHashSet<E> newLinkedHashSet()
Creates a mutable, empty LinkedHashSet instance.
static <E> Set<E> newSetFromMap(Map<E, Boolean> map)
Returns a set backed by the specified map.
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

public static CopyOnWriteArraySet<E> newCopyOnWriteArraySet ()

Creates an empty CopyOnWriteArraySet instance.

Note: if you need an immutable empty Set, use emptySet() instead.

Returns
  • a new, empty CopyOnWriteArraySet

public static HashSet<E> newHashSet (Iterator<? extends E> elements)

Creates a mutable HashSet instance containing the given elements in unspecified order.

Parameters
elements the elements that the set should contain
Returns
  • a new HashSet containing those elements (minus duplicates)

public static HashSet<E> newHashSet (E... elements)

Creates a mutable HashSet instance containing the given elements in unspecified order.

Parameters
elements the elements that the set should contain
Returns
  • a new HashSet containing those elements (minus duplicates)

public static HashSet<E> newHashSet ()

Creates a mutable, empty HashSet instance.

Returns
  • a new, empty HashSet

public static HashSet<E> newHashSet (Iterable<? extends E> elements)

Creates a mutable HashSet instance containing the given elements in unspecified order.

Parameters
elements the elements that the set should contain
Returns
  • a new HashSet containing those elements (minus duplicates)

public static HashSet<E> newHashSetWithCapacity (int capacity)

Creates a HashSet instance, with a high enough "initial capacity" that it should hold expectedSize elements without growth. This behavior cannot be broadly guaranteed, but it is observed to be true for OpenJDK 1.6. It also can't be guaranteed that the method isn't inadvertently oversizing the returned set.

Parameters
capacity the number of elements you expect to add to the returned set
Returns
  • a new, empty HashSet with enough capacity to hold expectedSize elements without resizing
Throws
IllegalArgumentException if expectedSize is negative

public static Set<E> newIdentityHashSet ()

Creates an empty Set that uses identity to determine equality. It compares object references, instead of calling equals, to determine whether a provided object matches an element in the set. For example, contains returns false when passed an object that equals a set member, but isn't the same instance. This behavior is similar to the way IdentityHashMap handles key lookups.

public static LinkedHashSet<E> newLinkedHashSet ()

Creates a mutable, empty LinkedHashSet instance.

Returns
  • a new, empty LinkedHashSet

public static Set<E> newSetFromMap (Map<E, Boolean> map)

Returns a set backed by the specified map. The resulting set displays the same ordering, concurrency, and performance characteristics as the backing map. In essence, this factory method provides a Set implementation corresponding to any Map implementation. There is no need to use this method on a Map implementation that already has a corresponding Set implementation (such as java.util.HashMap or java.util.TreeMap).

Each method invocation on the set returned by this method results in exactly one method invocation on the backing map or its keySet view, with one exception. The addAll method is implemented as a sequence of put invocations on the backing map.

The specified map must be empty at the time this method is invoked, and should not be accessed directly after this method returns. These conditions are ensured if the map is created empty, passed directly to this method, and no reference to the map is retained, as illustrated in the following code fragment:

Set<Object> identityHashSet = Sets.newSetFromMap(
     new IdentityHashMap<Object, Boolean>());
 

This method has the same behavior as the JDK 6 method Collections.newSetFromMap(). The returned set is serializable if the backing map is.

Parameters
map the backing map
Returns
  • the set backed by the map
Throws
IllegalArgumentException if map is not empty