Class ZHashSet<T>

Type Parameters:
T - the type of elements maintained by this set.
All Implemented Interfaces:
Serializable, Cloneable, Iterable, Collection, Set
Direct Known Subclasses:
Ztrings

public class ZHashSet<T>
extends HashSet
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.

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

    • add

      public boolean add​(Object 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
      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
      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
      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
      Parameters:
      c - collection containing elements to be removed from this set.
      Returns:
      true if this set changed as a result of the call.
    • 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
      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
      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
      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.
    • 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
      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
      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
    • 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.