jtp.util
Class ReplacementHashSet
java.lang.Object
|
+--java.util.AbstractCollection
|
+--java.util.AbstractSet
|
+--jtp.util.ReplacementHashSet
- All Implemented Interfaces:
- Collection, Set
- public class ReplacementHashSet
- extends AbstractSet
This is an implementation of set based on a simple replacement hash table. The table consists of a fixed number of slots.
Each object added to the set is assigned a slot according to its hash code. If an attempt is made to add an object that belongs
to the same slot as another object already in the table, the new object replaces the old; the old object is no longer in the set.
This set is not thread-safe. Also, the behavior of its iterator is undefined in case when the set is modified after the
creation of iterator.
ReplacementHashSet
public ReplacementHashSet(int capacity)
size
public int size()
- Specified by:
size
in interface Set
- Specified by:
size
in class AbstractCollection
iterator
public Iterator iterator()
- Specified by:
iterator
in interface Set
- Specified by:
iterator
in class AbstractCollection
contains
public boolean contains(Object o)
- Specified by:
contains
in interface Set
- Overrides:
contains
in class AbstractCollection
remove
public boolean remove(Object o)
- Specified by:
remove
in interface Set
- Overrides:
remove
in class AbstractCollection
add
public boolean add(Object o)
- Specified by:
add
in interface Set
- Overrides:
add
in class AbstractCollection
getSlot
public int getSlot(Object o)
- calculates the slot number to assign to an object.
- Parameters:
o
- the object to assign to a slot.
- Returns:
- the slot number for the input object.
getCapacity
public int getCapacity()
getObjectAtSlot
public Object getObjectAtSlot(int slot)
- returns the object in a given slot of the table
- Returns:
- the object in a given slot of the table