Class CachedVector<E>

All Implemented Interfaces:
Serializable, Cloneable, Iterable<E>, Collection<E>, List<E>, RandomAccess

public class CachedVector<E> extends 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:
  • Constructor Details

    • CachedVector

      public CachedVector()
    • CachedVector

      public CachedVector(Collection<? extends E> c)
    • CachedVector

      public CachedVector(int initialCapacity)
  • Method Details

    • indexOf

      public int indexOf(Object elem)
      Specified by:
      indexOf in interface List<E>
      Overrides:
      indexOf in class 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.
    • createCache

      protected Map<Object,CachedVector.IntegerWrapper> createCache()
    • cacheIt

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

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

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

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

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

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

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

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

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

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

      public E set(int index, E element)
      Specified by:
      set in interface List<E>
      Overrides:
      set in class 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 Vector<E>