Package org.geotools.util
Class DisjointSet<E>
Object
AbstractCollection<E>
AbstractSet<E>
DisjointSet<E>
- Type Parameters:
E
- The type of elements in this set.
- All Implemented Interfaces:
Serializable
,Iterable<E>
,Collection<E>
,Set<E>
A set which is disjoint from others
DisjointSet
s. Two sets are disjoint (or mutually exclusive) if
their intersection is the empty set. Adding an element to a DisjointSet
remove it from any other mutually
exclusive DisjointSet
. Optionnaly, DisjointSet
s may also have a trash set receiving removed elements.
The example below creates 3 mutually exclusive sets with a trash:
Disjoint sets are thread-safe.DisjointSet set0 = new DisjointSet(true); // Used as the trash set. DisjointSet set1 = new DisjointSet(set0); DisjointSet set2 = new DisjointSet(set0);
- Since:
- 2.0
- Author:
- Martin Desruisseaux (IRD)
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionConstruct a initially empty set.DisjointSet
(boolean hasTrash) Construct a initially empty set with an optional trash set.DisjointSet
(DisjointSet<E> disjointSet) Construct a new set mutually exclusive with the specified set. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Ensures that this collection contains the specified element.boolean
addAll
(Collection<? extends E> c) Adds all of the elements in the specified collection to this set.void
clear()
Removes all of the elements from this set.boolean
Returnstrue
if this set contains the specified element.boolean
containsAll
(Collection<?> c) Returnstrue
if this set contains all of the elements in the specified collection.boolean
Compare this set with the specified object for equality.getTrash()
Returns the trash set, ornull
if there is none.int
hashCode()
Returns an hash value for this set.iterator()
Returns an iterator over the elements in this collection.boolean
Removes a single instance of the specified element from this set, if it is present.boolean
removeAll
(Collection<?> c) Removes from this set all of its elements that are contained in the specified collection.boolean
retainAll
(Collection<?> c) Retains only the elements in this set that are contained in the specified collection.int
size()
Returns the number of elements in this set.Object[]
toArray()
Returns an array containing all of the elements in this collection.<T> T[]
toArray
(T[] a) Returns an array containing all of the elements in this collection.toString()
Returns a string representation of this set.Methods inherited from class AbstractCollection
isEmpty
Methods inherited from interface Collection
parallelStream, removeIf, stream, toArray
Methods inherited from interface Set
isEmpty, spliterator
-
Constructor Details
-
DisjointSet
public DisjointSet()Construct a initially empty set. There is initially no other set mutually exclusive with this one. Mutually exclusive sets must be created using theDisjointSet(DisjointSet)
constructor with this newly created set as argument.DisjointSet
s constructed using this constructor has no trash. All remove operations on this set really remove all references to the removed element, like a usualSet
. This is opposed to moving the element to a "trash" set, which is allowed by theDisjointSet(true)
constructor. -
DisjointSet
public DisjointSet(boolean hasTrash) Construct a initially empty set with an optional trash set. There is initially no other set mutually exclusive with this one. Mutually exclusive sets must be created using theDisjointSet(DisjointSet)
constructor with this newly created set as argument.- Parameters:
hasTrash
- Iftrue
, all remove operations will add removed elements to a trash set (thus, really just moving the element to the trash). Iffalse
, there is no trash and this constructor behave like the no-argument constructor.- See Also:
-
DisjointSet
Construct a new set mutually exclusive with the specified set. All sets mutually exclusive withdisjointSet
will also be mutually exclusive with the newly created set. IfdisjointSet
has a trash set, the newly created set will use the same trash (i.e. allremove
operations will really move the element to the trash set). Otherwise, the newDisjointSet
have no trash.- Parameters:
disjointSet
- The set to be disjoint from.
-
-
Method Details
-
getTrash
Returns the trash set, ornull
if there is none. The trash set receive all elements removed from this set.- Returns:
- The trash set, or
null
if none.
-
size
public int size()Returns the number of elements in this set. The size of this set may change as a result of adding elements to a mutually exclusive set.- Specified by:
size
in interfaceCollection<E>
- Specified by:
size
in interfaceSet<E>
- Specified by:
size
in classAbstractCollection<E>
-
contains
Returnstrue
if this set contains the specified element.- Specified by:
contains
in interfaceCollection<E>
- Specified by:
contains
in interfaceSet<E>
- Overrides:
contains
in classAbstractCollection<E>
- Parameters:
element
- Object to be checked for containment in this set.- Returns:
true
if this set contains the specified element.
-
add
Ensures that this collection contains the specified element. Adding an element to this set will remove it from any mutually exclusive set.- Specified by:
add
in interfaceCollection<E>
- Specified by:
add
in interfaceSet<E>
- Overrides:
add
in classAbstractCollection<E>
- Parameters:
element
- Element whose presence in this set is to be ensured.- Returns:
true
if the set changed as a result of the call.
-
remove
Removes a single instance of the specified element from this set, if it is present. If thisDisjointSet
has a trash set, the removed element will be added to the trash set.- Specified by:
remove
in interfaceCollection<E>
- Specified by:
remove
in interfaceSet<E>
- Overrides:
remove
in classAbstractCollection<E>
- Parameters:
element
- Element to be removed from this set.- Returns:
true
if the set changed as a result of the call.
-
containsAll
Returnstrue
if this set contains all of the elements in the specified collection.- Specified by:
containsAll
in interfaceCollection<E>
- Specified by:
containsAll
in interfaceSet<E>
- Overrides:
containsAll
in classAbstractCollection<E>
- Parameters:
c
- collection to be checked for containment in this collection.- Returns:
true
if this set contains all of the elements in the specified collection.
-
addAll
Adds all of the elements in the specified collection to this set. All of the elements will be removed from mutually exclusive sets.- Specified by:
addAll
in interfaceCollection<E>
- Specified by:
addAll
in interfaceSet<E>
- Overrides:
addAll
in classAbstractCollection<E>
- Parameters:
c
- collection whose elements are to be added to this set.- Returns:
true
if this set changed as a result of the call.
-
removeAll
Removes from this set all of its elements that are contained in the specified collection. If thisDisjointSet
has a trash set, all removed elements will be added to the trash set.- Specified by:
removeAll
in interfaceCollection<E>
- Specified by:
removeAll
in interfaceSet<E>
- Overrides:
removeAll
in classAbstractSet<E>
- Parameters:
c
- elements to be removed from this set.- Returns:
true
if this set changed as a result of the call.
-
retainAll
Retains only the elements in this set that are contained in the specified collection. If thisDisjointSet
has a trash set, all removed elements will be added to the trash set.- Specified by:
retainAll
in interfaceCollection<E>
- Specified by:
retainAll
in interfaceSet<E>
- Overrides:
retainAll
in classAbstractCollection<E>
- Parameters:
c
- elements to be retained in this collection.- Returns:
true
if this collection changed as a result of the call.
-
clear
public void clear()Removes all of the elements from this set. If thisDisjointSet
has a trash set, all removed elements will be added to the trash set.- Specified by:
clear
in interfaceCollection<E>
- Specified by:
clear
in interfaceSet<E>
- Overrides:
clear
in classAbstractCollection<E>
-
iterator
Returns an iterator over the elements in this collection. -
toArray
Returns an array containing all of the elements in this collection.- Specified by:
toArray
in interfaceCollection<E>
- Specified by:
toArray
in interfaceSet<E>
- Overrides:
toArray
in classAbstractCollection<E>
- Returns:
- an array containing all of the elements in this set.
-
toArray
public <T> T[] toArray(T[] a) Returns an array containing all of the elements in this collection.- Specified by:
toArray
in interfaceCollection<E>
- Specified by:
toArray
in interfaceSet<E>
- Overrides:
toArray
in classAbstractCollection<E>
- Type Parameters:
T
- The type of elements in the array.- Parameters:
a
- The array into which the elements of the set are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.- Returns:
- an array containing the elements of the set.
-
toString
Returns a string representation of this set.- Overrides:
toString
in classAbstractCollection<E>
-
hashCode
public int hashCode()Returns an hash value for this set.- Specified by:
hashCode
in interfaceCollection<E>
- Specified by:
hashCode
in interfaceSet<E>
- Overrides:
hashCode
in classAbstractSet<E>
-
equals
Compare this set with the specified object for equality.- Specified by:
equals
in interfaceCollection<E>
- Specified by:
equals
in interfaceSet<E>
- Overrides:
equals
in classAbstractSet<E>
- Parameters:
set
- The object to compare with this set for equality.
-