Class LongHashSet

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Iterable<java.lang.Long>, java.util.Collection<java.lang.Long>, java.util.Set<java.lang.Long>

    public class LongHashSet
    extends java.util.AbstractSet<java.lang.Long>
    implements java.io.Serializable
    A hash set implementation of Set<Long> that uses open addressing values. To minimize the memory footprint, this class uses open addressing rather than chaining. Collisions are resolved using linear probing. Deletions implement compaction, so cost of remove can approach O(N) for full maps, which makes a small loadFactor recommended. The implementation is based on Agrona IntHashSet but uses long primitive keys and a different MISSING_VALUE to account for Long.hashCode() being 0 for -1.
    See Also:
    Serialized Form
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      class  LongHashSet.LongIterator
      Iterator which supports unboxed access to values.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(long value)
      Primitive specialised overload of {this#add(Long)}
      boolean add​(java.lang.Long value)
      boolean addAll​(java.util.Collection<? extends java.lang.Long> coll)
      int capacity()
      Get the total capacity for the set to which the load factor with be a fraction of.
      void clear()
      void compact()
      Compact the backing arrays by rehashing with a capacity just larger than current size and giving consideration to the load factor.
      boolean contains​(long value)
      Contains method that does not box values.
      boolean contains​(java.lang.Object value)
      boolean containsAll​(LongHashSet other)
      LongHashSet specialised variant of {this#containsAll(Collection)}.
      boolean equals​(java.lang.Object other)
      int hashCode()
      boolean isEmpty()
      LongHashSet.LongIterator iterator()
      float loadFactor()
      Get the load factor beyond which the set will increase size.
      boolean remove​(long value)
      An int specialised version of {this#remove(Object)}.
      boolean remove​(java.lang.Object value)
      boolean removeAll​(java.util.Collection<?> coll)
      int resizeThreshold()
      Get the actual threshold which when reached the map will resize.
      int size()
      java.lang.Object[] toArray()
      <E> E[] toArray​(E[] a)
      java.lang.String toString()
      • Methods inherited from class java.util.AbstractCollection

        containsAll, retainAll
      • 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

        containsAll, retainAll, spliterator
    • Field Detail

      • DEFAULT_INITIAL_CAPACITY

        public static final int DEFAULT_INITIAL_CAPACITY
        The initial capacity used when none is specified in the constructor.
        See Also:
        Constant Field Values
    • Constructor Detail

      • LongHashSet

        public LongHashSet​(int proposedCapacity)
        Construct a hash set with a proposed capacity and DEFAULT_LOAD_FACTOR.
        Parameters:
        proposedCapacity - for the initial capacity of the set.
      • LongHashSet

        public LongHashSet​(int proposedCapacity,
                           float loadFactor)
        Construct a hash set with a proposed initial capacity and load factor.
        Parameters:
        proposedCapacity - for the initial capacity of the set.
        loadFactor - to be used for resizing.
    • Method Detail

      • loadFactor

        public float loadFactor()
        Get the load factor beyond which the set will increase size.
        Returns:
        load factor for when the set should increase size.
      • capacity

        public int capacity()
        Get the total capacity for the set to which the load factor with be a fraction of.
        Returns:
        the total capacity for the set.
      • resizeThreshold

        public int resizeThreshold()
        Get the actual threshold which when reached the map will resize. This is a function of the current capacity and load factor.
        Returns:
        the threshold when the map will resize.
      • add

        public boolean add​(java.lang.Long value)
        Specified by:
        add in interface java.util.Collection<java.lang.Long>
        Specified by:
        add in interface java.util.Set<java.lang.Long>
        Overrides:
        add in class java.util.AbstractCollection<java.lang.Long>
      • add

        public boolean add​(long value)
        Primitive specialised overload of {this#add(Long)}
        Parameters:
        value - the value to add
        Returns:
        true if the collection has changed, false otherwise
        Throws:
        java.lang.IllegalArgumentException - if value is missingValue
      • remove

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

        public boolean remove​(long value)
        An int specialised version of {this#remove(Object)}.
        Parameters:
        value - the value to remove
        Returns:
        true if the value was present, false otherwise
      • compact

        public void compact()
        Compact the backing arrays by rehashing with a capacity just larger than current size and giving consideration to the load factor.
      • contains

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

        public boolean contains​(long value)
        Contains method that does not box values.
        Parameters:
        value - to be check for if the set contains it.
        Returns:
        true if the value is contained in the set otherwise false.
        See Also:
        Collection.contains(Object)
      • size

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

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

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

        public boolean addAll​(java.util.Collection<? extends java.lang.Long> coll)
        Specified by:
        addAll in interface java.util.Collection<java.lang.Long>
        Specified by:
        addAll in interface java.util.Set<java.lang.Long>
        Overrides:
        addAll in class java.util.AbstractCollection<java.lang.Long>
      • removeAll

        public boolean removeAll​(java.util.Collection<?> coll)
        Specified by:
        removeAll in interface java.util.Collection<java.lang.Long>
        Specified by:
        removeAll in interface java.util.Set<java.lang.Long>
        Overrides:
        removeAll in class java.util.AbstractSet<java.lang.Long>
      • iterator

        public LongHashSet.LongIterator iterator()
        Specified by:
        iterator in interface java.util.Collection<java.lang.Long>
        Specified by:
        iterator in interface java.lang.Iterable<java.lang.Long>
        Specified by:
        iterator in interface java.util.Set<java.lang.Long>
        Specified by:
        iterator in class java.util.AbstractCollection<java.lang.Long>
      • toString

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

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

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

        public boolean containsAll​(LongHashSet other)
        LongHashSet specialised variant of {this#containsAll(Collection)}.
        Parameters:
        other - int hash set to compare against.
        Returns:
        true if every element in other is in this.
      • equals

        public boolean equals​(java.lang.Object other)
        Specified by:
        equals in interface java.util.Collection<java.lang.Long>
        Specified by:
        equals in interface java.util.Set<java.lang.Long>
        Overrides:
        equals in class java.util.AbstractSet<java.lang.Long>
      • hashCode

        public int hashCode()
        Specified by:
        hashCode in interface java.util.Collection<java.lang.Long>
        Specified by:
        hashCode in interface java.util.Set<java.lang.Long>
        Overrides:
        hashCode in class java.util.AbstractSet<java.lang.Long>