public class ObjectLinkedOpenHashSet<K>
extends java.util.AbstractSet<K>
implements java.util.Set<K>, java.io.Serializable, java.lang.Cloneable
Instances of this class use a hash table to represent a set. The table is filled up to a specified load factor, and then doubled in size to accommodate new entries. If the table is emptied below one fourth of the load factor, it is halved in size; however, the table is never reduced to a size smaller than that at creation time: this approach makes it possible to create sets with a large capacity in which insertions and deletions do not cause immediately rehashing. Moreover, halving is not performed when deleting entries from an iterator, as it would interfere with the iteration process.
Note that clear()
does not modify the hash table size.
Iterators generated by this set will enumerate elements in the same order in which they have been added to the set (addition of elements already present in the set does not change the iteration order). Note that this order has nothing in common with the natural order of the keys. The order is kept by means of a doubly linked list, represented via an array of longs parallel to the table.
The iterators provided by this class are type-specific
list iterators, and can be started at any
element which is in the set (if the provided element is not in the
set, a NoSuchElementException
exception will be thrown). If, however,
the provided element is not the first or last element in the set, the first
access to the list index will require linear time, as in the worst case the
entire set must be scanned in iteration order to retrieve the positional
index of the starting element.
Modifier and Type | Field and Description |
---|---|
protected <any> |
hashingStrategy
The strategy used to hash objects in this collection.
|
Constructor and Description |
---|
ObjectLinkedOpenHashSet()
Creates a new hash set with initial expected
DEFAULT_INITIAL_SIZE elements and
DEFAULT_LOAD_FACTOR as load factor
DEFAULT_HASHING_STRATEGY default hashing strategy |
ObjectLinkedOpenHashSet(<any> strategy)
Creates a new hash set with initial expected
|
ObjectLinkedOpenHashSet(java.util.Collection<? extends K> c,
<any> strategy)
Creates a new hash set with
DEFAULT_LOAD_FACTOR as load factor
copying a given collection. |
ObjectLinkedOpenHashSet(int expected)
Creates a new hash set with
DEFAULT_LOAD_FACTOR as load factor
and with default hashing strategy DEFAULT_HASHING_STRATEGY |
ObjectLinkedOpenHashSet(int expected,
<any> strategy)
Creates a new hash set with
DEFAULT_LOAD_FACTOR as load factor. |
ObjectLinkedOpenHashSet(int expected,
float f,
<any> strategy)
Creates a new hash set.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(K k) |
boolean |
addAll(java.util.Collection<? extends K> c) |
K |
addOrGet(K k)
Add a random element if not present, get the existing value if already
present.
|
void |
clear() |
ObjectLinkedOpenHashSet<K> |
clone()
Returns a deep copy of this set.
|
boolean |
contains(java.lang.Object k) |
boolean |
equals(java.lang.Object o) |
K |
get(java.lang.Object k)
Returns the element of this set that is equal to the given key, or
null . |
int |
hashCode()
Returns a hash code for this set.
|
boolean |
isEmpty() |
java.util.Iterator<K> |
iterator()
Returns a type-specific list iterator on the elements in this set, starting
from the first element.
|
boolean |
remove(java.lang.Object k) |
int |
size() |
java.lang.String |
toString() |
finalize, getClass, notify, notifyAll, wait, wait, wait
protected final <any> hashingStrategy
public ObjectLinkedOpenHashSet()
DEFAULT_INITIAL_SIZE
elements and
DEFAULT_LOAD_FACTOR
as load factor
DEFAULT_HASHING_STRATEGY
default hashing strategypublic ObjectLinkedOpenHashSet(int expected)
DEFAULT_LOAD_FACTOR
as load factor
and with default hashing strategy DEFAULT_HASHING_STRATEGY
expected
- the expected number of elements in the hash set.public ObjectLinkedOpenHashSet(<any> strategy)
strategy
- TObjectHashingStrategy
used to compute hash codes and to compare objects.public ObjectLinkedOpenHashSet(int expected, <any> strategy)
DEFAULT_LOAD_FACTOR
as load factor.expected
- the expected number of elements in the hash set.strategy
- TObjectHashingStrategy
used to compute hash codes and to compare objects.public ObjectLinkedOpenHashSet(java.util.Collection<? extends K> c, <any> strategy)
DEFAULT_LOAD_FACTOR
as load factor
copying a given collection.c
- a Collection
to be copied into the new hash set.strategy
- TObjectHashingStrategy
used to compute hash codes and to compare objects.public ObjectLinkedOpenHashSet(int expected, float f, <any> strategy)
expected
/f
.expected
- the expected number of elements in the hash set.f
- the load factor.strategy
- TObjectHashingStrategy
used to compute hash codes and to compare objects.public boolean addAll(java.util.Collection<? extends K> c)
public boolean add(K k)
public K addOrGet(K k)
This is equivalent to (but faster than) doing a:
K exist = set.get(k); if (exist == null) { set.add(k); exist = k; }
public boolean remove(java.lang.Object k)
public boolean contains(java.lang.Object k)
public K get(java.lang.Object k)
null
.null
.public void clear()
public int size()
public boolean isEmpty()
public java.util.Iterator<K> iterator()
iterator
in interface java.lang.Iterable<K>
iterator
in interface java.util.Collection<K>
iterator
in interface java.util.Set<K>
iterator
in class java.util.AbstractCollection<K>
public ObjectLinkedOpenHashSet<K> clone()
This method performs a deep copy of this hash set; the data stored in the set, however, is not cloned. Note that this makes a difference only for object keys.
clone
in class java.lang.Object
public int hashCode()
This method overrides the generic method provided by the superclass. Since
equals()
is not overridden, it is important that the value returned by
this method is the same value as the one returned by the overridden method.
public boolean equals(java.lang.Object o)
public java.lang.String toString()
toString
in class java.util.AbstractCollection<K>