public class EquivalentHashSet<E>
extends java.util.AbstractSet<E>
Equivalence
function implementations for values,
as opposed to relying on their own equals/hashCode/toString implementations.
This is handy when using key/values whose mentioned methods cannot be
overriden, i.e. arrays, and in situations where users want to avoid using
wrapper objects.Modifier and Type | Field and Description |
---|---|
private Equivalence<E> |
entryEq
Equivalence function for the entries of this hash set, providing
functionality to check equality, calculate hash codes...etc of the
stored entries.
|
private EquivalentHashMap<E,java.lang.Boolean> |
m
The underlying map.
|
Constructor and Description |
---|
EquivalentHashSet(Equivalence<E> entryEq)
Constructs a new, empty set, with a given equivalence function
|
EquivalentHashSet(int initialCapacity,
Equivalence<E> entryEq)
Constructs a new, empty set, with a given initial capacity and a
particular equivalence function to compare entries.
|
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.
|
boolean |
contains(java.lang.Object o)
Returns
true if this set contains the specified element. |
int |
hashCode()
Returns the hash code value for this set using the
Equivalence
function associated with it. |
boolean |
isEmpty()
Returns
true if this set contains no elements. |
java.util.Iterator<E> |
iterator()
Returns an iterator over the elements in this set.
|
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.
|
addAll, containsAll, retainAll, toArray, toArray, toString
private final Equivalence<E> entryEq
private final EquivalentHashMap<E,java.lang.Boolean> m
public EquivalentHashSet(Equivalence<E> entryEq)
entryEq
- the Equivalence function to be used to compare entries
in this set.public EquivalentHashSet(int initialCapacity, Equivalence<E> entryEq)
initialCapacity
- this set's initial capacityentryEq
- the Equivalence function to be used to compare entries
in this set.public java.util.Iterator<E> iterator()
public int size()
Integer.MAX_VALUE
elements, it
returns Integer.MAX_VALUE
.public boolean isEmpty()
true
if this set contains no elements.public boolean contains(java.lang.Object o)
true
if this set contains the specified element.public boolean add(E o)
public boolean remove(java.lang.Object o)
public void clear()
public int hashCode()
Equivalence
function associated with it. The hash code of a set is defined to be
the sum of the hash codes of the elements in the set, where the hash
code of a null element is defined to be zero. This ensures that
Equivalence.equals(Object s1, Object s2)
implies that
Equivalence.hashCode(Object s1)
==Equivalence.hashCode(Object s2)
for any two sets s1 and s2, as required by the general
contract of Object.hashCode()
.
This implementation iterates over the set, calling the hashCode method on each element in the set, and adding up the results.