public final class IdentityHashSet<T> extends AbstractSet<T>
Implements the Set interface with a hash table, using reference-equality in place of object-equality when comparing elements. In other words, in an IdentityHashSet, two elements e1 and e2 are considered equal if and only if (e1==e2). (In normal Set implementations (like Set) two elements e1 and e2 are considered equal if and only if (e1==null ? e2==null : e1.equals(e2)).)
This class is not a general-purpose Set implementation! While this class implements the Set interface, it intentionally violates Set's general contract, which mandates the use of the equals method when comparing objects. This class is designed for use only in the rare cases wherein reference-equality semantics are required.
This class provides all of the optional set operations, and permits null elements. This class makes no guarantees as to the order of the set; in particular, it does not guarantee that the order will remain constant over time.
This class provides constant-time performance for the basic
operations (get and put), assuming the system
identity hash function (System.identityHashCode(Object)
)
disperses elements properly among the buckets.
This class has one tuning parameter (which affects performance but not semantics): expected maximum size. This parameter is the maximum number of elements that the set is expected to hold. Internally, this parameter is used to determine the number of buckets initially comprising the hash table. The precise relationship between the expected maximum size and the number of buckets is unspecified.
If the size of the set sufficiently exceeds the expected maximum size, the number of buckets is increased Increasing the number of buckets ("rehashing") may be fairly expensive, so it pays to create identity hash sets with a sufficiently large expected maximum size. On the other hand, iteration requires time proportional to the number of buckets in the hash table, so it pays not to set the expected maximum size too high if you are especially concerned with iteration performance or memory usage.
Note that this implementation is not synchronized. The iterators returned by all of this class are not fail-fast: in the face of concurrent modification, the iterator risks arbitrary, non-deterministic behavior at an undetermined time in the future.
Implementation note: This is a simple linear-probe hash table,
as described for example in texts by Sedgewick and Knuth. For many JRE
implementations and operation mixes, this class will yield better performance than
HashSet
(which uses chaining rather than linear-probing).
elems: set T
Constructor and Description |
---|
IdentityHashSet()
Constructs a new, empty identity hash map with a default expected
maximum size of 16.
|
IdentityHashSet(Collection<? extends T> c)
Constructs a new identity hash set containing the elements
in the specified collection.
|
IdentityHashSet(int expectedMaxSize)
Constructs a new, empty set with the specified expected maximum size.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(T element) |
boolean |
addAll(Collection<? extends T> c) |
void |
clear() |
boolean |
contains(Object o)
Tests whether the specified object reference is an element in this identity
hash map.
|
boolean |
equals(Object o)
Compares the specified object with this set for equality.
|
int |
hashCode()
Returns the hash code value for this set.
|
boolean |
isEmpty() |
Iterator<T> |
iterator() |
boolean |
remove(Object o) |
boolean |
removeAll(Collection<?> c) |
int |
size() |
containsAll, retainAll, toArray, toArray, toString
containsAll, retainAll, spliterator, toArray, toArray
parallelStream, removeIf, stream
public IdentityHashSet()
no this.elems'
public IdentityHashSet(Collection<? extends T> c)
this.elems' = c.elems
NullPointerException
- c = nullpublic IdentityHashSet(int expectedMaxSize)
no this.elems'
IllegalArgumentException
- expectedMaxSize < 0public boolean add(T element)
add
in interface Collection<T>
add
in interface Set<T>
add
in class AbstractCollection<T>
public boolean addAll(Collection<? extends T> c)
addAll
in interface Collection<T>
addAll
in interface Set<T>
addAll
in class AbstractCollection<T>
public void clear()
clear
in interface Collection<T>
clear
in interface Set<T>
clear
in class AbstractCollection<T>
public boolean contains(Object o)
contains
in interface Collection<T>
contains
in interface Set<T>
contains
in class AbstractCollection<T>
public boolean equals(Object o)
Owing to the reference-equality-based semantics of this set it is possible that the symmetry and transitivity requirements of the Object.equals contract may be violated if this set is compared to a normal set. However, the Object.equals contract is guaranteed to hold among IdentityHashSet instances.
equals
in interface Collection<T>
equals
in interface Set<T>
equals
in class AbstractSet<T>
Object.equals(Object)
public int hashCode()
Object.hashCode()
.
Owing to the reference-equality-based semantics of the elements in this set, it is possible that the contractual requirement of Object.hashCode mentioned in the previous paragraph will be violated if one of the two objects being compared is an IdentityHashSet instance and the other is a normal set.
hashCode
in interface Collection<T>
hashCode
in interface Set<T>
hashCode
in class AbstractSet<T>
Object.hashCode()
,
Object.equals(Object)
,
equals(Object)
public boolean isEmpty()
isEmpty
in interface Collection<T>
isEmpty
in interface Set<T>
isEmpty
in class AbstractCollection<T>
public boolean remove(Object o)
remove
in interface Collection<T>
remove
in interface Set<T>
remove
in class AbstractCollection<T>
public boolean removeAll(Collection<?> c)
removeAll
in interface Collection<T>
removeAll
in interface Set<T>
removeAll
in class AbstractSet<T>
public int size()
size
in interface Collection<T>
size
in interface Set<T>
size
in class AbstractCollection<T>
© Emina Torlak 2005-present