Java容器类源码-Vector的最全的源码分析(四)

Java容器类源码-Vector的最全的源码分析(四)

(31) public synchronized boolean retainAll(Collection<?> c)

源码解释:

将数组中不是c中包含的元素全部移除。调用AbstractCollection的实现,代码也很简单,不赘叙。

 public synchronized boolean retainAll(Collection<?> c) {
 return super.retainAll(c);
 }
 public boolean retainAll(Collection<?> c) {
 Objects.requireNonNull(c);
 boolean modified = false;
 Iterator<E> it = iterator();
 while (it.hasNext()) {
 if (!c.contains(it.next())) {
 it.remove();
 modified = true;
 }
 }
 return modified;
 }

(32) public synchronized boolean addAll(int index, Collection<? extends E> c)

源码解释:

在index位置插入集合c。实现原理和在某个位置插入元素原理一样,不赘叙。

 public synchronized boolean addAll(int index, Collection<? extends E> c) {
 modCount++;
 if (index < 0 || index > elementCount)
 throw new ArrayIndexOutOfBoundsException(index);
 
 Object[] a = c.toArray();
 int numNew = a.length;
 ensureCapacityHelper(elementCount + numNew);
 
 int numMoved = elementCount - index;
 if (numMoved > 0)
 System.arraycopy(elementData, index, elementData, index + numNew,
 numMoved);
 
 System.arraycopy(a, 0, elementData, index, numNew);
 elementCount += numNew;
 return numNew != 0;
 }

(33) public synchronized List<E> subList(int fromIndex, int toIndex)

源码解释:

调用父类的subList,然后通过Collections.synchronizedList来保证子List是同步的,这也就印证了我们前面所说的Collections.synchronizedList初始化的ArrayList和Vector是一样效率的,因为它们的同步方式都是一样的,而增删改查这些操作对于它们两个来说都是一样的原理,所以可以知道它们的效率是一样的。

 public synchronized List<E> subList(int fromIndex, int toIndex) {
 return Collections.synchronizedList(super.subList(fromIndex, toIndex),
 this);
 }

(34) protected synchronized void removeRange(int fromIndex, int toIndex)

源码解释:

将某个范围的数据移除。实现原理和删除某个位置的元素原理是一样的,不赘叙。

 protected synchronized void removeRange(int fromIndex, int toIndex) {
 modCount++;
 int numMoved = elementCount - toIndex;
 System.arraycopy(elementData, toIndex, elementData, fromIndex,
 numMoved);
 
 // Let gc do its work
 int newElementCount = elementCount - (toIndex-fromIndex);
 while (elementCount != newElementCount)
 elementData[--elementCount] = null;
 }

(35) public synchronized ListIterator<E> listIterator(int index)

源码解释:

返回一个从index位置开始的LIstIterator,方便我们遍历Vector,关于ListIterator在《Java容器类源码-LinkedList的最全的源码分析》已经详说,这里不赘叙。

public synchronized ListIterator<E> listIterator(int index) {
 if (index < 0 || index > elementCount)
 throw new IndexOutOfBoundsException("Index: "+index);
 return new ListItr(index);
 }

(36) public synchronized ListIterator<E> listIterator()

源码解释:

返回一个从0位置开始的ListIterator,不赘叙。

 public synchronized ListIterator<E> listIterator() {
 return new ListItr(0);
 }

(37) public synchronized Iterator<E> iterator()

源码解释:

返回一个Iterator实现类Itr。有人会问ListIterator和Itr有什么区别吗?其实ListIterator是Itr的子类,它在Itr的基础上再增加了一些接口,例如hasPrevious(),nextIndex()等,所以如果觉得Iterator不能满足你的需求,可以看一下ListIterator里面提供的API。

 public synchronized Iterator<E> iterator() {
 return new Itr();
 }

(38) public Spliterator<E> spliterator()

源码解释:

实例化一个VectorSpliterator对象,并返回。VectorSpliterator是JDK1.8之后LinkedList新增的内部类,因为用得比较少,我就不在这里班门弄斧了,大家有需要可以自行深入研究。

 public Spliterator<E> spliterator() {
 return new VectorSpliterator<>(this, null, 0, -1, 0);
 }

四、总结

看完了Vector的源码,我觉得我们需要学到比较重要的几点。首先是开头所说的Vector和ArrayList的区别,这里就不重复了。第二个就是我们通过subList这个实现可以看到,它的子序列其实也是通过Collections.synchronizedList来初始化子序列并返回的,所以其实Collections.synchronizedList初始化的ArrayList实现同步的原理和Vector是一样的,而ArrayList和Vector的底层都是数组,常规的增删改查操作是一样的,所以我们可以确定Vector和实现了同步的ArrayList在数据操作时的效率是相近的,所以我觉得我们并不需要纠结在考虑线程安全时到底是用Collections.synchronizedList初始化的ArrayList还是Vector。第三个就是需要了解到Vector的增删改查实现原理,它的api核心可以说就是这几个方法了,所有其他api都是围绕这几个方法来进行扩展。

---------------------

每天都在分享文章,也每天都有人想要我出来给大家分享下怎么去学习Java。大家都知道,我们是学Java全栈的,大家就肯定以为我有全套的Java系统教程。没错,我是有Java全套系统教程,进扣裙【47】974【9726】所示,进群的时候记得表明自己想要学习什么,不要用小号,这样小编才好给你们发定向资源,今天小编就免费送!~

Java容器类源码-Vector的最全的源码分析(四)

“我们相信人人都可以成为一个程序员,现在开始,找个师兄,带你入门,学习的路上不再迷茫。这里是ja+va修真院,初学者转行到互联网行业的聚集地。"

猜你喜欢

转载自blog.csdn.net/qq_41552245/article/details/86231487