AbstractList.equals(Object o)

做java2年多,没什么长进,在未来1年要研究一般JDK源码,每天记载一点:

AbstractList.equals(Object o)的实现源码写的实在是太简练、精辟了

public boolean equals(Object o) {
	if (o == this)
	    return true;
	if (!(o instanceof List))
	    return false;

	ListIterator<E> e1 = listIterator();
	ListIterator e2 = ((List) o).listIterator();
	while(e1.hasNext() && e2.hasNext()) {
	    E o1 = e1.next();
	    Object o2 = e2.next();
	    if (!(o1==null ? o2==null : o1.equals(o2)))
		return false;
	}
	return !(e1.hasNext() || e2.hasNext());
    }

Iterrator实现源码

private class Itr implements Iterator<E> {
		/**
		 * Index of element to be returned by subsequent call to next.
		 */
		int cursor = 0;

		/**
		 * Index of element returned by most recent call to next or
		 * previous.  Reset to -1 if this element is deleted by a call
		 * to remove.
		 */
		int lastRet = -1;

		/**
		 * The modCount value that the iterator believes that the backing
		 * List should have.  If this expectation is violated, the iterator
		 * has detected concurrent modification.
		 */
		int expectedModCount = modCount;

		public boolean hasNext() {
	            return cursor != size();
		}

		public E next() {
	            checkForComodification();
		    try {
			E next = get(cursor);
			lastRet = cursor++;
			return next;
		    } catch (IndexOutOfBoundsException e) {
			checkForComodification();
			throw new NoSuchElementException();
		    }
		}

		public void remove() {
			//验证当前迭代元素是否已经remove
		    if (lastRet == -1)
			throw new IllegalStateException();
	            checkForComodification();

		    try {
		    //remove当前迭代的元素
			AbstractList.this.remove(lastRet);
			//索引位置-1
			if (lastRet < cursor)
			    cursor--;
			//修改移除元素标志
			lastRet = -1;
			//遍历时 集合添加修改标志?
			expectedModCount = modCount;
		    } catch (IndexOutOfBoundsException e) {
			throw new ConcurrentModificationException();
		    }
		}

		final void checkForComodification() {
		    if (modCount != expectedModCount)
			throw new ConcurrentModificationException();
		}
	    }



 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值