Class HashedList<E>

java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
com.pnfsoftware.jeb.util.collect.HashedList<E>
Type Parameters:
E - list's object type
All Implemented Interfaces:
Iterable<E>, Collection<E>, List<E>, SequencedCollection<E>

@Ser public class HashedList<E> extends AbstractList<E>
A List type suitable for long lists of unique items for which presence checks are often required. For typical list implementations, contains(Object) is in O(N) and therefore slow for long lists.

Limitations: Neither duplicate (per equals) nor null entries are allowed. Methods throw NullPointerException on attempts to insert null entries, and IllegalArgumentException on attempts to insert duplicates.

This class is not thread-safe. This class is serializable.

  • Constructor Details

    • HashedList

      public HashedList()
      Construct an empty hashed-list.
    • HashedList

      public HashedList(int initialCapacity)
      Construct an empty hashed-list.
      Parameters:
      initialCapacity - initial capacity
    • HashedList

      public HashedList(Collection<? extends E> coll)
      Construct an empty hashed-list from a source collection. The constructor will raise if null or duplicate entries are detected.
      Parameters:
      coll - source collection, which should not contain duplicates or null entries
    • HashedList

      public HashedList(Collection<? extends E> coll, boolean ignoreBadEntries)
      Construct an empty hashed-list from a source collection.
      Parameters:
      coll - source collection, which should not contain duplicates or null entries
      ignoreBadEntries - skip insertion of a null or a duplicate element
  • Method Details

    • createUnsafe

      public static <E> HashedList<E> createUnsafe(Collection<? extends E> coll)
      Fast construction of a HashedList: the source collection is not checked for null or duplicate entries. Usage is not recommended.
      Type Parameters:
      E - element type
      Parameters:
      coll - source collection, which should not contain duplicates or null entries
      Returns:
      created hashed list
    • get

      public E get(int index)
      Specified by:
      get in interface List<E>
      Specified by:
      get in class AbstractList<E>
    • size

      public int size()
      Specified by:
      size in interface Collection<E>
      Specified by:
      size in interface List<E>
      Specified by:
      size in class AbstractCollection<E>
    • set

      public E set(int index, E element)
      Specified by:
      set in interface List<E>
      Overrides:
      set in class AbstractList<E>
    • add

      public void add(int index, E element)
      Specified by:
      add in interface List<E>
      Overrides:
      add in class AbstractList<E>
    • remove

      public E remove(int index)
      Specified by:
      remove in interface List<E>
      Overrides:
      remove in class AbstractList<E>
    • contains

      public boolean contains(Object o)
      Fast presence check, in O(log N) instead of O(N), the expected time complexity for search in a list.
      Specified by:
      contains in interface Collection<E>
      Specified by:
      contains in interface List<E>
      Overrides:
      contains in class AbstractCollection<E>
    • containsAll

      public boolean containsAll(Collection<?> c)
      Fast presence check, in O(log N) instead of O(N).
      Specified by:
      containsAll in interface Collection<E>
      Specified by:
      containsAll in interface List<E>
      Overrides:
      containsAll in class AbstractCollection<E>