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>
A synchronized implementation of HashSet.

The Java implementation of HashSet is unsynchronized. This class extends the implementation to provide synchronization.

See Also:
Serialized Form
  • 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.a

      This method is used by the class internally, but you should use it directly if you use lockWrite() directly.

    • add

      public boolean add​(T e)
      Synchronized implementation of HashSet.add(java.lang.Object).
      Specified by:
      add in interface Collection<T>
      Specified by:
      add in interface Set<T>
      Overrides:
      add in class HashSet<T>
      Parameters:
      e - element to be added to this set.
      Returns:
      true if this set did not already contain the specified element.
    • addAll

      public boolean addAll​(Collection c)
      Synchronized implementation of AbstractCollection.addAll(java.util.Collection).
      Specified by:
      addAll in interface Collection<T>
      Specified by:
      addAll in interface Set<T>
      Overrides:
      addAll in class AbstractCollection<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

      public boolean remove​(Object o)
      Synchronized implementation of HashSet.remove(java.lang.Object).
      Specified by:
      remove in interface Collection<T>
      Specified by:
      remove in interface Set<T>
      Overrides:
      remove in class HashSet<T>
      Parameters:
      o - object to be removed from this set, if present.
      Returns:
      true if the set contained the specified element.
    • removeAll

      public boolean removeAll​(Collection c)
      Synchronized implementation of AbstractSet.removeAll(java.util.Collection).
      Specified by:
      removeAll in interface Collection<T>
      Specified by:
      removeAll in interface Set<T>
      Overrides:
      removeAll in class AbstractSet<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

      public boolean removeIf​(Predicate<? super T> filter)
      Parameters:
      filter - a predicate which returns true for elements to be removed
      Returns:
      true if any elements were removed
    • retainAll

      public boolean retainAll​(Collection c)
      Specified by:
      retainAll in interface Collection<T>
      Specified by:
      retainAll in interface Set<T>
      Overrides:
      retainAll in class AbstractCollection<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

      public boolean contains​(Object o)
      Synchronized implementation of HashSet.contains(java.lang.Object).
      Specified by:
      contains in interface Collection<T>
      Specified by:
      contains in interface Set<T>
      Overrides:
      contains in class HashSet<T>
      Parameters:
      o - element whose presence in this set is to be tested.
      Returns:
      true if this set contains the specified element.
    • containsAll

      public boolean containsAll​(Collection c)
      Specified by:
      containsAll in interface Collection<T>
      Specified by:
      containsAll in interface Set<T>
      Overrides:
      containsAll in class AbstractCollection<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

      public void forEach​(Consumer<? super T> action)
      Synchronized implementation of Iterable.forEach(java.util.function.Consumer).
      Parameters:
      action - The action to be performed for each element
    • isEmpty

      public boolean isEmpty()
      Synchronized implementation of HashSet.isEmpty().
      Specified by:
      isEmpty in interface Collection<T>
      Specified by:
      isEmpty in interface Set<T>
      Overrides:
      isEmpty in class HashSet<T>
      Returns:
      true if this set contains no elements.
    • size

      public int size()
      Synchronized implementation of HashSet.size().
      Specified by:
      size in interface Collection<T>
      Specified by:
      size in interface Set<T>
      Overrides:
      size in class HashSet<T>
      Returns:
      the number of elements in this set.
    • clear

      public void clear()
      Synchronized implementation of HashSet.clear().
      Specified by:
      clear in interface Collection<T>
      Specified by:
      clear in interface Set<T>
      Overrides:
      clear in class HashSet<T>
    • intersection

      public Collection<T> intersection​(Collection<T> c)
      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

      public static Collection intersection​(Collection... cols)
      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

      public ZHashSet<T> union​(Collection<T> c)
      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

      public boolean addTo​(Collection<T> col)
      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:
      true if the target collection changed as a result of the call
    • toArray

      public T[] toArray​(Class<T> cl)
      Converts this set to a simple array.

      Synchronizes the access to this set and converts it to a simple array.

      Parameters:
      cl - the Class object representing the type of the set elements.
      Returns:
      an array containing all of the elements in this set
    • anyMatch

      public boolean anyMatch​(Predicate<? super T> predicate)
      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:
      true if any elements of the set match the provided predicate, otherwise false