1. Collection < E >
集合框架结构中的根接口。
- int size(); 返回集合中元素个数
- boolean isEmpty(); 返回集合是否为空
- boolean contains(Object o); 传入一个对象判断集合中是否存在
- Iterator iterator(); 覆写父接口interface Iterable中迭代方法
- Object[] toArray(); 返回集合为对象数组
- T[] toArray(T[] a); 传入一个泛型数组
返回一个包含此集合中所有元素的数组;
返回的数组的运行时类型是指定数组的运行时类型。
如果集合适合指定的数组,则返回该集合。
类的运行时类型将为新数组分配
指定的数组和集合的大小。
- boolean add(E e); 添加泛型元素到集合中
- boolean remove(Object o); 删除集合中指定对象
- boolean containsAll(Collection<?> c); 传入实现了Collection接口的集合并判断该集合是否与其有交集,有则返回true否则false
- boolean addAll(Collection<? extends E> c); 传入实现了Collection接口的集合并且其泛型类型为该集合泛型类型或者子类类型的、添加到当前集合中。
- boolean removeAll(Collection<?> c); 将传入集合与当前集合取差集
default boolean removeIf(Predicate<? super E> filter) {
Objects.requireNonNull(filter);
boolean removed = false;
final Iterator<E> each = iterator();
while (each.hasNext()) {
if (filter.test(each.next())) {
each.remove();
removed = true;
}
}
return removed;
}
删除此集合中满足给定条件的所有元素
- boolean retainAll(Collection<?> c); c集合作为白名单到该集合中取出名单中元素
- void clear(); 从集合中移除所有元素
- boolean equals(Object o); 将指定对象与此集合进行相等比较
- int hashCode(); 返回此对象的hash值
default Spliterator<E> spliterator() {
return Spliterators.spliterator(this, 0);
}
在此集合中的元素上创建一个扫描竞技器(用于遍历和分区源元素的对象)。
default Stream<E> stream() {
return StreamSupport.stream(spliterator(), false);
}
使用此集合作为其源返回顺序流。
default Stream<E> parallelStream() {
return StreamSupport.stream(spliterator(), true);
}
将此集合作为其源返回可能并行流。
该方法允许返回顺序流。
2. ArrayList< E >
该类继承了AbstractList < E >抽像类、实现了List < E >, RandomAccess, Cloneable, java.io.Serializable接口
2.1 abstract class AbstractList < E >
2.1.1 AbstractList
==
1. 内部类 Itr
- boolean hasNext() 获取下一个元素是否存在、判断索引是否不等于size()
public boolean hasNext() {
return cursor != size();
}
- E next()获取下一个元素
public E next() {
checkForComodification();
try {
int i = cursor;
E next = get(i);
lastRet = i;
cursor = i + 1;
return next;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
- void remove() 清空集合
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
AbstractList.this.remove(lastRet);
if (lastRet < cursor)
cursor--;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException e) {
throw new ConcurrentModificationException();
}
}
- final void checkForComodification()
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
2. 内部类 ListItr
结构
private class ListItr extends Itr
implements ListIterator<E>
- 构造器指定索引位置
ListItr(int index) {
cursor = index;
}
- public boolean hasPrevious() 判断索引不为0
public boolean hasPrevious() {
return cursor != 0;
}
- public E previous() 返回一个元素
public E previous() {
checkForComodification();
try {
int i = cursor - 1;
E previous = get(i);
lastRet = cursor = i;
return previous;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
- public int nextIndex() 下一个元素索引
public int nextIndex() {
return cursor;
}
- public int previousIndex() 前一个元素索引
public int previousIndex() {
return cursor-1;
}
- public void set(E e) 后一元素设置为e
public void set(E e) {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
AbstractList.this.set(lastRet, e);
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
- public void add(E e) 追加元素
public void add(E e) {
checkForComodification();
try {
int i = cursor;
AbstractList.this.add(i, e);
lastRet = -1;
cursor = i + 1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}