public class IdentityHashSet<E>
extends java.util.AbstractSet<E>
implements java.lang.Cloneable, java.io.Serializable
IdentityHashMap
-backed
implementation of the Set
interface. This means that
whether an object is an element of the set depends on whether it is ==
(rather than equals()
) to an element of the set. This is
different from a normal HashSet
, where set membership
depends on equals()
, 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).Constructor and Description |
---|
IdentityHashSet()
Construct a new, empty IdentityHashSet whose backing IdentityHashMap
has the default expected maximum size (21);
|
IdentityHashSet(java.util.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).
|
IdentityHashSet(int expectedMaxSize)
Construct a new, empty IdentityHashSet whose backing IdentityHashMap
has the specified expected maximum size.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(E o)
Adds the specified element to this set if it is not already present.
|
void |
clear()
Removes all of the elements from this set.
|
java.lang.Object |
clone()
Returns a shallow copy of this
IdentityHashSet instance:
the elements themselves are not cloned. |
boolean |
contains(java.lang.Object o)
Returns true if this set contains the specified element.
|
boolean |
isEmpty()
Returns
true if this set contains no elements. |
java.util.Iterator<E> |
iterator()
Returns an iterator over the elements in this set.
|
static void |
main(java.lang.String[] args)
Just for testing.
|
boolean |
remove(java.lang.Object o)
Removes the specified element from this set if it is present.
|
int |
size()
Returns the number of elements in this set (its cardinality).
|
addAll, containsAll, retainAll, toArray, toArray, toString
finalize, getClass, notify, notifyAll, wait, wait, wait
public IdentityHashSet()
public IdentityHashSet(int expectedMaxSize)
expectedMaxSize
- the expected maximum size of the set.public IdentityHashSet(java.util.Collection<? extends E> c)
c
- a Collection containing the elements with which this set will
be initialized.public boolean add(E o)
equals()
) to test whether an element is present in the
set.public void clear()
public java.lang.Object clone()
IdentityHashSet
instance:
the elements themselves are not cloned.clone
in class java.lang.Object
public boolean contains(java.lang.Object o)
equals()
) to test whether an element is present in the
set.public boolean isEmpty()
true
if this set contains no elements.public java.util.Iterator<E> iterator()
public boolean remove(java.lang.Object o)
equals()
) to test whether an element is present in the
set.public int size()
public static void main(java.lang.String[] args)