Class ConcurrentHashSet<E>

java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractSet<E>
host.anzo.commons.collection.ConcurrentHashSet<E>
Type Parameters:
E - the type of elements in this set
All Implemented Interfaces:
Iterable<E>, Collection<E>, Set<E>

public class ConcurrentHashSet<E> extends AbstractSet<E> implements Set<E>
A thread-safe implementation of a Set using a ConcurrentHashMap. This class allows for concurrent access and modification of the set without the need for external synchronization.
Since:
9/20/2021
  • Constructor Details

    • ConcurrentHashSet

      public ConcurrentHashSet()
      Constructs a new, empty ConcurrentHashSet.
  • Method Details

    • size

      public int size()
      Returns the number of elements in this set.
      Specified by:
      size in interface Collection<E>
      Specified by:
      size in interface Set<E>
      Specified by:
      size in class AbstractCollection<E>
      Returns:
      the number of elements in this set
    • iterator

      @NotNull public @NotNull Iterator<E> iterator()
      Returns an iterator over the elements in this set.
      Specified by:
      iterator in interface Collection<E>
      Specified by:
      iterator in interface Iterable<E>
      Specified by:
      iterator in interface Set<E>
      Specified by:
      iterator in class AbstractCollection<E>
      Returns:
      an iterator over the elements in this set
    • isEmpty

      public boolean isEmpty()
      Returns true if this set contains no elements.
      Specified by:
      isEmpty in interface Collection<E>
      Specified by:
      isEmpty in interface Set<E>
      Overrides:
      isEmpty in class AbstractCollection<E>
      Returns:
      true if this set is empty, false otherwise
    • add

      public boolean add(E o)
      Adds the specified element to this set if it is not already present.
      Specified by:
      add in interface Collection<E>
      Specified by:
      add in interface Set<E>
      Overrides:
      add in class AbstractCollection<E>
      Parameters:
      o - element to be added to this set
      Returns:
      true if this set did not already contain the specified element
    • contains

      public boolean contains(Object o)
      Returns true if this set contains the specified element.
      Specified by:
      contains in interface Collection<E>
      Specified by:
      contains in interface Set<E>
      Overrides:
      contains in class AbstractCollection<E>
      Parameters:
      o - element whose presence in this set is to be tested
      Returns:
      true if this set contains the specified element, false otherwise
    • clear

      public void clear()
      Removes all the elements from this set.
      Specified by:
      clear in interface Collection<E>
      Specified by:
      clear in interface Set<E>
      Overrides:
      clear in class AbstractCollection<E>
    • remove

      public boolean remove(Object o)
      Removes the specified element from this set if it is present.
      Specified by:
      remove in interface Collection<E>
      Specified by:
      remove in interface Set<E>
      Overrides:
      remove in class AbstractCollection<E>
      Parameters:
      o - element to be removed from this set, if present
      Returns:
      true if this set contained the specified element
    • addIfAbsent

      public boolean addIfAbsent(E o)
      Adds the specified element to this set if it is not already present. This method is equivalent to add(E) method, but may be more efficient in a concurrent context.
      Parameters:
      o - element to be added to this set
      Returns:
      true if this set did not already contain the specified element