Class ConcurrentHashSet<T>

  • Type Parameters:
    T - the type of the set objects
    All Implemented Interfaces:
    java.lang.Iterable<T>, java.util.Collection<T>, java.util.Set<T>

    @ThreadSafe
    public final class ConcurrentHashSet<T>
    extends java.util.AbstractSet<T>
    A concurrent hash set. This is backed by a ConcurrentHashMap, and Set operations are translated to ConcurrentHashMap operations.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(T element)  
      boolean addIfAbsent​(T element)
      Adds an element into the set, if and only if it is not already a part of the set.
      void clear()  
      boolean contains​(java.lang.Object o)  
      boolean containsAll​(java.util.Collection<?> c)  
      boolean isEmpty()  
      java.util.Iterator<T> iterator()  
      boolean remove​(java.lang.Object o)  
      boolean removeAll​(java.util.Collection<?> c)  
      boolean retainAll​(java.util.Collection<?> c)  
      int size()  
      java.lang.Object[] toArray()  
      <E> E[] toArray​(E[] a)  
      java.lang.String toString()  
      • Methods inherited from class java.util.AbstractSet

        equals, hashCode
      • Methods inherited from class java.util.AbstractCollection

        addAll
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        parallelStream, removeIf, stream, toArray
      • Methods inherited from interface java.lang.Iterable

        forEach
      • Methods inherited from interface java.util.Set

        addAll, spliterator
    • Constructor Detail

      • ConcurrentHashSet

        public ConcurrentHashSet​(int initialCapacity,
                                 float loadFactor,
                                 int concurrencyLevel)
        Creates a new ConcurrentHashSet.
        Parameters:
        initialCapacity - the initial capacity
        loadFactor - the load factor threshold, used to control resizing
        concurrencyLevel - the estimated number of concurrently updating threads
    • Method Detail

      • iterator

        public java.util.Iterator<T> iterator()
        Specified by:
        iterator in interface java.util.Collection<T>
        Specified by:
        iterator in interface java.lang.Iterable<T>
        Specified by:
        iterator in interface java.util.Set<T>
        Specified by:
        iterator in class java.util.AbstractCollection<T>
      • size

        public int size()
        Specified by:
        size in interface java.util.Collection<T>
        Specified by:
        size in interface java.util.Set<T>
        Specified by:
        size in class java.util.AbstractCollection<T>
      • add

        public boolean add​(T element)
        Specified by:
        add in interface java.util.Collection<T>
        Specified by:
        add in interface java.util.Set<T>
        Overrides:
        add in class java.util.AbstractCollection<T>
      • addIfAbsent

        public boolean addIfAbsent​(T element)
        Adds an element into the set, if and only if it is not already a part of the set.
        Parameters:
        element - the element to add into the set
        Returns:
        true if this set did not already contain the specified element
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Collection<T>
        Specified by:
        clear in interface java.util.Set<T>
        Overrides:
        clear in class java.util.AbstractCollection<T>
      • contains

        public boolean contains​(java.lang.Object o)
        Specified by:
        contains in interface java.util.Collection<T>
        Specified by:
        contains in interface java.util.Set<T>
        Overrides:
        contains in class java.util.AbstractCollection<T>
      • containsAll

        public boolean containsAll​(java.util.Collection<?> c)
        Specified by:
        containsAll in interface java.util.Collection<T>
        Specified by:
        containsAll in interface java.util.Set<T>
        Overrides:
        containsAll in class java.util.AbstractCollection<T>
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Collection<T>
        Specified by:
        isEmpty in interface java.util.Set<T>
        Overrides:
        isEmpty in class java.util.AbstractCollection<T>
      • remove

        public boolean remove​(java.lang.Object o)
        Specified by:
        remove in interface java.util.Collection<T>
        Specified by:
        remove in interface java.util.Set<T>
        Overrides:
        remove in class java.util.AbstractCollection<T>
      • removeAll

        public boolean removeAll​(java.util.Collection<?> c)
        Specified by:
        removeAll in interface java.util.Collection<T>
        Specified by:
        removeAll in interface java.util.Set<T>
        Overrides:
        removeAll in class java.util.AbstractSet<T>
      • retainAll

        public boolean retainAll​(java.util.Collection<?> c)
        Specified by:
        retainAll in interface java.util.Collection<T>
        Specified by:
        retainAll in interface java.util.Set<T>
        Overrides:
        retainAll in class java.util.AbstractCollection<T>
      • toArray

        public java.lang.Object[] toArray()
        Specified by:
        toArray in interface java.util.Collection<T>
        Specified by:
        toArray in interface java.util.Set<T>
        Overrides:
        toArray in class java.util.AbstractCollection<T>
      • toArray

        public <E> E[] toArray​(E[] a)
        Specified by:
        toArray in interface java.util.Collection<T>
        Specified by:
        toArray in interface java.util.Set<T>
        Overrides:
        toArray in class java.util.AbstractCollection<T>
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.util.AbstractCollection<T>