Package net.ontopia.utils
Class CompactHashSet<E>
- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractSet<E>
-
- net.ontopia.utils.CompactHashSet<E>
-
- All Implemented Interfaces:
Iterable<E>
,Collection<E>
,Set<E>
- Direct Known Subclasses:
CompactIdentityHashSet
,SynchronizedCompactHashSet
,TreeWidget.UniversalSet
,UniqueSet
public class CompactHashSet<E> extends AbstractSet<E>
INTERNAL: Implements the Set interface more compactly than java.util.HashSet by using a closed hashtable.
-
-
Field Summary
Fields Modifier and Type Field Description protected static Object
deletedObject
When an object is deleted this object is put into the hashtable in its place, so that other objects with the same key (collisions) further down the hashtable are not lost after we delete an object in the collision chain.protected int
elements
protected int
freecells
This is the number of empty (null) cells.protected static int
INITIAL_SIZE
protected static double
LOAD_FACTOR
protected int
modCount
protected static Object
nullObject
This object is used to represent null, should clients add that to the set.protected E[]
objects
-
Constructor Summary
Constructors Constructor Description CompactHashSet()
Constructs a new, empty set.CompactHashSet(int size)
Constructs a new, empty set.CompactHashSet(Collection<E> c)
Constructs a new set containing the elements in the specified collection.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
add(Object 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(Object o)
Returns true if this set contains the specified element.void
dump()
INTERNAL: Used for debugging only.boolean
isEmpty()
Returns true if this set contains no elements.Iterator<E>
iterator()
Returns an iterator over the elements in this set.protected void
rehash()
INTERNAL: Figures out correct size for rehashed set, then does the rehash.protected void
rehash(int newCapacity)
INTERNAL: Rehashes the hashset to a bigger size.boolean
remove(Object o)
Removes the specified element from the set.int
size()
Returns the number of elements in this set (its cardinality).Object[]
toArray()
<T> T[]
toArray(T[] a)
-
Methods inherited from class java.util.AbstractSet
equals, hashCode, removeAll
-
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, retainAll, toString
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
-
Methods inherited from interface java.util.Set
addAll, containsAll, retainAll, spliterator
-
-
-
-
Field Detail
-
INITIAL_SIZE
protected static final int INITIAL_SIZE
- See Also:
- Constant Field Values
-
LOAD_FACTOR
protected static final double LOAD_FACTOR
- See Also:
- Constant Field Values
-
nullObject
protected static final Object nullObject
This object is used to represent null, should clients add that to the set.
-
deletedObject
protected static final Object deletedObject
When an object is deleted this object is put into the hashtable in its place, so that other objects with the same key (collisions) further down the hashtable are not lost after we delete an object in the collision chain.
-
elements
protected int elements
-
freecells
protected int freecells
This is the number of empty (null) cells. It's not necessarily the same as objects.length - elements, because some cells may contain deletedObject.
-
objects
protected E[] objects
-
modCount
protected int modCount
-
-
Constructor Detail
-
CompactHashSet
public CompactHashSet()
Constructs a new, empty set.
-
CompactHashSet
public CompactHashSet(int size)
Constructs a new, empty set.
-
CompactHashSet
public CompactHashSet(Collection<E> c)
Constructs a new set containing the elements in the specified collection.- Parameters:
c
- the collection whose elements are to be placed into this set.
-
-
Method Detail
-
iterator
public Iterator<E> iterator()
Returns an iterator over the elements in this set. The elements are returned in no particular order.- Specified by:
iterator
in interfaceCollection<E>
- Specified by:
iterator
in interfaceIterable<E>
- Specified by:
iterator
in interfaceSet<E>
- Specified by:
iterator
in classAbstractCollection<E>
- Returns:
- an Iterator over the elements in this set.
- See Also:
ConcurrentModificationException
-
size
public int size()
Returns the number of elements in this set (its cardinality).- Specified by:
size
in interfaceCollection<E>
- Specified by:
size
in interfaceSet<E>
- Specified by:
size
in classAbstractCollection<E>
-
isEmpty
public boolean isEmpty()
Returns true if this set contains no elements.- Specified by:
isEmpty
in interfaceCollection<E>
- Specified by:
isEmpty
in interfaceSet<E>
- Overrides:
isEmpty
in classAbstractCollection<E>
-
contains
public boolean contains(Object o)
Returns true if this set contains the specified element.- Specified by:
contains
in interfaceCollection<E>
- Specified by:
contains
in interfaceSet<E>
- Overrides:
contains
in classAbstractCollection<E>
- Parameters:
o
- element whose presence in this set is to be tested.- Returns:
- true if this set contains the specified element.
-
add
public boolean add(Object o)
Adds the specified element to this set if it is not already present.- Specified by:
add
in interfaceCollection<E>
- Specified by:
add
in interfaceSet<E>
- Overrides:
add
in classAbstractCollection<E>
- Parameters:
o
- element to be added to this set.- Returns:
- true if the set did not already contain the specified element.
-
remove
public boolean remove(Object o)
Removes the specified element from the set.- Specified by:
remove
in interfaceCollection<E>
- Specified by:
remove
in interfaceSet<E>
- Overrides:
remove
in classAbstractCollection<E>
-
clear
public void clear()
Removes all of the elements from this set.- Specified by:
clear
in interfaceCollection<E>
- Specified by:
clear
in interfaceSet<E>
- Overrides:
clear
in classAbstractCollection<E>
-
toArray
public Object[] toArray()
- Specified by:
toArray
in interfaceCollection<E>
- Specified by:
toArray
in interfaceSet<E>
- Overrides:
toArray
in classAbstractCollection<E>
-
toArray
public <T> T[] toArray(T[] a)
- Specified by:
toArray
in interfaceCollection<E>
- Specified by:
toArray
in interfaceSet<E>
- Overrides:
toArray
in classAbstractCollection<E>
-
dump
public void dump()
INTERNAL: Used for debugging only.
-
rehash
protected void rehash()
INTERNAL: Figures out correct size for rehashed set, then does the rehash.
-
rehash
protected void rehash(int newCapacity)
INTERNAL: Rehashes the hashset to a bigger size.
-
-