public class SetUtils extends Object
| Modifier and Type | Class and Description |
|---|---|
static class |
SetUtils
An unmodifiable
view of a set that may be backed by other sets.
|
| Modifier and Type | Field and Description |
|---|---|
static SortedSet |
EMPTY_SORTED_SET
An empty unmodifiable sorted set.
|
| Modifier and Type | Method and Description |
|---|---|
static <E> SetUtils |
difference(Set
Returns a unmodifiable
view containing the difference of the given
Sets, denoted by
a \ b (or
a - b).
|
static <E> SetUtils |
disjunction(Set
Returns a unmodifiable
view of the symmetric difference of the given
Sets.
|
static <T> Set |
emptyIfNull(Set
Returns an immutable empty set if the argument is
null, or the argument itself otherwise.
|
static <E> Set |
emptySet()
Get a typed empty unmodifiable Set.
|
static <E> SortedSet |
emptySortedSet()
Get a typed empty unmodifiable sorted set.
|
static <T> int |
hashCodeForSet(Collection
Generates a hash code using the algorithm specified in
Set.
|
static <E> SetUtils |
intersection(Set
Returns a unmodifiable
view of the intersection of the given
Sets.
|
static boolean |
isEqualSet(Collection
Tests two sets for equality as per the
equals() contract in
Set.
|
static <E> Set |
newIdentityHashSet()
Returns a new hash set that matches elements based on
== not
equals().
|
static <E> Set |
orderedSet(Set
Returns a set that maintains the order of elements that are added backed by the given set.
|
static <E> SortedSet |
predicatedNavigableSet(NavigableSet
Returns a predicated (validating) navigable set backed by the given navigable set.
|
static <E> Set |
predicatedSet(Set
Returns a predicated (validating) set backed by the given set.
|
static <E> SortedSet |
predicatedSortedSet(SortedSet
Returns a predicated (validating) sorted set backed by the given sorted set.
|
static <E> Set |
synchronizedSet(Set
Returns a synchronized set backed by the given set.
|
static <E> SortedSet |
synchronizedSortedSet(SortedSet
Returns a synchronized sorted set backed by the given sorted set.
|
static <E> SortedSet |
transformedNavigableSet(NavigableSet
Returns a transformed navigable set backed by the given navigable set.
|
static <E> Set |
transformedSet(Set
Returns a transformed set backed by the given set.
|
static <E> SortedSet |
transformedSortedSet(SortedSet
Returns a transformed sorted set backed by the given set.
|
static <E> SetUtils |
union(Set
Returns a unmodifiable
view of the union of the given
Sets.
|
static <E> SortedSet |
unmodifiableNavigableSet(NavigableSet
Returns an unmodifiable navigable set backed by the given navigable set.
|
static <E> Set |
unmodifiableSet(Set
Returns an unmodifiable set backed by the given set.
|
static <E> SortedSet |
unmodifiableSortedSet(SortedSet
Returns an unmodifiable sorted set backed by the given sorted set.
|
public static final SortedSetEMPTY_SORTED_SET
public static <E> Set<E> emptySet()
E - the element type
public static <E> SortedSet<E> emptySortedSet()
E - the element type
public static <T> Set<T> emptyIfNull(Set <T> set)
null, or the argument itself otherwise.
T - the element type
set - the set, possibly
null
null
public static boolean isEqualSet(Collection<?> set1, Collection <?> set2)
equals() contract in
Set.equals(java.lang.Object) .
This method is useful for implementing Set when you cannot extend AbstractSet. The method takes Collection instances to enable other collection types to use the Set implementation algorithm.
The relevant text (slightly paraphrased as this is a static method) is:
Two sets are considered equal if they have the same size, and every member of the first set is contained in the second. This ensures that the
equalsmethod works properly across different implementations of theSetinterface.This implementation first checks if the two sets are the same object: if so it returns
true. Then, it checks if the two sets are identical in size; if not, it returns false. If so, it returnsa.containsAll((Collection) b).
set1 - the first set, may be null
set2 - the second set, may be null
Set
public static <T> int hashCodeForSet(Collection<T> set)
Set.hashCode() .
This method is useful for implementing Set when you cannot extend AbstractSet. The method takes Collection instances to enable other collection types to use the Set implementation algorithm.
T - the element type
set - the set to calculate the hash code for, may be null
Set.hashCode()
public static <E> Set<E> newIdentityHashSet()
== not
equals().
This set will violate the detail of various Set contracts. As a general rule, don't compare this set to other sets. In particular, you can't use decorators like ListOrderedSet on it, which silently assume that these contracts are fulfilled.
Note that the returned set is not synchronized and is not thread-safe. If you wish to use this set from multiple threads concurrently, you must use appropriate synchronization. The simplest approach is to wrap this map using Collections. This class may throw exceptions when accessed by concurrent threads without synchronization.
E - the element type
public static <E> Set<E> synchronizedSet(Set <E> set)
You must manually synchronize on the returned set's iterator to avoid non-deterministic behavior:
Set s = SetUtils.synchronizedSet(mySet);
synchronized (s) {
Iterator i = s.iterator();
while (i.hasNext()) {
process (i.next());
}
}
This method is just a wrapper for
Collections.synchronizedSet(Set) .
E - the element type
set - the set to synchronize, must not be null
NullPointerException - if the set is null
public static <E> Set<E> unmodifiableSet(Set <? extends E> set)
This method uses the implementation in the decorators subpackage.
E - the element type
set - the set to make unmodifiable, must not be null
NullPointerException - if the set is null
public static <E> Set<E> predicatedSet(Set <E> set, Predicate <? super E> predicate)
Only objects that pass the test in the given predicate can be added to the set. Trying to add an invalid object results in an IllegalArgumentException. It is important not to use the original set after invoking this method, as it is a backdoor for adding invalid objects.
E - the element type
set - the set to predicate, must not be null
predicate - the predicate for the set, must not be null
NullPointerException - if the set or predicate is null
public static <E> Set<E> transformedSet(Set <E> set, Transformer <? super E ,? extends E> transformer)
Each object is passed through the transformer as it is added to the Set. It is important not to use the original set after invoking this method, as it is a backdoor for adding untransformed objects.
Existing entries in the specified set will not be transformed. If you want that behaviour, see TransformedSet.
E - the element type
set - the set to transform, must not be null
transformer - the transformer for the set, must not be null
NullPointerException - if the set or transformer is null
public static <E> Set<E> orderedSet(Set <E> set)
If an element is added twice, the order is determined by the first add. The order is observed through the iterator or toArray.
E - the element type
set - the set to order, must not be null
NullPointerException - if the set is null
public static <E> SortedSet<E> synchronizedSortedSet(SortedSet <E> set)
You must manually synchronize on the returned set's iterator to avoid non-deterministic behavior:
Set s = SetUtils.synchronizedSortedSet(mySet);
synchronized (s) {
Iterator i = s.iterator();
while (i.hasNext()) {
process (i.next());
}
}
This method is just a wrapper for
Collections.synchronizedSortedSet(SortedSet) .
E - the element type
set - the sorted set to synchronize, must not be null
NullPointerException - if the set is null
public static <E> SortedSet<E> unmodifiableSortedSet(SortedSet <E> set)
This method uses the implementation in the decorators subpackage.
E - the element type
set - the sorted set to make unmodifiable, must not be null
NullPointerException - if the set is null
public static <E> SortedSet<E> predicatedSortedSet(SortedSet <E> set, Predicate <? super E> predicate)
Only objects that pass the test in the given predicate can be added to the set. Trying to add an invalid object results in an IllegalArgumentException. It is important not to use the original set after invoking this method, as it is a backdoor for adding invalid objects.
E - the element type
set - the sorted set to predicate, must not be null
predicate - the predicate for the sorted set, must not be null
NullPointerException - if the set or predicate is null
public static <E> SortedSet<E> transformedSortedSet(SortedSet <E> set, Transformer <? super E ,? extends E> transformer)
Each object is passed through the transformer as it is added to the Set. It is important not to use the original set after invoking this method, as it is a backdoor for adding untransformed objects.
Existing entries in the specified set will not be transformed. If you want that behaviour, see TransformedSortedSet.
E - the element type
set - the set to transform, must not be null
transformer - the transformer for the set, must not be null
NullPointerException - if the set or transformer is null
public static <E> SortedSet<E> unmodifiableNavigableSet(NavigableSet <E> set)
This method uses the implementation in the decorators subpackage.
E - the element type
set - the navigable set to make unmodifiable, must not be null
NullPointerException - if the set is null
public static <E> SortedSet<E> predicatedNavigableSet(NavigableSet <E> set, Predicate <? super E> predicate)
Only objects that pass the test in the given predicate can be added to the set. Trying to add an invalid object results in an IllegalArgumentException. It is important not to use the original set after invoking this method, as it is a backdoor for adding invalid objects.
E - the element type
set - the navigable set to predicate, must not be null
predicate - the predicate for the navigable set, must not be null
NullPointerException - if the set or predicate is null
public static <E> SortedSet<E> transformedNavigableSet(NavigableSet <E> set, Transformer <? super E ,? extends E> transformer)
Each object is passed through the transformer as it is added to the Set. It is important not to use the original set after invoking this method, as it is a backdoor for adding untransformed objects.
Existing entries in the specified set will not be transformed. If you want that behaviour, see TransformedNavigableSet.
E - the element type
set - the navigable set to transform, must not be null
transformer - the transformer for the set, must not be null
NullPointerException - if the set or transformer is null
public static <E> SetUtils.SetView <E> union(Set <? extends E> a, Set <? extends E> b)
Sets.
The returned view contains all elements of a and b.
E - the generic type that is able to represent the types contained in both input sets.
a - the first set, must not be null
b - the second set, must not be null
NullPointerException - if either input set is null
public static <E> SetUtils.SetView <E> difference(Set <? extends E> a, Set <? extends E> b)
Sets, denoted by
a \ b (or
a - b).
The returned view contains all elements of a that are not a member of b.
E - the generic type that is able to represent the types contained in both input sets.
a - the set to subtract from, must not be null
b - the set to subtract, must not be null
public static <E> SetUtils.SetView <E> intersection(Set <? extends E> a, Set <? extends E> b)
Sets.
The returned view contains all elements that are members of both input sets (a and b).
E - the generic type that is able to represent the types contained in both input sets.
a - the first set, must not be null
b - the second set, must not be null
public static <E> SetUtils.SetView <E> disjunction(Set <? extends E> a, Set <? extends E> b)
Sets.
The returned view contains all elements of a and b that are not a member of the other set.
This is equivalent to union(difference(a, b), difference(b, a)).
E - the generic type that is able to represent the types contained in both input sets.
a - the first set, must not be null
b - the second set, must not be null