public class IdentityHashSet<E> extends AbstractSet<E> implements Cloneable, 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(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.
|
Object |
clone()
Returns a shallow copy of this
IdentityHashSet instance:
the elements themselves are not cloned. |
boolean |
contains(Object o)
Returns true if this set contains the specified element.
|
boolean |
isEmpty()
Returns
true if this set contains no elements. |
Iterator<E> |
iterator()
Returns an iterator over the elements in this set.
|
static void |
main(String[] args)
Just for testing.
|
boolean |
remove(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).
|
equals, hashCode, removeAll
addAll, containsAll, retainAll, toArray, toArray, toString
finalize, getClass, notify, notifyAll, wait, wait, wait
addAll, containsAll, retainAll, spliterator, toArray, toArray
parallelStream, removeIf, stream
public IdentityHashSet()
public IdentityHashSet(int expectedMaxSize)
expectedMaxSize
- the expected maximum size of the set.public IdentityHashSet(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.add
in interface Collection<E>
add
in interface Set<E>
add
in class AbstractCollection<E>
o
- element to add to this setpublic void clear()
clear
in interface Collection<E>
clear
in interface Set<E>
clear
in class AbstractCollection<E>
public Object clone()
IdentityHashSet
instance:
the elements themselves are not cloned.public boolean contains(Object o)
equals()
) to test whether an element is present in the
set.contains
in interface Collection<E>
contains
in interface Set<E>
contains
in class AbstractCollection<E>
o
- Element whose presence in this set is to be
tested.true
if this set contains the specified element.public boolean isEmpty()
true
if this set contains no elements.isEmpty
in interface Collection<E>
isEmpty
in interface Set<E>
isEmpty
in class AbstractCollection<E>
true
if this set contains no elements.public Iterator<E> iterator()
public boolean remove(Object o)
equals()
) to test whether an element is present in the
set.remove
in interface Collection<E>
remove
in interface Set<E>
remove
in class AbstractCollection<E>
o
- Object to be removed from this set, if present.true
if the set contained the specified element.public int size()
size
in interface Collection<E>
size
in interface Set<E>
size
in class AbstractCollection<E>
public static void main(String[] args)