Class ArraySet<E>

  • Type Parameters:
    E - the type contained in the Set
    All Implemented Interfaces:
    java.lang.Iterable<E>, java.util.Collection<E>, java.util.Set<E>

    public final class ArraySet<E>
    extends java.lang.Object
    implements java.util.Set<E>
    A Set implementation with low allocation cost. It should only be used for small number of objects, as it is implemented as scanning an ArrayList for equality matches. In other words: Performance will only be acceptable for small sets.

    The rationale for this class is the high cost of the object identifier used in IdentityHashMap, where the key set is often used as an identity set.

    Author:
    Steinar Knutsen, baldersheim
    • Constructor Summary

      Constructors 
      Constructor Description
      ArraySet​(int initSize)
      Create a set with an initial capacity of initSize.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(E arg)  
      boolean addAll​(java.util.Collection<? extends E> arg)  
      void clear()  
      boolean contains​(java.lang.Object arg)  
      boolean containsAll​(java.util.Collection<?> arg)
      This is an extremely expensive implementation of Set.containsAll(Collection).
      int indexOf​(java.lang.Object e)
      Expose the index in the internal array of a given object.
      boolean isEmpty()  
      java.util.Iterator<E> iterator()  
      boolean remove​(java.lang.Object arg)  
      boolean removeAll​(java.util.Collection<?> arg)
      This is an extremely expensive implementation of Set.removeAll(Collection).
      boolean retainAll​(java.util.Collection<?> arg)
      This is an extremely expensive implementation of Set.retainAll(Collection).
      int size()  
      java.lang.Object[] toArray()  
      <T> T[] toArray​(T[] arg)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, 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

        equals, hashCode, spliterator
    • Constructor Detail

      • ArraySet

        public ArraySet​(int initSize)
        Create a set with an initial capacity of initSize. The internal array will grow automatically with a linear growth rate if more elements than initSize are added.
        Parameters:
        initSize - initial size of internal element array
    • Method Detail

      • indexOf

        public int indexOf​(java.lang.Object e)
        Expose the index in the internal array of a given object. -1 is returned if the object is not present in the internal array.
        Parameters:
        e - an object to check whether exists in this set
        Returns:
        the index of the argument e in the internal array, or -1 if the object is not present
      • add

        public boolean add​(E arg)
        Specified by:
        add in interface java.util.Collection<E>
        Specified by:
        add in interface java.util.Set<E>
      • addAll

        public boolean addAll​(java.util.Collection<? extends E> arg)
        Specified by:
        addAll in interface java.util.Collection<E>
        Specified by:
        addAll in interface java.util.Set<E>
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Collection<E>
        Specified by:
        clear in interface java.util.Set<E>
      • contains

        public boolean contains​(java.lang.Object arg)
        Specified by:
        contains in interface java.util.Collection<E>
        Specified by:
        contains in interface java.util.Set<E>
      • containsAll

        public boolean containsAll​(java.util.Collection<?> arg)
        This is an extremely expensive implementation of Set.containsAll(Collection). It is implemented as O(n**2).
        Specified by:
        containsAll in interface java.util.Collection<E>
        Specified by:
        containsAll in interface java.util.Set<E>
      • isEmpty

        public boolean isEmpty()
        Specified by:
        isEmpty in interface java.util.Collection<E>
        Specified by:
        isEmpty in interface java.util.Set<E>
      • iterator

        public java.util.Iterator<E> iterator()
        Specified by:
        iterator in interface java.util.Collection<E>
        Specified by:
        iterator in interface java.lang.Iterable<E>
        Specified by:
        iterator in interface java.util.Set<E>
      • remove

        public boolean remove​(java.lang.Object arg)
        Specified by:
        remove in interface java.util.Collection<E>
        Specified by:
        remove in interface java.util.Set<E>
      • removeAll

        public boolean removeAll​(java.util.Collection<?> arg)
        This is an extremely expensive implementation of Set.removeAll(Collection). It is implemented as O(n**2).
        Specified by:
        removeAll in interface java.util.Collection<E>
        Specified by:
        removeAll in interface java.util.Set<E>
      • retainAll

        public boolean retainAll​(java.util.Collection<?> arg)
        This is an extremely expensive implementation of Set.retainAll(Collection). It is implemented as O(n**2).
        Specified by:
        retainAll in interface java.util.Collection<E>
        Specified by:
        retainAll in interface java.util.Set<E>
      • size

        public int size()
        Specified by:
        size in interface java.util.Collection<E>
        Specified by:
        size in interface java.util.Set<E>
      • toArray

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

        public <T> T[] toArray​(T[] arg)
        Specified by:
        toArray in interface java.util.Collection<E>
        Specified by:
        toArray in interface java.util.Set<E>