Class ObjectLinkedOpenHashSet<K>

All Implemented Interfaces:
Hash, ObjectBidirectionalIterable<K>, ObjectCollection<K>, ObjectIterable<K>, ObjectSet<K>, ObjectSortedSet<K>, Serializable, Cloneable, Iterable<K>, Collection<K>, SequencedCollection<K>, SequencedSet<K>, Set<K>, SortedSet<K>

public class ObjectLinkedOpenHashSet<K> extends AbstractObjectSortedSet<K> implements Serializable, Cloneable, Hash
A type-specific linked hash set with with a fast, small-footprint implementation.

Instances of this class use a hash table to represent a set. The table is filled up to a specified load factor, and then doubled in size to accommodate new entries. If the table is emptied below one fourth of the load factor, it is halved in size; however, the table is never reduced to a size smaller than that at creation time: this approach makes it possible to create sets with a large capacity in which insertions and deletions do not cause immediately rehashing. Moreover, halving is not performed when deleting entries from an iterator, as it would interfere with the iteration process.

Note that clear() does not modify the hash table size. Rather, a family of trimming methods lets you control the size of the table; this is particularly useful if you reuse instances of this class.

Iterators generated by this set will enumerate elements in the same order in which they have been added to the set (addition of elements already present in the set does not change the iteration order). Note that this order has nothing in common with the natural order of the keys. The order is kept by means of a doubly linked list, represented via an array of longs parallel to the table.

This class implements the interface of a sorted set, so to allow easy access of the iteration order: for instance, you can get the first element in iteration order with first() without having to create an iterator; however, this class partially violates the SortedSet contract because all subset methods throw an exception and comparator() returns always null.

Additional methods, such as addAndMoveToFirst(), make it easy to use instances of this class as a cache (e.g., with LRU policy).

The iterators provided by this class are type-specific list iterators, and can be started at any element which is in the set (if the provided element is not in the set, a NoSuchElementException exception will be thrown). If, however, the provided element is not the first or last element in the set, the first access to the list index will require linear time, as in the worst case the entire set must be scanned in iteration order to retrieve the positional index of the starting element. If you use just the methods of a type-specific BidirectionalIterator, however, all operations will be performed in constant time.

See Also:
  • Constructor Details

    • ObjectLinkedOpenHashSet

      public ObjectLinkedOpenHashSet(int expected, float f)
      Creates a new hash set.

      The actual table size will be the least power of two greater than expected/f.

      Parameters:
      expected - the expected number of elements in the hash set.
      f - the load factor.
    • ObjectLinkedOpenHashSet

      public ObjectLinkedOpenHashSet(int expected)
      Creates a new hash set with Hash.DEFAULT_LOAD_FACTOR as load factor.
      Parameters:
      expected - the expected number of elements in the hash set.
    • ObjectLinkedOpenHashSet

      public ObjectLinkedOpenHashSet()
      Creates a new hash set with initial expected Hash.DEFAULT_INITIAL_SIZE elements and Hash.DEFAULT_LOAD_FACTOR as load factor.
    • ObjectLinkedOpenHashSet

      public ObjectLinkedOpenHashSet(Collection<? extends K> c, float f)
      Creates a new hash set copying a given collection.
      Parameters:
      c - a Collection to be copied into the new hash set.
      f - the load factor.
    • ObjectLinkedOpenHashSet

      public ObjectLinkedOpenHashSet(Collection<? extends K> c)
      Creates a new hash set with Hash.DEFAULT_LOAD_FACTOR as load factor copying a given collection.
      Parameters:
      c - a Collection to be copied into the new hash set.
    • ObjectLinkedOpenHashSet

      public ObjectLinkedOpenHashSet(ObjectCollection<? extends K> c, float f)
      Creates a new hash set copying a given type-specific collection.
      Parameters:
      c - a type-specific collection to be copied into the new hash set.
      f - the load factor.
    • ObjectLinkedOpenHashSet

      public ObjectLinkedOpenHashSet(ObjectCollection<? extends K> c)
      Creates a new hash set with Hash.DEFAULT_LOAD_FACTOR as load factor copying a given type-specific collection.
      Parameters:
      c - a type-specific collection to be copied into the new hash set.
    • ObjectLinkedOpenHashSet

      public ObjectLinkedOpenHashSet(Iterator<? extends K> i, float f)
      Creates a new hash set using elements provided by a type-specific iterator.
      Parameters:
      i - a type-specific iterator whose elements will fill the set.
      f - the load factor.
    • ObjectLinkedOpenHashSet

      public ObjectLinkedOpenHashSet(Iterator<? extends K> i)
      Creates a new hash set with Hash.DEFAULT_LOAD_FACTOR as load factor using elements provided by a type-specific iterator.
      Parameters:
      i - a type-specific iterator whose elements will fill the set.
    • ObjectLinkedOpenHashSet

      public ObjectLinkedOpenHashSet(K[] a, int offset, int length, float f)
      Creates a new hash set and fills it with the elements of a given array.
      Parameters:
      a - an array whose elements will be used to fill the set.
      offset - the first element to use.
      length - the number of elements to use.
      f - the load factor.
    • ObjectLinkedOpenHashSet

      public ObjectLinkedOpenHashSet(K[] a, int offset, int length)
      Creates a new hash set with Hash.DEFAULT_LOAD_FACTOR as load factor and fills it with the elements of a given array.
      Parameters:
      a - an array whose elements will be used to fill the set.
      offset - the first element to use.
      length - the number of elements to use.
    • ObjectLinkedOpenHashSet

      public ObjectLinkedOpenHashSet(K[] a, float f)
      Creates a new hash set copying the elements of an array.
      Parameters:
      a - an array to be copied into the new hash set.
      f - the load factor.
    • ObjectLinkedOpenHashSet

      public ObjectLinkedOpenHashSet(K[] a)
      Creates a new hash set with Hash.DEFAULT_LOAD_FACTOR as load factor copying the elements of an array.
      Parameters:
      a - an array to be copied into the new hash set.
  • Method Details

    • of

      public static <K> ObjectLinkedOpenHashSet<K> of()
      Creates a new empty hash set.
      Returns:
      a new empty hash set.
    • of

      public static <K> ObjectLinkedOpenHashSet<K> of(K e)
      Creates a new hash set with Hash.DEFAULT_LOAD_FACTOR as load factor using the given element.
      Parameters:
      e - the element that the returned set will contain.
      Returns:
      a new hash set with Hash.DEFAULT_LOAD_FACTOR as load factor containing e.
    • of

      public static <K> ObjectLinkedOpenHashSet<K> of(K e0, K e1)
      Creates a new hash set with Hash.DEFAULT_LOAD_FACTOR as load factor using the elements given.
      Parameters:
      e0 - the first element.
      e1 - the second element.
      Returns:
      a new hash set with Hash.DEFAULT_LOAD_FACTOR as load factor containing e0 and e1.
      Throws:
      IllegalArgumentException - if there were duplicate entries.
    • of

      public static <K> ObjectLinkedOpenHashSet<K> of(K e0, K e1, K e2)
      Creates a new hash set with Hash.DEFAULT_LOAD_FACTOR as load factor using the elements given.
      Parameters:
      e0 - the first element.
      e1 - the second element.
      e2 - the third element.
      Returns:
      a new hash set with Hash.DEFAULT_LOAD_FACTOR as load factor containing e0, e1, and e2.
      Throws:
      IllegalArgumentException - if there were duplicate entries.
    • of

      @SafeVarargs public static <K> ObjectLinkedOpenHashSet<K> of(K... a)
      Creates a new hash set with Hash.DEFAULT_LOAD_FACTOR as load factor using a list of elements.
      Parameters:
      a - a list of elements that will be used to initialize the new hash set.
      Returns:
      a new hash set with Hash.DEFAULT_LOAD_FACTOR as load factor containing the elements of a.
      Throws:
      IllegalArgumentException - if a duplicate entry was encountered.
    • toSet

      public static <K> Collector<K,?,ObjectLinkedOpenHashSet<K>> toSet()
      Returns a Collector that collects a Stream's elements into a new hash set.
    • toSetWithExpectedSize

      public static <K> Collector<K,?,ObjectLinkedOpenHashSet<K>> toSetWithExpectedSize(int expectedSize)
      Returns a Collector that collects a Stream's elements into a new hash set, potentially pre-allocated to handle the given size.
    • ensureCapacity

      public void ensureCapacity(int capacity)
      Ensures that this set can hold a certain number of elements without rehashing.
      Parameters:
      capacity - a number of elements; there will be no rehashing unless the set size exceeds this number.
    • addAll

      public boolean addAll(Collection<? extends K> c)
      Specified by:
      addAll in interface Collection<K>
      Specified by:
      addAll in interface Set<K>
      Overrides:
      addAll in class AbstractCollection<K>
    • add

      public boolean add(K k)
      Specified by:
      add in interface Collection<K>
      Specified by:
      add in interface Set<K>
      Overrides:
      add in class AbstractCollection<K>
    • addOrGet

      public K addOrGet(K k)
      Add a random element if not present, get the existing value if already present. This is equivalent to (but faster than) doing a:
       K exist = set.get(k);
       if (exist == null) {
              set.add(k);
              exist = k;
       }
       
    • remove

      public boolean remove(Object k)
      Specified by:
      remove in interface Collection<K>
      Specified by:
      remove in interface Set<K>
      Overrides:
      remove in class AbstractCollection<K>
    • contains

      public boolean contains(Object k)
      Specified by:
      contains in interface Collection<K>
      Specified by:
      contains in interface Set<K>
      Overrides:
      contains in class AbstractCollection<K>
    • get

      public K get(Object k)
      Returns the element of this set that is equal to the given key, or null.
      Returns:
      the element of this set that is equal to the given key, or null.
    • removeFirst

      public K removeFirst()
      Removes the first key in iteration order.
      Specified by:
      removeFirst in interface SequencedCollection<K>
      Specified by:
      removeFirst in interface SortedSet<K>
      Returns:
      the first key.
      Throws:
      NoSuchElementException - is this set is empty.
    • removeLast

      public K removeLast()
      Removes the the last key in iteration order.
      Specified by:
      removeLast in interface SequencedCollection<K>
      Specified by:
      removeLast in interface SortedSet<K>
      Returns:
      the last key.
      Throws:
      NoSuchElementException - is this set is empty.
    • addAndMoveToFirst

      public boolean addAndMoveToFirst(K k)
      Adds a key to the set; if the key is already present, it is moved to the first position of the iteration order.
      Parameters:
      k - the key.
      Returns:
      true if the key was not present.
    • addAndMoveToLast

      public boolean addAndMoveToLast(K k)
      Adds a key to the set; if the key is already present, it is moved to the last position of the iteration order.
      Parameters:
      k - the key.
      Returns:
      true if the key was not present.
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<K>
      Specified by:
      clear in interface Set<K>
      Overrides:
      clear in class AbstractCollection<K>
    • size

      public int size()
      Specified by:
      size in interface Collection<K>
      Specified by:
      size in interface Set<K>
      Specified by:
      size in class AbstractCollection<K>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Collection<K>
      Specified by:
      isEmpty in interface Set<K>
      Overrides:
      isEmpty in class AbstractCollection<K>
    • first

      public K first()
      Returns the first element of this set in iteration order.
      Specified by:
      first in interface SortedSet<K>
      Returns:
      the first element in iteration order.
    • last

      public K last()
      Returns the last element of this set in iteration order.
      Specified by:
      last in interface SortedSet<K>
      Returns:
      the last element in iteration order.
    • tailSet

      public ObjectSortedSet<K> tailSet(K from)
      Returns a view of the portion of this sorted set whose elements are greater than or equal to fromElement.
      Specified by:
      tailSet in interface ObjectSortedSet<K>
      Specified by:
      tailSet in interface SortedSet<K>
      See Also:
      Implementation Specification:
      This implementation just throws an UnsupportedOperationException.
    • headSet

      public ObjectSortedSet<K> headSet(K to)
      Returns a view of the portion of this sorted set whose elements are strictly less than toElement.
      Specified by:
      headSet in interface ObjectSortedSet<K>
      Specified by:
      headSet in interface SortedSet<K>
      See Also:
      Implementation Specification:
      This implementation just throws an UnsupportedOperationException.
    • subSet

      public ObjectSortedSet<K> subSet(K from, K to)
      Returns a view of the portion of this sorted set whose elements range from fromElement, inclusive, to toElement, exclusive.
      Specified by:
      subSet in interface ObjectSortedSet<K>
      Specified by:
      subSet in interface SortedSet<K>
      See Also:
      Implementation Specification:
      This implementation just throws an UnsupportedOperationException.
    • comparator

      public Comparator<? super K> comparator()
      Specified by:
      comparator in interface SortedSet<K>
      Implementation Specification:
      This implementation just returns null.
    • iterator

      public ObjectListIterator<K> iterator(K from)
      Returns a type-specific list iterator on the elements in this set, starting from a given element of the set. Please see the class documentation for implementation details.
      Specified by:
      iterator in interface ObjectSortedSet<K>
      Parameters:
      from - an element to start from.
      Returns:
      a type-specific list iterator starting at the given element.
      Throws:
      IllegalArgumentException - if from does not belong to the set.
    • iterator

      public ObjectListIterator<K> iterator()
      Returns a type-specific list iterator on the elements in this set, starting from the first element. Please see the class documentation for implementation details.
      Specified by:
      iterator in interface Collection<K>
      Specified by:
      iterator in interface Iterable<K>
      Specified by:
      iterator in interface ObjectBidirectionalIterable<K>
      Specified by:
      iterator in interface ObjectCollection<K>
      Specified by:
      iterator in interface ObjectIterable<K>
      Specified by:
      iterator in interface ObjectSet<K>
      Specified by:
      iterator in interface ObjectSortedSet<K>
      Specified by:
      iterator in interface Set<K>
      Specified by:
      iterator in class AbstractObjectSortedSet<K>
      Returns:
      a type-specific list iterator starting at the first element.
      See Also:
    • spliterator

      public ObjectSpliterator<K> spliterator()
      Returns a type-specific spliterator on the elements of this sorted-set.

      SortedSet spliterators must report at least Spliterator.DISTINCT, Spliterator.ORDERED, and Spliterator.SORTED. The returned spliterator's getComparator() must be the same (or at the very least, consistent with) this instance's SortedSet.comparator().

      See SortedSet.spliterator() for more documentation on the requirements of the returned spliterator.

      There isn't a way to split efficiently while still preserving order for a linked data structure, so this implementation is just backed by the iterator. Thus, this spliterator is not well optimized for parallel streams.

      Note, contrary to the specification of SortedSet, this spliterator does not, report Spliterator.SORTED. This is because iteration order is based on insertion order, not natural ordering.

      Specified by:
      spliterator in interface Collection<K>
      Specified by:
      spliterator in interface Iterable<K>
      Specified by:
      spliterator in interface ObjectCollection<K>
      Specified by:
      spliterator in interface ObjectIterable<K>
      Specified by:
      spliterator in interface ObjectSet<K>
      Specified by:
      spliterator in interface ObjectSortedSet<K>
      Specified by:
      spliterator in interface Set<K>
      Specified by:
      spliterator in interface SortedSet<K>
      Returns:
      a type-specific spliterator on the elements of this collection.
    • forEach

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

      public boolean trim()
      Rehashes this set, making the table as small as possible.

      This method rehashes the table to the smallest size satisfying the load factor. It can be used when the set will not be changed anymore, so to optimize access speed and size.

      If the table size is already the minimum possible, this method does nothing.

      Returns:
      true if there was enough memory to trim the set.
      See Also:
    • trim

      public boolean trim(int n)
      Rehashes this set if the table is too large.

      Let N be the smallest table size that can hold max(n,size()) entries, still satisfying the load factor. If the current table size is smaller than or equal to N, this method does nothing. Otherwise, it rehashes this set in a table of size N.

      This method is useful when reusing sets. Clearing a set leaves the table size untouched. If you are reusing a set many times, you can call this method with a typical size to avoid keeping around a very large table just because of a few large transient sets.

      Parameters:
      n - the threshold for the trimming.
      Returns:
      true if there was enough memory to trim the set.
      See Also:
    • clone

      public ObjectLinkedOpenHashSet<K> clone()
      Returns a deep copy of this set.

      This method performs a deep copy of this hash set; the data stored in the set, however, is not cloned. Note that this makes a difference only for object keys.

      Returns:
      a deep copy of this set.
    • hashCode

      public int hashCode()
      Returns a hash code for this set. This method overrides the generic method provided by the superclass. Since equals() is not overriden, it is important that the value returned by this method is the same value as the one returned by the overriden method.
      Specified by:
      hashCode in interface Collection<K>
      Specified by:
      hashCode in interface Set<K>
      Overrides:
      hashCode in class AbstractObjectSet<K>
      Returns:
      a hash code for this set.