Class CachedVector<E>

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Cloneable, java.lang.Iterable<E>, java.util.Collection<E>, java.util.List<E>, java.util.RandomAccess

    public class CachedVector<E>
    extends java.util.Vector<E>
    This is a fast access Vector that sacrifices memory for speed. It will reduce the speed of indexOf method from O(n) to O(1). However it will at least double the memory used by Vector. So use it appropriately.

    Just like Vector, this implementation is synchronized. In comparison, CachedArrayList is not synchronized.

    See Also:
    Serialized Form
    • Field Summary

      • Fields inherited from class java.util.Vector

        capacityIncrement, elementCount, elementData
      • Fields inherited from class java.util.AbstractList

        modCount
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(int index, E element)  
      boolean add​(E element)  
      boolean addAll​(int index, java.util.Collection<? extends E> c)  
      boolean addAll​(java.util.Collection<? extends E> c)  
      protected void adjustCache​(int index, int increase)
      Adjusts the cache so that all values that are greater than index will increase by the value specified by the increase parameter.
      void cacheAll()
      Cache all the element index.
      void cacheIt​(java.lang.Object o, int index)
      Caches the index of the element.
      void clear()  
      protected java.util.Map<java.lang.Object,​CachedVector.IntegerWrapper> createCache()  
      int indexOf​(java.lang.Object elem)  
      void invalidateCache()
      Invalidated the whole cache.
      boolean isLazyCaching()  
      E remove​(int index)  
      boolean remove​(java.lang.Object o)  
      boolean removeAll​(java.util.Collection<?> c)  
      protected void removeRange​(int fromIndex, int toIndex)  
      E set​(int index, E element)  
      void setLazyCaching​(boolean lazyCaching)  
      void uncacheAll()
      Uncache the whole cache.
      void uncacheIt​(java.lang.Object o)
      Uncaches the index of the element.
      • Methods inherited from class java.util.Vector

        addElement, capacity, clone, contains, containsAll, copyInto, elementAt, elements, ensureCapacity, equals, firstElement, forEach, get, hashCode, indexOf, insertElementAt, isEmpty, iterator, lastElement, lastIndexOf, lastIndexOf, listIterator, listIterator, removeAllElements, removeElement, removeElementAt, removeIf, replaceAll, retainAll, setElementAt, setSize, size, sort, spliterator, subList, toArray, toArray, toString, trimToSize
      • Methods inherited from class java.lang.Object

        finalize, getClass, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface java.util.Collection

        parallelStream, stream, toArray
    • Constructor Detail

      • CachedVector

        public CachedVector()
      • CachedVector

        public CachedVector​(java.util.Collection<? extends E> c)
      • CachedVector

        public CachedVector​(int initialCapacity)
    • Method Detail

      • indexOf

        public int indexOf​(java.lang.Object elem)
        Specified by:
        indexOf in interface java.util.List<E>
        Overrides:
        indexOf in class java.util.Vector<E>
      • adjustCache

        protected void adjustCache​(int index,
                                   int increase)
        Adjusts the cache so that all values that are greater than index will increase by the value specified by the increase parameter.
        Parameters:
        index - the index. All values above this index will be changed.
        increase - a positive number to increase or a negative number to decrease.
      • cacheIt

        public void cacheIt​(java.lang.Object o,
                            int index)
        Caches the index of the element.
        Parameters:
        o - the element
        index - the index.
      • uncacheIt

        public void uncacheIt​(java.lang.Object o)
        Uncaches the index of the element.
        Parameters:
        o - the element
      • add

        public boolean add​(E element)
        Specified by:
        add in interface java.util.Collection<E>
        Specified by:
        add in interface java.util.List<E>
        Overrides:
        add in class java.util.Vector<E>
      • add

        public void add​(int index,
                        E element)
        Specified by:
        add in interface java.util.List<E>
        Overrides:
        add in class java.util.Vector<E>
      • remove

        public E remove​(int index)
        Specified by:
        remove in interface java.util.List<E>
        Overrides:
        remove in class java.util.Vector<E>
      • remove

        public boolean remove​(java.lang.Object o)
        Specified by:
        remove in interface java.util.Collection<E>
        Specified by:
        remove in interface java.util.List<E>
        Overrides:
        remove in class java.util.Vector<E>
      • removeAll

        public boolean removeAll​(java.util.Collection<?> c)
        Specified by:
        removeAll in interface java.util.Collection<E>
        Specified by:
        removeAll in interface java.util.List<E>
        Overrides:
        removeAll in class java.util.Vector<E>
      • clear

        public void clear()
        Specified by:
        clear in interface java.util.Collection<E>
        Specified by:
        clear in interface java.util.List<E>
        Overrides:
        clear in class java.util.Vector<E>
      • addAll

        public boolean addAll​(java.util.Collection<? extends E> c)
        Specified by:
        addAll in interface java.util.Collection<E>
        Specified by:
        addAll in interface java.util.List<E>
        Overrides:
        addAll in class java.util.Vector<E>
      • addAll

        public boolean addAll​(int index,
                              java.util.Collection<? extends E> c)
        Specified by:
        addAll in interface java.util.List<E>
        Overrides:
        addAll in class java.util.Vector<E>
      • set

        public E set​(int index,
                     E element)
        Specified by:
        set in interface java.util.List<E>
        Overrides:
        set in class java.util.Vector<E>
      • invalidateCache

        public void invalidateCache()
        Invalidated the whole cache.
      • uncacheAll

        public void uncacheAll()
        Uncache the whole cache. It is the same as invalidateCache().
      • cacheAll

        public void cacheAll()
        Cache all the element index.
      • isLazyCaching

        public boolean isLazyCaching()
      • setLazyCaching

        public void setLazyCaching​(boolean lazyCaching)
      • removeRange

        protected void removeRange​(int fromIndex,
                                   int toIndex)
        Overrides:
        removeRange in class java.util.Vector<E>