public class SetUtils extends Object
Modifier and Type | Field and Description |
---|---|
static SortedSet |
EMPTY_SORTED_SET
An empty unmodifiable sorted set.
|
Modifier and Type | Method and Description |
---|---|
static <T> Set<T> |
emptyIfNull(Set<T> set)
Returns an immutable empty set if the argument is
null ,
or the argument itself otherwise. |
static <E> Set<E> |
emptySet()
Get a typed empty unmodifiable Set.
|
static <E> SortedSet<E> |
emptySortedSet()
Get a typed empty unmodifiable sorted set.
|
static <T> int |
hashCodeForSet(Collection<T> set)
Generates a hash code using the algorithm specified in
Set.hashCode() . |
static boolean |
isEqualSet(Collection<?> set1,
Collection<?> set2)
Tests two sets for equality as per the
equals() contract
in Set.equals(java.lang.Object) . |
static <E> Set<E> |
orderedSet(Set<E> set)
Returns a set that maintains the order of elements that are added
backed by the given set.
|
static <E> Set<E> |
predicatedSet(Set<E> set,
Predicate<? super E> predicate)
Returns a predicated (validating) set backed by the given set.
|
static <E> SortedSet<E> |
predicatedSortedSet(SortedSet<E> set,
Predicate<? super E> predicate)
Returns a predicated (validating) sorted set backed by the given sorted set.
|
static <E> Set<E> |
synchronizedSet(Set<E> set)
Returns a synchronized set backed by the given set.
|
static <E> SortedSet<E> |
synchronizedSortedSet(SortedSet<E> set)
Returns a synchronized sorted set backed by the given sorted set.
|
static <E> Set<E> |
transformedSet(Set<E> set,
Transformer<? super E,? extends E> transformer)
Returns a transformed set backed by the given set.
|
static <E> SortedSet<E> |
transformedSortedSet(SortedSet<E> set,
Transformer<? super E,? extends E> transformer)
Returns a transformed sorted set backed by the given set.
|
static <E> Set<E> |
unmodifiableSet(Set<? extends E> set)
Returns an unmodifiable set backed by the given set.
|
static <E> SortedSet<E> |
unmodifiableSortedSet(SortedSet<E> set)
Returns an unmodifiable sorted set backed by the given sorted set.
|
public static final SortedSet EMPTY_SORTED_SET
public static <E> Set<E> emptySet()
E
- the element typepublic static <E> SortedSet<E> emptySortedSet()
E
- the element typepublic static <T> Set<T> emptyIfNull(Set<T> set)
null
,
or the argument itself otherwise.T
- the element typeset
- 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 equals method works properly across different implementations of the Set interface.
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 returns a.containsAll((Collection) b).
set1
- the first set, may be nullset2
- the second set, may be nullSet
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 typeset
- the set to calculate the hash code for, may be nullSet.hashCode()
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 typeset
- the set to synchronize, must not be nullIllegalArgumentException
- if the set is nullpublic static <E> Set<E> unmodifiableSet(Set<? extends E> set)
This method uses the implementation in the decorators subpackage.
E
- the element typeset
- the set to make unmodifiable, must not be nullIllegalArgumentException
- if the set is nullpublic 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 typeset
- the set to predicate, must not be nullpredicate
- the predicate for the set, must not be nullIllegalArgumentException
- if the Set or Predicate is nullpublic 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.transformedSet(java.util.Set<E>, org.apache.commons.collections4.Transformer<? super E, ? extends E>)
.
E
- the element typeset
- the set to transform, must not be nulltransformer
- the transformer for the set, must not be nullIllegalArgumentException
- if the Set or Transformer is nullpublic 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 typeset
- the set to order, must not be nullIllegalArgumentException
- if the Set is nullpublic 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.synchronizedSet(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 typeset
- the sorted set to synchronize, must not be nullIllegalArgumentException
- if the set is nullpublic static <E> SortedSet<E> unmodifiableSortedSet(SortedSet<E> set)
This method uses the implementation in the decorators subpackage.
E
- the element typeset
- the sorted set to make unmodifiable, must not be nullIllegalArgumentException
- if the set is nullpublic 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 typeset
- the sorted set to predicate, must not be nullpredicate
- the predicate for the sorted set, must not be nullIllegalArgumentException
- if the Set or Predicate is nullpublic 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.transformedSortedSet(java.util.SortedSet<E>, org.apache.commons.collections4.Transformer<? super E, ? extends E>)
.
E
- the element typeset
- the set to transform, must not be nulltransformer
- the transformer for the set, must not be nullIllegalArgumentException
- if the Set or Transformer is nullCopyright © 2001–2013 The Apache Software Foundation. All rights reserved.