Package edu.stanford.nlp.util
Class IdentityHashSet<E>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractSet<E>
-
- edu.stanford.nlp.util.IdentityHashSet<E>
-
- All Implemented Interfaces:
Serializable,Cloneable,Iterable<E>,Collection<E>,Set<E>
public class IdentityHashSet<E> extends AbstractSet<E> implements Cloneable, Serializable
This class provides aIdentityHashMap-backed implementation of theSetinterface. This means that whether an object is an element of the set depends on whether it is == (rather thanequals()) to an element of the set. This is different from a normalHashSet, where set membership depends onequals(), rather than ==. Each element in the set is a key in the backing IdentityHashMap; each key maps to a static token, denoting that the key does, in fact, exist. Most operations are O(1), assuming no hash collisions. In the worst case (where all hashes collide), operations are O(n).- Author:
- Bill MacCartney
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description IdentityHashSet()Construct a new, empty IdentityHashSet whose backing IdentityHashMap has the default expected maximum size (21);IdentityHashSet(int expectedMaxSize)Construct a new, empty IdentityHashSet whose backing IdentityHashMap has the specified expected maximum size.IdentityHashSet(Collection<? extends E> c)Construct a new IdentityHashSet with the same elements as the supplied Collection (eliminating any duplicates, of course); the backing IdentityHashMap will have the default expected maximum size (21).
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(E o)Adds the specified element to this set if it is not already present.voidclear()Removes all of the elements from this set.Objectclone()Returns a shallow copy of thisIdentityHashSetinstance: the elements themselves are not cloned.booleancontains(Object o)Returns true if this set contains the specified element.booleanisEmpty()Returnstrueif this set contains no elements.Iterator<E>iterator()Returns an iterator over the elements in this set.static voidmain(String[] args)Just for testing.booleanremove(Object o)Removes the specified element from this set if it is present.intsize()Returns the number of elements in this set (its cardinality).-
Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAll
-
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, retainAll, toArray, toArray, 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
addAll, containsAll, retainAll, spliterator, toArray, toArray
-
-
-
-
Constructor Detail
-
IdentityHashSet
public IdentityHashSet()
Construct a new, empty IdentityHashSet whose backing IdentityHashMap has the default expected maximum size (21);
-
IdentityHashSet
public IdentityHashSet(int expectedMaxSize)
Construct a new, empty IdentityHashSet whose backing IdentityHashMap has the specified expected maximum size. Putting more than the expected number of elements into the set may cause the internal data structure to grow, which may be somewhat time-consuming.- Parameters:
expectedMaxSize- the expected maximum size of the set.
-
IdentityHashSet
public IdentityHashSet(Collection<? extends E> c)
Construct a new IdentityHashSet with the same elements as the supplied Collection (eliminating any duplicates, of course); the backing IdentityHashMap will have the default expected maximum size (21).- Parameters:
c- a Collection containing the elements with which this set will be initialized.
-
-
Method Detail
-
add
public boolean add(E o)
Adds the specified element to this set if it is not already present. Remember that this set implementation uses == (notequals()) to test whether an element is present in the set.- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceSet<E>- Overrides:
addin classAbstractCollection<E>- Parameters:
o- element to add to this set- Returns:
- true if the element was added, false otherwise
-
clear
public void clear()
Removes all of the elements from this set.- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceSet<E>- Overrides:
clearin classAbstractCollection<E>
-
clone
public Object clone()
Returns a shallow copy of thisIdentityHashSetinstance: the elements themselves are not cloned.
-
contains
public boolean contains(Object o)
Returns true if this set contains the specified element. Remember that this set implementation uses == (notequals()) to test whether an element is present in the set.- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceSet<E>- Overrides:
containsin classAbstractCollection<E>- Parameters:
o- Element whose presence in this set is to be tested.- Returns:
trueif this set contains the specified element.
-
isEmpty
public boolean isEmpty()
Returnstrueif this set contains no elements.- Specified by:
isEmptyin interfaceCollection<E>- Specified by:
isEmptyin interfaceSet<E>- Overrides:
isEmptyin classAbstractCollection<E>- Returns:
trueif this set contains no elements.
-
iterator
public Iterator<E> iterator()
Returns an iterator over the elements in this set. The elements are returned in no particular order.
-
remove
public boolean remove(Object o)
Removes the specified element from this set if it is present. Remember that this set implementation uses == (notequals()) to test whether an element is present in the set.- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceSet<E>- Overrides:
removein classAbstractCollection<E>- Parameters:
o- Object to be removed from this set, if present.- Returns:
trueif the set contained the specified element.
-
size
public int size()
Returns the number of elements in this set (its cardinality).- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceSet<E>- Specified by:
sizein classAbstractCollection<E>- Returns:
- the number of elements in this set (its cardinality).
-
main
public static void main(String[] args)
Just for testing.
-
-