美文网首页程序员Java学习笔记
Java Collection框架 - Vector

Java Collection框架 - Vector

作者: xiedacon | 来源:发表于2017-03-17 23:13 被阅读102次

Java Collection框架 - Vector

基于jdk1.8

简介

Vector也是基于数组实现的动态数组。Vector几乎每个方法上都存在synchronized关键字,但这并不意味着Vector是线程安全的,只能说明在Vector的方法内是线程安全的。如果需要线程安全的List,可以使用Collections.synchronizedList()或使用java.util.concurrent包下的List实现类

数据结构

先看一下数据结构:

Vector数据结构

Vector的数据结构与ArrayList基本相同,Vector的elementCount属性的作用相当于ArrayList的size属性

主要属性与方法列表

//内部数组
elementData : Object[]
//数量
elementCount : int
//增长数量,默认为0
//小于等于0时,呈2倍增长
- capacityIncrement : int

copyInto(Object[]) : void
trimToSize() : void
ensureCapacity(int) : void
setSize(int) : void
capacity() : int
size() : int
isEmpty() : boolean
elements() : Enumeration<E>
contains(Object) : boolean
indexOf(Object) : int
indexOf(Object, int) : int
lastIndexOf(Object) : int
lastIndexOf(Object, int) : int
elementAt(int) : E
firstElement() : E
lastElement() : E
setElementAt(E, int) : void
removeElementAt(int) : void
insertElementAt(E, int) : void
addElement(E) : void
removeElement(Object) : boolean
removeAllElements() : void
clone() : Object
toArray() : Object[]
toArray(T[]) <T> : T[]
get(int) : E
set(int, E) : E
add(E) : boolean
remove(Object) : boolean
add(int, E) : void
remove(int) : E
clear() : void
containsAll(Collection<?>) : boolean
addAll(Collection<? extends E>) : boolean
removeAll(Collection<? extends E>) : boolean
retainAll(Collection<?>) : boolean
addAll(int, Collection<? extends E>) : boolean
equals(Object) : boolean
hashCode() : int
toString() : String
subList(int, int) : List<E>
listIterator(int) : ListIterator<E>
listIterator() : ListIterator<E>
iterator() : Iterator<E>
sort(Comparator<? super E>) : void

主要代码分析

Vector与ArrayList的实现代码基本相同,只是在每个方法上都加上了synchronized关键字。在这里只分析add(E e)remove(Object o)

  1. add(E e):向Vector中添加元素
  public synchronized boolean add(E e) {
      modCount++;
      ensureCapacityHelper(elementCount + 1);
      elementData[elementCount++] = e;
      return true;
  }

ensureCapacityHelper(int minCapacity)方法用于确定并增加容量,实现代码如下:

  private void ensureCapacityHelper(int minCapacity) {
      // overflow-conscious code
      if (minCapacity - elementData.length > 0)
          grow(minCapacity);
  }

ensureCapacityHelper(int minCapacity)方法很简单,只有当minCapacity大于内部数组长度时,才增加容量。grow(int minCapacity)实现代码如下:

  private void grow(int minCapacity) {
      // overflow-conscious code
      int oldCapacity = elementData.length;
      //当capacityIncrement <= 0时,以2倍进行扩增
      int newCapacity = oldCapacity + ((capacityIncrement > 0) ?
                                       capacityIncrement : oldCapacity);
      if (newCapacity - minCapacity < 0)
          newCapacity = minCapacity;
      if (newCapacity - MAX_ARRAY_SIZE > 0)
          newCapacity = hugeCapacity(minCapacity);
      elementData = Arrays.copyOf(elementData, newCapacity);
  }
  1. remove(Object o):删除对应元素
  public boolean remove(Object o) {
      return removeElement(o);
  }

remove(Object o)依赖removeElement(Object obj),代码如下:

  public synchronized boolean removeElement(Object obj) {
      modCount++;
      //查找obj在Vector中的索引位置
      int i = indexOf(obj);
      if (i >= 0) {
          //删除i位置的元素
          removeElementAt(i);
          return true;
      }
      return false;
  }

removeElement(Object obj)先使用indexOf(Object obj)获取索引位置,然后使用removeElementAt(int index)删除该元素。removeElementAt(int index)代码如下:

  public synchronized void removeElementAt(int index) {
      modCount++;
      if (index >= elementCount) {
          throw new ArrayIndexOutOfBoundsException(index + " >= " +
                                                   elementCount);
      }
      else if (index < 0) {
          throw new ArrayIndexOutOfBoundsException(index);
      }
      int j = elementCount - index - 1;
      if (j > 0) {
          //将index位置后的元素前移一位
          System.arraycopy(elementData, index + 1, elementData, index, j);
      }
      elementCount--;
      elementData[elementCount] = null; /* to let gc do its work */
  }

总结

  1. 默认容量是10
  2. 默认以2倍进行扩增
  3. 大多数方法都被synchronized修饰,但并不意味着Vector是线程安全的,需要线程安全List,可以使用Collections.synchronizedList()或使用java.util.concurrent包下的List实现类
  4. Vector是jdk1.0时期的List实现,现在基本不使用,最好不用

相关文章

网友评论

    本文标题:Java Collection框架 - Vector

    本文链接:https://www.haomeiwen.com/subject/mvwynttx.html