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
DisjointSets. 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, DisjointSets 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 TypeMethodDescriptionbooleanEnsures that this collection contains the specified element.booleanaddAll(Collection<? extends E> c) Adds all of the elements in the specified collection to this set.voidclear()Removes all of the elements from this set.booleanReturnstrueif this set contains the specified element.booleancontainsAll(Collection<?> c) Returnstrueif this set contains all of the elements in the specified collection.booleanCompare this set with the specified object for equality.getTrash()Returns the trash set, ornullif there is none.inthashCode()Returns an hash value for this set.iterator()Returns an iterator over the elements in this collection.booleanRemoves a single instance of the specified element from this set, if it is present.booleanremoveAll(Collection<?> c) Removes from this set all of its elements that are contained in the specified collection.booleanretainAll(Collection<?> c) Retains only the elements in this set that are contained in the specified collection.intsize()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
isEmptyMethods inherited from interface Collection
parallelStream, removeIf, stream, toArrayMethods 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.DisjointSets 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 withdisjointSetwill also be mutually exclusive with the newly created set. IfdisjointSethas a trash set, the newly created set will use the same trash (i.e. allremoveoperations will really move the element to the trash set). Otherwise, the newDisjointSethave no trash.- Parameters:
disjointSet- The set to be disjoint from.
-
-
Method Details
-
getTrash
Returns the trash set, ornullif there is none. The trash set receive all elements removed from this set.- Returns:
- The trash set, or
nullif 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:
sizein interfaceCollection<E>- Specified by:
sizein interfaceSet<E>- Specified by:
sizein classAbstractCollection<E>
-
contains
Returnstrueif this set contains the specified element.- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceSet<E>- Overrides:
containsin classAbstractCollection<E>- Parameters:
element- Object to be checked for containment in this set.- Returns:
trueif 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:
addin interfaceCollection<E>- Specified by:
addin interfaceSet<E>- Overrides:
addin classAbstractCollection<E>- Parameters:
element- Element whose presence in this set is to be ensured.- Returns:
trueif 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 thisDisjointSethas a trash set, the removed element will be added to the trash set.- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceSet<E>- Overrides:
removein classAbstractCollection<E>- Parameters:
element- Element to be removed from this set.- Returns:
trueif the set changed as a result of the call.
-
containsAll
Returnstrueif this set contains all of the elements in the specified collection.- Specified by:
containsAllin interfaceCollection<E>- Specified by:
containsAllin interfaceSet<E>- Overrides:
containsAllin classAbstractCollection<E>- Parameters:
c- collection to be checked for containment in this collection.- Returns:
trueif 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:
addAllin interfaceCollection<E>- Specified by:
addAllin interfaceSet<E>- Overrides:
addAllin classAbstractCollection<E>- Parameters:
c- collection whose elements are to be added to this set.- Returns:
trueif 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 thisDisjointSethas a trash set, all removed elements will be added to the trash set.- Specified by:
removeAllin interfaceCollection<E>- Specified by:
removeAllin interfaceSet<E>- Overrides:
removeAllin classAbstractSet<E>- Parameters:
c- elements to be removed from this set.- Returns:
trueif 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 thisDisjointSethas a trash set, all removed elements will be added to the trash set.- Specified by:
retainAllin interfaceCollection<E>- Specified by:
retainAllin interfaceSet<E>- Overrides:
retainAllin classAbstractCollection<E>- Parameters:
c- elements to be retained in this collection.- Returns:
trueif this collection changed as a result of the call.
-
clear
public void clear()Removes all of the elements from this set. If thisDisjointSethas a trash set, all removed elements will be added to the trash set.- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceSet<E>- Overrides:
clearin 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:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceSet<E>- Overrides:
toArrayin 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:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceSet<E>- Overrides:
toArrayin 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:
toStringin classAbstractCollection<E>
-
hashCode
public int hashCode()Returns an hash value for this set.- Specified by:
hashCodein interfaceCollection<E>- Specified by:
hashCodein interfaceSet<E>- Overrides:
hashCodein classAbstractSet<E>
-
equals
Compare this set with the specified object for equality.- Specified by:
equalsin interfaceCollection<E>- Specified by:
equalsin interfaceSet<E>- Overrides:
equalsin classAbstractSet<E>- Parameters:
set- The object to compare with this set for equality.
-