| JGL - The Generic Collection Library for Java |
All Packages Class Hierarchy This Package Previous Next Index
Class com.objectspace.jgl.HashSet
java.lang.Object
|
+----com.objectspace.jgl.HashSet
- public class HashSet
- extends Object
- implements Set
A HashSet is a container that is optimized for fast associative lookup.
Items are matched by default using a BinaryPredicate that uses equals()
for comparisons.
When an item is inserted into a HashSet, it is stored in a data structure
that allows the item to be found very quickly. Items are stored in buckets
based on their hash value, computed using the standard function hashCode().
By default, a HashSet cannot contain items that match.
The HashSet class supports the full range of generic set algorithms such as
union() and intersection() in a user-friendly manner.
HashSets are useful when fast associate lookup is important, when
index-based lookup is unnecessary, and when duplicates are not allowed.
Insertion can invalidate iterators.
Removal can invalidate iterators.
- See Also:
- Set, BinaryPredicate, SetOperations, HashSet examples
-
HashSet()
- Construct myself to be an empty HashSet that compares objects using equals() and
does not allow duplicates.
-
HashSet(BinaryPredicate)
- Construct myself to be an empty HashSet that compares objects using the specified
binary predicate and does not allow duplicates.
-
HashSet(BinaryPredicate, boolean)
- Construct myself to be an empty HashSet that compares objects using the specified
binary predicate and conditionally allows duplicates.
-
HashSet(BinaryPredicate, boolean, int, float)
- Construct myself to be an empty HashSet that compares objects using the specified
binary predicate and conditionally allows duplicates.
-
HashSet(BinaryPredicate, int, float)
- Construct myself to be an empty HashSet that compares objects using the specified
binary predicate and conditionally allows duplicates.
-
HashSet(boolean)
- Construct myself to be an empty HashSet that compares objects using equals() and
that conditionally allows duplicates.
-
HashSet(HashSet)
- Construct myself to be a shallow copy of an existing HashSet.
-
add(Object)
- If the object doesn't exist or duplicates are allowed, add the object and return null,
otherwise don't modify the set and return the matching object.
-
allowExpansion(boolean)
- Enable or disable the current expansion mode.
-
allowsDuplicates()
- Return true if I allow duplicate objects.
-
begin()
- Return an iterator positioned at my first item.
-
clear()
- Remove all of my elements.
-
clone()
- Return a shallow copy of myself.
-
copy(HashSet)
- Become a shallow copy of an existing HashSet.
-
count(Object)
- Return the number of items that match a particular object.
-
difference(HashSet)
- Return a new HashSet that contains the elements that are in me but not in a
specified set.
-
elements()
- Return an Enumeration of my objects.
-
end()
- Return an iterator positioned immediately after my last item.
-
equalRange(Object)
- Return a range whose first element is an iterator positioned
at the first occurence of a specific object and whose second element is an
iterator positioned immediately after the last occurence of that object.
-
equals(HashSet)
- Return true if I contain exactly the same items as another HashSet.
-
equals(Object)
- Return true if I'm equal to another object.
-
expansionAllowed()
- Return true if adding an object to myself could result in an expansion
of the number of hash buckets I currently use.
-
find(Object)
- Find an object and return its position.
-
finish()
- Return an iterator positioned immediately afer my last item.
-
get(Object)
- Return the first object that matches the given object, or null if no match exists.
-
getComparator()
- Return my comparator.
-
getLoadRatio()
- Return my load ratio.
-
hashCode()
- Return my hash code for support of hashing containers
-
intersection(HashSet)
- Return a new HashSet that contains the elements that are both in me and in
a specified set.
-
isEmpty()
- Return true if I contain no entries.
-
lowerBound(Object)
- Return an iterator positioned at the first location that a
particular object could be inserted without violating the ordering
criteria.
-
maxSize()
- Return the maximum number of entries that I can contain.
-
nextBucketSize(int)
- Return the number of new buckets to create when expanding the
collection.
-
properSubsetOf(HashSet)
- Return true if every element in me is also in a specified HashSet and I'm smaller
than the specified HashSet.
-
put(Object)
- If the object doesn't exist, add the object and return null, otherwise replace the
first object that matches and return the old object.
-
remove(Enumeration)
- Remove the element at a particular position.
-
remove(Enumeration, Enumeration)
- Remove the elements within a specified range.
-
remove(Object)
- Remove all objects that match the given object.
-
remove(Object, int)
- Remove at most a given number of objects that match the given object.
-
size()
- Return the number of entries that I contain.
-
start()
- Return an iterator positioned at my first item.
-
subsetOf(HashSet)
- Return true if every element in me is also in a specified HashSet.
-
swap(HashSet)
- Swap my contents with another HashSet.
-
symmetricDifference(HashSet)
- Return a new HashSet that contains the elements that are either in me or in
a specified HashSet, but not both.
-
toString()
- Return a string that describes me.
-
union(HashSet)
- Return a new HashSet that contains all of my elements and all of the elements in
a specified HashSet.
-
upperBound(Object)
- Return an iterator positioned at the last location that
a particular object could be inserted without violating the ordering
criteria.
HashSet
public HashSet()
- Construct myself to be an empty HashSet that compares objects using equals() and
does not allow duplicates.
HashSet
public HashSet(boolean allowDuplicates)
- Construct myself to be an empty HashSet that compares objects using equals() and
that conditionally allows duplicates.
- Parameters:
- allowDuplicates - true if duplicates are allowed.
HashSet
public HashSet(BinaryPredicate comparator)
- Construct myself to be an empty HashSet that compares objects using the specified
binary predicate and does not allow duplicates.
- Parameters:
- comparator - The predicate for comparing objects.
HashSet
public HashSet(BinaryPredicate comparator,
boolean allowDuplicates)
- Construct myself to be an empty HashSet that compares objects using the specified
binary predicate and conditionally allows duplicates.
- Parameters:
- comparator - The predicate for comparing objects.
- allowDuplicates - true if duplicates are allowed.
HashSet
public HashSet(BinaryPredicate comparator,
int capacity,
float loadRatio)
- Construct myself to be an empty HashSet that compares objects using the specified
binary predicate and conditionally allows duplicates. The initial capacity and
load ratio must also be specified.
- Parameters:
- comparator - The predicate for comparing objects.
- capacity - The initial number of hash buckets to reserve.
- loadRatio - The maximum load ratio.
HashSet
public HashSet(BinaryPredicate comparator,
boolean allowDuplicates,
int capacity,
float loadRatio)
- Construct myself to be an empty HashSet that compares objects using the specified
binary predicate and conditionally allows duplicates. The initial capacity and
load ratio must also be specified.
- Parameters:
- comparator - The predicate for comparing objects.
- allowDuplicates - true if duplicates are allowed.
- capacity - The initial number of hash buckets to reserve.
- loadRatio - The maximum load ratio.
HashSet
public HashSet(HashSet set)
- Construct myself to be a shallow copy of an existing HashSet.
- Parameters:
- set - The HashSet to copy.
allowsDuplicates
public boolean allowsDuplicates()
- Return true if I allow duplicate objects.
getComparator
public BinaryPredicate getComparator()
- Return my comparator.
getLoadRatio
public float getLoadRatio()
- Return my load ratio.
clone
public synchronized Object clone()
- Return a shallow copy of myself.
- Overrides:
- clone in class Object
copy
public synchronized void copy(HashSet set)
- Become a shallow copy of an existing HashSet.
- Parameters:
- set - The HashSet that I shall become a shallow copy of.
toString
public synchronized String toString()
- Return a string that describes me.
- Overrides:
- toString in class Object
elements
public synchronized Enumeration elements()
- Return an Enumeration of my objects.
start
public ForwardIterator start()
- Return an iterator positioned at my first item.
finish
public ForwardIterator finish()
- Return an iterator positioned immediately afer my last item.
begin
public synchronized HashSetIterator begin()
- Return an iterator positioned at my first item.
end
public synchronized HashSetIterator end()
- Return an iterator positioned immediately after my last item.
isEmpty
public boolean isEmpty()
- Return true if I contain no entries.
size
public int size()
- Return the number of entries that I contain.
maxSize
public int maxSize()
- Return the maximum number of entries that I can contain.
equals
public boolean equals(Object object)
- Return true if I'm equal to another object.
- Parameters:
- object - The object to compare myself against.
- Overrides:
- equals in class Object
equals
public synchronized boolean equals(HashSet set)
- Return true if I contain exactly the same items as another HashSet.
Use equals() to compare the individual elements.
- Parameters:
- set - The HashSet to compare myself against.
hashCode
public synchronized int hashCode()
- Return my hash code for support of hashing containers
- Overrides:
- hashCode in class Object
swap
public synchronized void swap(HashSet set)
- Swap my contents with another HashSet.
- Parameters:
- set - The HashSet that I will swap my contents with.
clear
public synchronized void clear()
- Remove all of my elements.
remove
public int remove(Object object)
- Remove all objects that match the given object.
- Parameters:
- object - The object to match for removals
- Returns:
- Return the number of values removed.
remove
public int remove(Object key,
int count)
- Remove at most a given number of objects that match the given object.
- Parameters:
- object - The object to match for removals
- count - The maximum number of the pair(s) to remove.
- Returns:
- Return the number of values removed.
remove
public synchronized Object remove(Enumeration e)
- Remove the element at a particular position.
- Parameters:
- e - An Enumeration positioned at the element to remove.
- Returns:
- Return the pair associated with the enumeration or null if none.
- Throws: IllegalArgumentException
- is the Enumeration isn't a
HashSetIterator for this HashSet object.
remove
public synchronized int remove(Enumeration first,
Enumeration last)
- Remove the elements within a specified range.
- Parameters:
- first - An Enumeration positioned at the first element to remove.
- last - An Enumeration positioned immediately after the last element to remove.
- Returns:
- Return the number of values removed.
- Throws: IllegalArgumentException
- is the Enumeration isn't a
HashSetIterator for this HashSet object.
find
public synchronized HashSetIterator find(Object object)
- Find an object and return its position. If the object
is not found, return end().
- Parameters:
- object - The object to locate.
count
public synchronized int count(Object object)
- Return the number of items that match a particular object.
- Parameters:
- object - The object to match against.
add
public synchronized Object add(Object object)
- If the object doesn't exist or duplicates are allowed, add the object and return null,
otherwise don't modify the set and return the matching object.
- Parameters:
- object - The object to be added.
- Throws: NullPointerException
- If the value of the object is equal to null.
get
public synchronized Object get(Object object)
- Return the first object that matches the given object, or null if no match exists.
- Parameters:
- object - The object to match against.
put
public synchronized Object put(Object object)
- If the object doesn't exist, add the object and return null, otherwise replace the
first object that matches and return the old object.
- Parameters:
- object - The object to add.
- Throws: NullPointerException
- If the value of the object is equal to null.
union
public synchronized HashSet union(HashSet set)
- Return a new HashSet that contains all of my elements and all of the elements in
a specified HashSet.
- Parameters:
- set - The HashSet to union myself with.
intersection
public synchronized HashSet intersection(HashSet set)
- Return a new HashSet that contains the elements that are both in me and in
a specified set.
- Parameters:
- set - The HashSet to intersect myself with.
difference
public synchronized HashSet difference(HashSet set)
- Return a new HashSet that contains the elements that are in me but not in a
specified set.
- Parameters:
- set - The HashSet to difference myself with.
symmetricDifference
public synchronized HashSet symmetricDifference(HashSet set)
- Return a new HashSet that contains the elements that are either in me or in
a specified HashSet, but not both.
- Parameters:
- set - The HashSet to symmetric difference myself with.
subsetOf
public synchronized boolean subsetOf(HashSet set)
- Return true if every element in me is also in a specified HashSet.
- Parameters:
- set - The HashSet to test against.
properSubsetOf
public synchronized boolean properSubsetOf(HashSet set)
- Return true if every element in me is also in a specified HashSet and I'm smaller
than the specified HashSet.
- Parameters:
- set - The HashSet to test against.
lowerBound
public synchronized HashSetIterator lowerBound(Object object)
- Return an iterator positioned at the first location that a
particular object could be inserted without violating the ordering
criteria. If no such location is found, return an iterator
positioned at end().
- Parameters:
- object - The object in question.
upperBound
public synchronized HashSetIterator upperBound(Object object)
- Return an iterator positioned at the last location that
a particular object could be inserted without violating the ordering
criteria. If no such location is found, return an iterator
positioned at end().
- Parameters:
- object - The object in question.
equalRange
public synchronized Range equalRange(Object object)
- Return a range whose first element is an iterator positioned
at the first occurence of a specific object and whose second element is an
iterator positioned immediately after the last occurence of that object.
Note that all objects inbetween these iterators will also match the specified
object. If no matching object is found, both ends of the range will be
the same.
- Parameters:
- object - The object whose bounds are to be found.
expansionAllowed
public boolean expansionAllowed()
- Return true if adding an object to myself could result in an expansion
of the number of hash buckets I currently use.
allowExpansion
public synchronized void allowExpansion(boolean allow)
- Enable or disable the current expansion mode. If disabled, no new
hash buckets will ever be created regardless of my size.
- Parameters:
- allow - The new expansion mode.
nextBucketSize
protected int nextBucketSize(int length)
- Return the number of new buckets to create when expanding the
collection. The number returned must be positive, and should be
greater than the current bucket size. It is advisable to have
the new size be prime (or have few divisors less than 20) for
the best hash distribution.
- Parameters:
- length - The current number of buckets.
- Returns:
- length * 2 + 1
All Packages Class Hierarchy This Package Previous Next Index