Package com.tangosol.util
Class LiteSet<E>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractSet<E>
com.tangosol.util.LiteSet<E>
- All Implemented Interfaces:
ExternalizableLite
,Externalizable
,Serializable
,Cloneable
,Iterable<E>
,Collection<E>
,Set<E>
public class LiteSet<E>
extends AbstractSet<E>
implements Cloneable, Externalizable, ExternalizableLite
An implementation of java.util.Set that is optimal (in terms of both size
and speed) for very small sets of data but still works excellently with
large sets of data. This implementation is not thread-safe.
The LiteSet implementation switches at runtime between several different sub-implementations for storing the set of objects, described here:
- "empty set" - a set that contains no data;
- "single entry" - a reference directly to an item is used to represent a set with exactly one item in it;
- "Object[]" - a reference is held to an array of Objects that store the contents of the Set; the item limit for this implementation is determined by the THRESHOLD constant;
- "delegation" - for more than THRESHOLD items, a set is created to
delegate the set management to; sub-classes can override the default
delegation class (java.util.HashSet) by overriding the factory method
instantiateSet()
.
The LiteSet implementation supports the null value.
- Author:
- cp 06/02/99
- See Also:
-
Constructor Summary
ConstructorDescriptionLiteSet()
Construct a LiteSetLiteSet
(Collection<? extends E> collection) Construct a LiteSet containing the elements of the passed Collection. -
Method Summary
Modifier and TypeMethodDescriptionboolean
Ensures that this Set contains the specified element.boolean
addAll
(Collection<? extends E> collection) Adds all of the elements in the specified Collection to this Set if they are not already present.protected void
After a mutation operation has reduced the size of an underlying Set, check if the delegation model should be replaced with a more size- efficient storage approach, and switch accordingly.void
clear()
Removes all of the elements from this Set.clone()
Create a clone of this Set.boolean
Returns true if this Set contains the specified element.boolean
containsAll
(Collection<?> collection) Returns true if this Set contains all of the elements in the specified Collection.elements()
Returns an Enumerator over the elements in this Set.protected void
initFromArray
(Object[] ao, int c) Initialize the contents of this Set from the passed array ao containing c values.(Factory pattern) Instantiate a Set object to store items in once the "lite" threshold has been exceeded.boolean
isEmpty()
Determine if this Set is empty.iterator()
Returns an Iterator over the elements in this Set.void
Restore the contents of this object by loading the object's state from the passed DataInput object.void
Initialize this object from the data in the passed ObjectInput stream.boolean
Removes the specified element from this Set if it is present.boolean
removeAll
(Collection<?> collection) Removes from this Set all of its elements that are contained in the specified Collection.boolean
retainAll
(Collection<?> collection) Retains only the elements in this Set that are contained in the specified Collection.int
size()
Returns the number of elements in this Set (its cardinality).Object[]
toArray()
Returns an array containing all of the elements in this Set.Object[]
Returns an array (whose runtime type is that of the specified array) containing all of the elements in this Set.void
writeExternal
(DataOutput out) Save the contents of this object by storing the object's state into the passed DataOutput object.void
Write this object's data to the passed ObjectOutput stream.Methods inherited from class java.util.AbstractSet
equals, hashCode
Methods inherited from class java.util.AbstractCollection
toString
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
Methods inherited from interface java.util.Set
spliterator
-
Constructor Details
-
LiteSet
public LiteSet()Construct a LiteSet -
LiteSet
Construct a LiteSet containing the elements of the passed Collection.- Parameters:
collection
- a Collection
-
-
Method Details
-
isEmpty
public boolean isEmpty()Determine if this Set is empty.- Specified by:
isEmpty
in interfaceCollection<E>
- Specified by:
isEmpty
in interfaceSet<E>
- Overrides:
isEmpty
in classAbstractCollection<E>
- Returns:
- true iff this Set is empty
-
size
public int size()Returns the number of elements in this Set (its cardinality).- Specified by:
size
in interfaceCollection<E>
- Specified by:
size
in interfaceSet<E>
- Specified by:
size
in classAbstractCollection<E>
- Returns:
- the number of elements in this Set
-
contains
Returns true if this Set contains the specified element. More formally, returns true if and only if this Set contains an elemente
such that(o==null ? e==null : o.equals(e))
.- Specified by:
contains
in interfaceCollection<E>
- Specified by:
contains
in interfaceSet<E>
- Overrides:
contains
in classAbstractCollection<E>
- Parameters:
o
- the object to check for- Returns:
- true if this Set contains the specified element
-
iterator
Returns an Iterator over the elements in this Set. The elements are returned in an arbitrary order. -
elements
Returns an Enumerator over the elements in this Set. The elements are returned in an arbitrary order.- Returns:
- an Enumerator over the elements in this Set
-
toArray
Returns an array containing all of the elements in this Set. Obeys the general contract of the Set.toArray method.- 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
Returns an array (whose runtime type is that of the specified array) containing all of the elements in this Set. Obeys the general contract of the Set.toArray(Object[]) method.- Specified by:
toArray
in interfaceCollection<E>
- Specified by:
toArray
in interfaceSet<E>
- Overrides:
toArray
in classAbstractCollection<E>
- Parameters:
aDest
- the array into which the elements of this 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 this Set
- Throws:
ArrayStoreException
- if the component type of aDest is not a supertype of the type of every element in this Set
-
add
Ensures that this Set contains the specified element. Returns true if the Set changed as a result of the call. (Returns false if this Set already contains the specified element.)- Specified by:
add
in interfaceCollection<E>
- Specified by:
add
in interfaceSet<E>
- Overrides:
add
in classAbstractCollection<E>
- Parameters:
o
- element to be added to this Set- Returns:
- true if this Set did not already contain the specified element
-
remove
Removes the specified element from this Set if it is present. More formally, removes an elemente
such that(o==null ? e==null : o.equals(e))
, if the Set contains such an element. Returns true if the Set contained the specified element (or equivalently, if the Set changed as a result of the call). The Set will not contain the specified element once the call returns.- Specified by:
remove
in interfaceCollection<E>
- Specified by:
remove
in interfaceSet<E>
- Overrides:
remove
in classAbstractCollection<E>
- Parameters:
o
- object to be removed from this Set, if present- Returns:
- true if the Set contained the specified element
-
containsAll
Returns true 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:
collection
- Collection to be checked for containment in this Set- 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 if they are not already present. If the specified Collection is also a Set, the addAll operation effectively modifies this Set so that its value is the union of the two Sets.- Specified by:
addAll
in interfaceCollection<E>
- Specified by:
addAll
in interfaceSet<E>
- Overrides:
addAll
in classAbstractCollection<E>
- Parameters:
collection
- Collection whose elements are to be added to 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. In other words, removes from this Set all of its elements that are not contained in the specified Collection. If the specified Collection is also a Set, this operation effectively modifies this Set so that its value is the intersection of the two Sets.- Specified by:
retainAll
in interfaceCollection<E>
- Specified by:
retainAll
in interfaceSet<E>
- Overrides:
retainAll
in classAbstractCollection<E>
- Parameters:
collection
- collection that defines which elements this Set will retain- 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 the specified Collection is also a Set, this operation effectively modifies this Set so that its value is the asymmetric set difference of the two Sets.- Specified by:
removeAll
in interfaceCollection<E>
- Specified by:
removeAll
in interfaceSet<E>
- Overrides:
removeAll
in classAbstractSet<E>
- Parameters:
collection
- Collection that defines which elements will be removed from this Set- Returns:
- true if this Set changed as a result of the call
-
clear
public void clear()Removes all of the elements from this Set. This Set will be empty after this call returns.- Specified by:
clear
in interfaceCollection<E>
- Specified by:
clear
in interfaceSet<E>
- Overrides:
clear
in classAbstractCollection<E>
-
clone
Create a clone of this Set. -
readExternal
Initialize this object from the data in the passed ObjectInput stream.- Specified by:
readExternal
in interfaceExternalizable
- Parameters:
in
- the stream to read data from in order to restore the object- Throws:
IOException
- if an I/O exception occursClassNotFoundException
-
writeExternal
Write this object's data to the passed ObjectOutput stream.- Specified by:
writeExternal
in interfaceExternalizable
- Parameters:
out
- the stream to write the object to- Throws:
IOException
- if an I/O exception occurs
-
readExternal
Restore the contents of this object by loading the object's state from the passed DataInput object.- Specified by:
readExternal
in interfaceExternalizableLite
- Parameters:
in
- the DataInput stream to read data from in order to restore the state of this object- Throws:
IOException
- if an I/O exception occurs
-
writeExternal
Save the contents of this object by storing the object's state into the passed DataOutput object.- Specified by:
writeExternal
in interfaceExternalizableLite
- Parameters:
out
- the DataOutput stream to write the state of this object to- Throws:
IOException
- if an I/O exception occurs
-
instantiateSet
(Factory pattern) Instantiate a Set object to store items in once the "lite" threshold has been exceeded. This method permits inheriting classes to easily override the choice of the Set object.- Returns:
- an instance of Set
-
initFromArray
Initialize the contents of this Set from the passed array ao containing c values.- Parameters:
ao
- the array that contains the values to place in this Setc
- the number of values that will be placed into this Set
-
checkShrinkFromOther
protected void checkShrinkFromOther()After a mutation operation has reduced the size of an underlying Set, check if the delegation model should be replaced with a more size- efficient storage approach, and switch accordingly.
-