java源码解读 集合类

1 A.contain(Object o);方法,由abstractCollection实现

public boolean contains(Object o) {
Iterator<E> e = iterator();
if (o==null) {
while (e.hasNext())
if (e.next()==null) //如果o为null,e.next==null,也返回true
return true;
} else {
while (e.hasNext())
if (o.equals(e.next()))
return true;
}
return false;
}


2 A.remove(Object o);方法,由abstractCollection实现

public boolean remove(Object o) {
Iterator<E> e = iterator();
if (o==null) {
while (e.hasNext()) {
if (e.next()==null) {
e.remove(); //如果o为null,e.next==null,也返回true
return true;
}
}
} else {
while (e.hasNext()) {
if (o.equals(e.next())) {
e.remove();
return true;
}
}
}
return false;
}


3.A.retainAll(c)删除c中不包含的对象

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


4.arraylist插入对象是,与初始建立的size需要比较,扩大size

public void ensureCapacity(int minCapacity) {
modCount++;
int oldCapacity = elementData.length;
if (minCapacity > oldCapacity) {
Object oldData[] = elementData;
int newCapacity = (oldCapacity * 3)/2 + 1; //新建的size的大小
if (newCapacity < minCapacity)
newCapacity = minCapacity;
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值