Class FastHashSet<K>

java.lang.Object
org.apache.jena.mem.collection.FastHashBase<K>
org.apache.jena.mem.collection.FastHashSet<K>
Type Parameters:
K - the element type
All Implemented Interfaces:
Iterable<K>, JenaMapSetCommon<K>, JenaSet<K>, JenaSetHashOptimized<K>, JenaSetIndexed<K>, Sized
Direct Known Subclasses:
FastHashedTripleBunch, TripleSet, TripleSet

public abstract class FastHashSet<K> extends FastHashBase<K> implements JenaSetIndexed<K>
Hash set specialization built on top of FastHashBase. Grows on demand but never shrinks, does not guarantee iteration order, does not allow null elements, and is not thread-safe.

Optimized for fast add / containsKey / stream / iterate operations. Removal is somewhat slower than in HashSet because of the back-shifting performed on the probe table. Iteration speed does not recover after many removals because the dense keys array is not compacted.

  • Constructor Details

    • FastHashSet

      public FastHashSet(int initialSize)
      Creates a set with the given initial key-array capacity.
      Parameters:
      initialSize - the initial capacity of the keys array
    • FastHashSet

      public FastHashSet()
      Creates a set with the default initial capacity.
  • Method Details

    • tryAdd

      public boolean tryAdd(K key)
      Description copied from interface: JenaSet
      Add the key to the set if it is not already present.
      Specified by:
      tryAdd in interface JenaSet<K>
      Parameters:
      key - the key to add. (null is not allowed)
      Returns:
      true if the key was added, false if it was already present
    • tryAdd

      public boolean tryAdd(K key, int hashCode)
      Description copied from interface: JenaSetHashOptimized
      Add an element with the given precomputed hash code if it is not already present.
      Specified by:
      tryAdd in interface JenaSetHashOptimized<K>
      Parameters:
      key - the element to add. (null is not allowed)
      hashCode - key.hashCode().
      Returns:
      true if added, false if already present
    • addAndGetIndex

      public int addAndGetIndex(K key)
      Add an element and return the index it was stored at. If the element is already present, returns the bitwise complement (~existingIndex) of the existing index, so callers can distinguish "newly inserted" from "already present" while still recovering the index in both cases.
      Specified by:
      addAndGetIndex in interface JenaSetIndexed<K>
      Parameters:
      key - the element to add
      Returns:
      the new index, or ~existingIndex if already present
    • addUnchecked

      public void addUnchecked(K key)
      Description copied from interface: JenaSet
      Add the key to the set without checking whether it is already present.

      Attention: this method must only be used if the caller has ensured that the key is not already in the set; otherwise the set's invariants will break (duplicates may be inserted).

      Specified by:
      addUnchecked in interface JenaSet<K>
      Parameters:
      key - the key to add. (null is not allowed)
    • addUnchecked

      public void addUnchecked(K key, int hashCode)
      Description copied from interface: JenaSetHashOptimized
      Add an element with the given precomputed hash code without checking whether it is already present. The caller MUST ensure the key is absent.
      Specified by:
      addUnchecked in interface JenaSetHashOptimized<K>
      Parameters:
      key - the element to add. (null is not allowed)
      hashCode - key.hashCode(). (null is not allowed)
    • forEach

      public void forEach(Consumer<? super K> action)
      Specified by:
      forEach in interface Iterable<K>
      Specified by:
      forEach in interface JenaSet<K>