Class ZHashSet<T>
- Type Parameters:
T- the type of elements maintained by this set.
- All Implemented Interfaces:
Serializable,Cloneable,Iterable<T>,Collection<T>,Set<T>
- Direct Known Subclasses:
Ztrings
public class ZHashSet<T> extends HashSet<T>
HashSet.
The Java implementation of HashSet is unsynchronized. This class extends the implementation to provide
synchronization.
- See Also:
- Serialized Form
-
Constructor Summary
Constructors Constructor Description ZHashSet()Constructs a new, empty set; the backing HashSet instance has default initial capacity (16) and load factor (0.75). -
Method Summary
Modifier and Type Method Description booleanadd(T e)Synchronized implementation ofHashSet.add(java.lang.Object).booleanaddAll(Collection c)Synchronized implementation ofAbstractCollection.addAll(java.util.Collection).booleanaddTo(Collection<T> col)Adds the elements of this set to the given collection.booleananyMatch(Predicate<? super T> predicate)Returns whether any elements of this set match the provided predicate.voidclear()Synchronized implementation ofHashSet.clear().booleancontains(Object o)Synchronized implementation ofHashSet.contains(java.lang.Object).booleancontainsAll(Collection c)Synchronized implementation ofAbstractCollection.containsAll(java.util.Collection).voidforEach(Consumer<? super T> action)Synchronized implementation ofIterable.forEach(java.util.function.Consumer).static Collectionintersection(Collection... cols)Returns an intersection of all given collections.Collection<T>intersection(Collection<T> c)Returns a new collection containing an intersection between this set and another collection without affecting this set.booleanisEmpty()Synchronized implementation ofHashSet.isEmpty().voidlockRead()Locks the set for reading.voidlockWrite()Locks the set for writing.booleanremove(Object o)Synchronized implementation ofHashSet.remove(java.lang.Object).booleanremoveAll(Collection c)Synchronized implementation ofAbstractSet.removeAll(java.util.Collection).booleanremoveIf(Predicate<? super T> filter)Synchronized implementation ofCollection.removeIf(java.util.function.Predicate).booleanretainAll(Collection c)Synchronized implementation ofAbstractCollection.retainAll(java.util.Collection).intsize()Synchronized implementation ofHashSet.size().T[]toArray(Class<T> cl)Converts this set to a simple array.ZHashSet<T>union(Collection<T> c)Returns a new set that contains the union of this set and the given collection.voidunlockRead()Unlocks the set after reading.voidunlockWrite()Unlocks the set after writing.a
-
Constructor Details
-
ZHashSet
public ZHashSet()Constructs a new, empty set; the backing HashSet instance has default initial capacity (16) and load factor (0.75).
-
-
Method Details
-
lockRead
public final void lockRead()Locks the set for reading.This method is used by the class internally to provide synchronization, but you can use it directly when you need to perform a synchronized bulk read operation on the set.
-
lockWrite
public final void lockWrite()Locks the set for writing.This method is used by the class internally to provide synchronization, but you can use it directly when you need to perform a synchronized bulk write operation on the set.
-
unlockRead
public final void unlockRead()Unlocks the set after reading.This method is used by the class internally, but you should use it directly if you use
lockRead()directly. -
unlockWrite
public final void unlockWrite()Unlocks the set after writing.aThis method is used by the class internally, but you should use it directly if you use
lockWrite()directly. -
add
Synchronized implementation ofHashSet.add(java.lang.Object). -
addAll
Synchronized implementation ofAbstractCollection.addAll(java.util.Collection).- Specified by:
addAllin interfaceCollection<T>- Specified by:
addAllin interfaceSet<T>- Overrides:
addAllin classAbstractCollection<T>- Parameters:
c- collection containing elements to be added to this set.- Returns:
- true if this set changed as a result of the call.
-
remove
Synchronized implementation ofHashSet.remove(java.lang.Object). -
removeAll
Synchronized implementation ofAbstractSet.removeAll(java.util.Collection).- Specified by:
removeAllin interfaceCollection<T>- Specified by:
removeAllin interfaceSet<T>- Overrides:
removeAllin classAbstractSet<T>- Parameters:
c- collection containing elements to be removed from this set.- Returns:
- true if this set changed as a result of the call.
-
removeIf
Synchronized implementation ofCollection.removeIf(java.util.function.Predicate).- Parameters:
filter- a predicate which returnstruefor elements to be removed- Returns:
trueif any elements were removed
-
retainAll
Synchronized implementation ofAbstractCollection.retainAll(java.util.Collection).- Specified by:
retainAllin interfaceCollection<T>- Specified by:
retainAllin interfaceSet<T>- Overrides:
retainAllin classAbstractCollection<T>- Parameters:
c- collection containing elements to be retained in this set.- Returns:
- true if this set changed as a result of the call.
-
contains
Synchronized implementation ofHashSet.contains(java.lang.Object). -
containsAll
Synchronized implementation ofAbstractCollection.containsAll(java.util.Collection).- Specified by:
containsAllin interfaceCollection<T>- Specified by:
containsAllin interfaceSet<T>- Overrides:
containsAllin classAbstractCollection<T>- Parameters:
c- collection to be checked for containment in this set.- Returns:
- true if this set contains all of the elements in the specified collection.
-
forEach
Synchronized implementation ofIterable.forEach(java.util.function.Consumer).- Parameters:
action- The action to be performed for each element
-
isEmpty
public boolean isEmpty()Synchronized implementation ofHashSet.isEmpty(). -
size
public int size()Synchronized implementation ofHashSet.size(). -
clear
public void clear()Synchronized implementation ofHashSet.clear(). -
intersection
Returns a new collection containing an intersection between this set and another collection without affecting this set.The type of the returned collection is the type of this set.
- Parameters:
c- the collection to intersect with this set.- Returns:
- a new collection in the type of this set containing an intersection between this set and another collection without affecting this set.
-
intersection
Returns an intersection of all given collections.This static method gets as arguments a variable length list of collections. If the list is empty, the method returns
null, otherwise it returns the intersection of all the collections. The type of the returned collection is the type of the first argument.- Parameters:
cols- the list of collections to operate on.- Returns:
- a collection containing the intersection of all given collections, in the type of the first collection, or null if there are no arguments.
-
union
Returns a new set that contains the union of this set and the given collection.The method instantiates a new set in the type of this set and fills it with the union of this set and the given collection. The current set is not affected.
- Parameters:
c- the collection to unite with this set.- Returns:
- a new set in the type of this set that contains the union of this set and the given collection.
-
addTo
Adds the elements of this set to the given collection.Synchronizes the access to this set and adds its elements to the given collection.
- Parameters:
col- the collection to add to- Returns:
trueif the target collection changed as a result of the call
-
toArray
Converts this set to a simple array.Synchronizes the access to this set and converts it to a simple array.
- Parameters:
cl- theClassobject representing the type of the set elements.- Returns:
- an array containing all of the elements in this set
-
anyMatch
Returns whether any elements of this set match the provided predicate.Returns whether any elements of this set match the provided predicate. The method synchronizes the set for reading before searching for a match.
- Parameters:
predicate- a non-interfering, stateless predicate to apply to elements of this set- Returns:
trueif any elements of the set match the provided predicate, otherwisefalse
-