Top Description Inners Fields Constructors Methods
org.apache.commons.collections4

public Class SetUtils

extends Object
Class Inheritance
Imports
java.util.AbstractSet, .Arrays, .Collection, .Collections, .HashSet, .IdentityHashMap, .Iterator, .NavigableSet, .Set, .SortedSet, .TreeSet, org.apache.commons.collections4.set.ListOrderedSet, .PredicatedNavigableSet, .PredicatedSet, .PredicatedSortedSet, .TransformedNavigableSet, .TransformedSet, .TransformedSortedSet, .UnmodifiableNavigableSet, .UnmodifiableSet, .UnmodifiableSortedSet

Provides utility methods and decorators for Set and SortedSet instances.
Since
2.1

Nested and Inner Type Summary

Modifier and TypeClass and Description
public abstract static class
SetUtils.SetView<
the element type
E
>

An unmodifiable view of a set that may be backed by other sets.

Field Summary

Modifier and TypeField and Description
public static final SortedSet<E>
EMPTY_SORTED_SET

An empty unmodifiable sorted set.

Constructor Summary

AccessConstructor and Description
private
SetUtils()

SetUtils should not normally be instantiated.

Method Summary

Modifier and TypeMethod and Description
public static <
the generic type that is able to represent the types contained in both input sets.
E
>
SetUtils.SetView<E>

Returns:

a view of the relative complement of of the two sets
difference
(final Set<? extends E>
the set to subtract from, must not be null
a
,
final Set<? extends E>
the set to subtract, must not be null
b
)

Returns a unmodifiable view containing the difference of the given Sets, denoted by a \ b (or a - b).

public static <
the generic type that is able to represent the types contained in both input sets.
E
>
SetUtils.SetView<E>

Returns:

a view of the symmetric difference of the two sets
disjunction
(final Set<? extends E>
the first set, must not be null
a
,
final Set<? extends E>
the second set, must not be null
b
)

Returns a unmodifiable view of the symmetric difference of the given Sets.

public static <
the element type
T
>
Set<T>

Returns:

an empty set if the argument is null
emptyIfNull
(final Set<T>
the set, possibly null
set
)

Returns an immutable empty set if the argument is null, or the argument itself otherwise.

public static <
the element type
E
>
Set<E>

Returns:

an empty Set
emptySet
()

Get a typed empty unmodifiable Set.

public static <
the element type
E
>
SortedSet<E>

Returns:

an empty sorted Set
emptySortedSet
()

Get a typed empty unmodifiable sorted set.

public static <
the element type
T
>
int

Returns:

the hash code
hashCodeForSet
(final Collection<T>
the set to calculate the hash code for, may be null
set
)

Generates a hash code using the algorithm specified in java.util.Set#hashCode().

public static <
the element type
E
>
HashSet<E>

Returns:

a set
hashSet
(final E...
the elements that make up the new set
items
)

Creates a set from the given items.

public static <
the generic type that is able to represent the types contained in both input sets.
E
>
SetUtils.SetView<E>

Returns:

a view of the intersection of the two sets
intersection
(final Set<? extends E>
the first set, must not be null
a
,
final Set<? extends E>
the second set, must not be null
b
)

Returns a unmodifiable view of the intersection of the given Sets.

public static boolean

Returns:

whether the sets are equal by value comparison
isEqualSet
(final Collection<?>
the first set, may be null
set1
,
final Collection<?>
the second set, may be null
set2
)

Tests two sets for equality as per the equals() contract in java.util.Set#equals(java.lang.Object).

public static <
the element type
E
>
Set<E>

Returns:

a new identity hash set
newIdentityHashSet
()

Returns a new hash set that matches elements based on == not equals().

public static <
the element type
E
>
Set<E>

Returns:

an ordered set backed by the given set
orderedSet
(final Set<E>
the set to order, must not be null
set
)

Returns a set that maintains the order of elements that are added backed by the given set.

public static <
the element type
E
>
SortedSet<E>

Returns:

a predicated navigable set backed by the given navigable set
predicatedNavigableSet
(final NavigableSet<E>
the navigable set to predicate, must not be null
set
,
final Predicate<? super E>
the predicate for the navigable set, must not be null
predicate
)

Returns a predicated (validating) navigable set backed by the given navigable set.

public static <
the element type
E
>
Set<E>

Returns:

a predicated set backed by the given set
predicatedSet
(final Set<E>
the set to predicate, must not be null
set
,
final Predicate<? super E>
the predicate for the set, must not be null
predicate
)

Returns a predicated (validating) set backed by the given set.

public static <
the element type
E
>
SortedSet<E>

Returns:

a predicated sorted set backed by the given sorted set
predicatedSortedSet
(final SortedSet<E>
the sorted set to predicate, must not be null
set
,
final Predicate<? super E>
the predicate for the sorted set, must not be null
predicate
)

Returns a predicated (validating) sorted set backed by the given sorted set.

public static <
the element type
E
>
Set<E>

Returns:

a synchronized set backed by the given set
synchronizedSet
(final Set<E>
the set to synchronize, must not be null
set
)

Returns a synchronized set backed by the given set.

public static <
the element type
E
>
SortedSet<E>

Returns:

a synchronized set backed by the given set
synchronizedSortedSet
(final SortedSet<E>
the sorted set to synchronize, must not be null
set
)

Returns a synchronized sorted set backed by the given sorted set.

public static <
the element type
E
>
SortedSet<E>

Returns:

a transformed set backed by the given set
transformedNavigableSet
(final NavigableSet<E>
the navigable set to transform, must not be null
set
,
final Transformer<? super E, ? extends E>
the transformer for the set, must not be null
transformer
)

Returns a transformed navigable set backed by the given navigable set.

public static <
the element type
E
>
Set<E>

Returns:

a transformed set backed by the given set
transformedSet
(final Set<E>
the set to transform, must not be null
set
,
final Transformer<? super E, ? extends E>
the transformer for the set, must not be null
transformer
)

Returns a transformed set backed by the given set.

public static <
the element type
E
>
SortedSet<E>

Returns:

a transformed set backed by the given set
transformedSortedSet
(final SortedSet<E>
the set to transform, must not be null
set
,
final Transformer<? super E, ? extends E>
the transformer for the set, must not be null
transformer
)

Returns a transformed sorted set backed by the given set.

public static <
the generic type that is able to represent the types contained in both input sets.
E
>
SetUtils.SetView<E>

Returns:

a view of the union of the two set
union
(final Set<? extends E>
the first set, must not be null
a
,
final Set<? extends E>
the second set, must not be null
b
)

Returns a unmodifiable view of the union of the given Sets.

public static <
the element type
E
>
SortedSet<E>

Returns:

an unmodifiable set backed by the given set
unmodifiableNavigableSet
(final NavigableSet<E>
the navigable set to make unmodifiable, must not be null
set
)

Returns an unmodifiable navigable set backed by the given navigable set.

public static <
the element type
E
>
Set<E>

Returns:

a set
unmodifiableSet
(final E...
the elements that make up the new set
items
)

Creates an unmodifiable set from the given items.

public static <
the element type
E
>
Set<E>

Returns:

an unmodifiable set backed by the given set
unmodifiableSet
(final Set<? extends E>
the set to make unmodifiable, must not be null
set
)

Returns an unmodifiable set backed by the given set.

public static <
the element type
E
>
SortedSet<E>

Returns:

an unmodifiable set backed by the given set
unmodifiableSortedSet
(final SortedSet<E>
the sorted set to make unmodifiable, must not be null
set
)

Returns an unmodifiable sorted set backed by the given sorted set.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

EMPTY_SORTED_SETback to summary
public static final SortedSet<E> EMPTY_SORTED_SET

An empty unmodifiable sorted set. This is not provided in the JDK.

Annotations
@SuppressWarnings:rawtypes

Constructor Detail

SetUtilsback to summary
private SetUtils()

SetUtils should not normally be instantiated.

Method Detail

differenceback to summary
public static <E> SetUtils.SetView<E> difference(final Set<? extends E> a, final Set<? extends E> b)

Returns a unmodifiable view containing the difference of the given Sets, denoted by a \ b (or a - b).

The returned view contains all elements of a that are not a member of b.

Parameters
<E>
the generic type that is able to represent the types contained in both input sets.
a:Set<? extends E>

the set to subtract from, must not be null

b:Set<? extends E>

the set to subtract, must not be null

Returns:SetUtils.SetView<E>

a view of the relative complement of of the two sets

Since
4.1
disjunctionback to summary
public static <E> SetUtils.SetView<E> disjunction(final Set<? extends E> a, final Set<? extends E> b)

Returns a unmodifiable view of the symmetric difference of the given 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)).

Parameters
<E>
the generic type that is able to represent the types contained in both input sets.
a:Set<? extends E>

the first set, must not be null

b:Set<? extends E>

the second set, must not be null

Returns:SetUtils.SetView<E>

a view of the symmetric difference of the two sets

Since
4.1
emptyIfNullback to summary
public static <T> Set<T> emptyIfNull(final Set<T> set)

Returns an immutable empty set if the argument is null, or the argument itself otherwise.

Parameters
<T>
the element type
set:Set<T>

the set, possibly null

Returns:Set<T>

an empty set if the argument is null

emptySetback to summary
public static <E> Set<E> emptySet()

Get a typed empty unmodifiable Set.

Parameters
<E>
the element type
Returns:Set<E>

an empty Set

emptySortedSetback to summary
public static <E> SortedSet<E> emptySortedSet()

Get a typed empty unmodifiable sorted set.

Parameters
<E>
the element type
Returns:SortedSet<E>

an empty sorted Set

Annotations
@SuppressWarnings:unchecked
hashCodeForSetback to summary
public static <T> int hashCodeForSet(final Collection<T> set)

Generates a hash code using the algorithm specified in java.util.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.

Parameters
<T>
the element type
set:Collection<T>

the set to calculate the hash code for, may be null

Returns:int

the hash code

See Also
java.util.Set#hashCode()
hashSetback to summary
public static <E> HashSet<E> hashSet(final E... items)

Creates a set from the given items. If the passed var-args argument is null, then the method returns null.

Parameters
<E>
the element type
items:E[]

the elements that make up the new set

Returns:HashSet<E>

a set

Since
4.3
intersectionback to summary
public static <E> SetUtils.SetView<E> intersection(final Set<? extends E> a, final Set<? extends E> b)

Returns a unmodifiable view of the intersection of the given Sets.

The returned view contains all elements that are members of both input sets (a and b).

Parameters
<E>
the generic type that is able to represent the types contained in both input sets.
a:Set<? extends E>

the first set, must not be null

b:Set<? extends E>

the second set, must not be null

Returns:SetUtils.SetView<E>

a view of the intersection of the two sets

Since
4.1
isEqualSetback to summary
public static boolean isEqualSet(final Collection<?> set1, final Collection<?> set2)

Tests two sets for equality as per the equals() contract in java.util.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).

Parameters
set1:Collection<?>

the first set, may be null

set2:Collection<?>

the second set, may be null

Returns:boolean

whether the sets are equal by value comparison

See Also
java.util.Set
newIdentityHashSetback to summary
public static <E> Set<E> newIdentityHashSet()

Returns a new hash set that matches elements based on == 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 java.util.Collections#synchronizedSet(Set). This class may throw exceptions when accessed by concurrent threads without synchronization.

Parameters
<E>
the element type
Returns:Set<E>

a new identity hash set

Since
4.1
orderedSetback to summary
public static <E> Set<E> orderedSet(final Set<E> set)

Returns a set that maintains the order of elements that are added backed by the given set.

If an element is added twice, the order is determined by the first add. The order is observed through the iterator or toArray.

Parameters
<E>
the element type
set:Set<E>

the set to order, must not be null

Returns:Set<E>

an ordered set backed by the given set

Exceptions
NullPointerException:
if the set is null
predicatedNavigableSetback to summary
public static <E> SortedSet<E> predicatedNavigableSet(final NavigableSet<E> set, final Predicate<? super E> predicate)

Returns a predicated (validating) navigable set backed by the given navigable set.

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.

Parameters
<E>
the element type
set:NavigableSet<E>

the navigable set to predicate, must not be null

predicate:Predicate<? super E>

the predicate for the navigable set, must not be null

Returns:SortedSet<E>

a predicated navigable set backed by the given navigable set

Exceptions
NullPointerException:
if the set or predicate is null
Since
4.1
predicatedSetback to summary
public static <E> Set<E> predicatedSet(final Set<E> set, final Predicate<? super E> predicate)

Returns a predicated (validating) set backed by the given set.

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.

Parameters
<E>
the element type
set:Set<E>

the set to predicate, must not be null

predicate:Predicate<? super E>

the predicate for the set, must not be null

Returns:Set<E>

a predicated set backed by the given set

Exceptions
NullPointerException:
if the set or predicate is null
predicatedSortedSetback to summary
public static <E> SortedSet<E> predicatedSortedSet(final SortedSet<E> set, final Predicate<? super E> predicate)

Returns a predicated (validating) sorted set backed by the given sorted set.

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.

Parameters
<E>
the element type
set:SortedSet<E>

the sorted set to predicate, must not be null

predicate:Predicate<? super E>

the predicate for the sorted set, must not be null

Returns:SortedSet<E>

a predicated sorted set backed by the given sorted set

Exceptions
NullPointerException:
if the set or predicate is null
synchronizedSetback to summary
public static <E> Set<E> synchronizedSet(final Set<E> set)

Returns a synchronized set backed by the given 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).
Parameters
<E>
the element type
set:Set<E>

the set to synchronize, must not be null

Returns:Set<E>

a synchronized set backed by the given set

Exceptions
NullPointerException:
if the set is null
synchronizedSortedSetback to summary
public static <E> SortedSet<E> synchronizedSortedSet(final SortedSet<E> set)

Returns a synchronized sorted set backed by the given sorted 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).
Parameters
<E>
the element type
set:SortedSet<E>

the sorted set to synchronize, must not be null

Returns:SortedSet<E>

a synchronized set backed by the given set

Exceptions
NullPointerException:
if the set is null
transformedNavigableSetback to summary
public static <E> SortedSet<E> transformedNavigableSet(final NavigableSet<E> set, final Transformer<? super E, ? extends E> transformer)

Returns a transformed navigable set backed by the given navigable set.

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#transformedNavigableSet.

Parameters
<E>
the element type
set:NavigableSet<E>

the navigable set to transform, must not be null

transformer:Transformer<? super E, ? extends E>

the transformer for the set, must not be null

Returns:SortedSet<E>

a transformed set backed by the given set

Exceptions
NullPointerException:
if the set or transformer is null
Since
4.1
transformedSetback to summary
public static <E> Set<E> transformedSet(final Set<E> set, final Transformer<? super E, ? extends E> transformer)

Returns a transformed set backed by the given set.

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.

Parameters
<E>
the element type
set:Set<E>

the set to transform, must not be null

transformer:Transformer<? super E, ? extends E>

the transformer for the set, must not be null

Returns:Set<E>

a transformed set backed by the given set

Exceptions
NullPointerException:
if the set or transformer is null
transformedSortedSetback to summary
public static <E> SortedSet<E> transformedSortedSet(final SortedSet<E> set, final Transformer<? super E, ? extends E> transformer)

Returns a transformed sorted set backed by the given set.

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.

Parameters
<E>
the element type
set:SortedSet<E>

the set to transform, must not be null

transformer:Transformer<? super E, ? extends E>

the transformer for the set, must not be null

Returns:SortedSet<E>

a transformed set backed by the given set

Exceptions
NullPointerException:
if the set or transformer is null
unionback to summary
public static <E> SetUtils.SetView<E> union(final Set<? extends E> a, final Set<? extends E> b)

Returns a unmodifiable view of the union of the given Sets.

The returned view contains all elements of a and b.

Parameters
<E>
the generic type that is able to represent the types contained in both input sets.
a:Set<? extends E>

the first set, must not be null

b:Set<? extends E>

the second set, must not be null

Returns:SetUtils.SetView<E>

a view of the union of the two set

Exceptions
NullPointerException:
if either input set is null
Since
4.1
unmodifiableNavigableSetback to summary
public static <E> SortedSet<E> unmodifiableNavigableSet(final NavigableSet<E> set)

Returns an unmodifiable navigable set backed by the given navigable set.

This method uses the implementation in the decorators subpackage.

Parameters
<E>
the element type
set:NavigableSet<E>

the navigable set to make unmodifiable, must not be null

Returns:SortedSet<E>

an unmodifiable set backed by the given set

Exceptions
NullPointerException:
if the set is null
Since
4.1
unmodifiableSetback to summary
public static <E> Set<E> unmodifiableSet(final E... items)

Creates an unmodifiable set from the given items. If the passed var-args argument is null, then the method returns null.

Parameters
<E>
the element type
items:E[]

the elements that make up the new set

Returns:Set<E>

a set

Since
4.3
unmodifiableSetback to summary
public static <E> Set<E> unmodifiableSet(final Set<? extends E> set)

Returns an unmodifiable set backed by the given set.

This method uses the implementation in the decorators subpackage.

Parameters
<E>
the element type
set:Set<? extends E>

the set to make unmodifiable, must not be null

Returns:Set<E>

an unmodifiable set backed by the given set

Exceptions
NullPointerException:
if the set is null
unmodifiableSortedSetback to summary
public static <E> SortedSet<E> unmodifiableSortedSet(final SortedSet<E> set)

Returns an unmodifiable sorted set backed by the given sorted set.

This method uses the implementation in the decorators subpackage.

Parameters
<E>
the element type
set:SortedSet<E>

the sorted set to make unmodifiable, must not be null

Returns:SortedSet<E>

an unmodifiable set backed by the given set

Exceptions
NullPointerException:
if the set is null
org.apache.commons.collections4 back to summary

public abstract Class SetUtils.SetView<E>

extends AbstractSet<E>
Class Inheritance
Type Parameters
<E>
the element type

An unmodifiable view of a set that may be backed by other sets.

If the decorated sets change, this view will change as well. The contents of this view can be transferred to another instance via the copyInto(Set) and toSet() methods.

Since
4.1

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public <
the set type
S extends Set<E>
>
void
copyInto(final S
the set for copying the contents
set
)

Copies the contents of this view into the provided set.

protected abstract Iterator<E>

Returns:

a new iterator for this view
createIterator
()

Return an iterator for this view; the returned iterator is not required to be unmodifiable.

public Iterator<E>
iterator()

Implements abstract java.util.AbstractCollection.iterator.

Implements java.util.Set.iterator, java.util.Collection.iterator.

Returns an iterator over the elements in this set.
public int
size()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.Set.size, java.util.Collection.size.

Returns the number of elements in this set (its cardinality).
public Set<E>

Returns:

a new set containing all elements of this view
toSet
()

Returns a new set containing the contents of this view.

Inherited from java.util.AbstractSet:
equalshashCoderemoveAll

Constructor Detail

SetViewback to summary
public SetView()

Method Detail

copyIntoback to summary
public <S extends Set<E>> void copyInto(final S set)

Copies the contents of this view into the provided set.

Parameters
<S>
the set type
set:S

the set for copying the contents

createIteratorback to summary
protected abstract Iterator<E> createIterator()

Return an iterator for this view; the returned iterator is not required to be unmodifiable.

Returns:Iterator<E>

a new iterator for this view

iteratorback to summary
public Iterator<E> iterator()

Implements abstract java.util.AbstractCollection.iterator.

Implements java.util.Set.iterator, java.util.Collection.iterator.

Doc from java.util.Set.iterator.

Returns an iterator over the elements in this set. The elements are returned in no particular order (unless this set is an instance of some class that provides a guarantee).

Returns:Iterator<E>

an iterator over the elements in this set

Annotations
@Override
sizeback to summary
public int size()

Implements abstract java.util.AbstractCollection.size.

Implements java.util.Set.size, java.util.Collection.size.

Doc from java.util.Set.size.

Returns the number of elements in this set (its cardinality). If this set contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.

Returns:int

the number of elements in this set (its cardinality)

Annotations
@Override
toSetback to summary
public Set<E> toSet()

Returns a new set containing the contents of this view.

Returns:Set<E>

a new set containing all elements of this view