《java collection》 学习笔记
Framework Basics
接口类图
Queue
多数运用在JUC
框架中,在此不再分析。
Collection Basic
Framework Implementations
针对上述表格中的所有实现:
- 所有的方法都是
unsynchronized
的,可以通过Collections.synchronizedCollection(Collection<T> c)
转换为线程安全的集合。 - 都实现了
fail−fast
机制。当修改了正在迭代的集合时,下一次迭代时会抛出ConcurrentModificationException
- 都实现了
serializable and cloneable
接口 - 实现了父类的部分方法。 如果实现没有支持一个操作,比如添加或删除元素,实现抛出一个调用尚未完全实现的方法时,
UnsupportedOperationException
。
<<java.util.Collection >>
Collection
接口是 Java Collections Framework的基础。
public interface Collection<E> extends Iterable<E> {
int size();
boolean isEmpty();
boolean contains(Object o);
boolean containsAll(Collection<?> c);
Iterator<E> iterator();
Object[] toArray();
<T> T[] toArray(T[] a);
boolean add(E e);
boolean addAll(Collection<? extends E> c);
boolean remove(Object o);
boolean removeAll(Collection<?> c);
boolean retainAll(Collection<?> c);
void clear();
boolean equals(Object o);
int hashCode();
}
jdk8之后新增了方法
:
//删除满足filter.test()条件的元素。
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;
}
Collection接口继承了java.lang.Iterable接口
:
public interface Iterable<T> {
//获取iterator
Iterator<T> iterator();
//jdk8后新增...
default void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action);
for (T t : this) {
action.accept(t);
}
}
}
<<java.util.Iterator>>
public interface Iterator<E> {
boolean hasNext();
//当 hasNext()返回false时,仍调用next()方法,会抛出NoSuchElementException异常;
E next();
//迭代过程中允许 删除元素.
default void remove() {
throw new UnsupportedOperationException("remove");
}
}
Collection Exceptions
- ConcurrentModificationException: 与集合迭代器的
fail−fast
快速失败性质有关。 - UnsupportedOperationException:当调用未提供实现的方法时相关。
AbstractCollection
在后续的Set
和List
中,他们都继承了AbstractCollection
:
AbstractCollection
public abstract class AbstractCollection<E> implements Collection<E> {
protected AbstractCollection() {
}
// 由子类实现
public abstract Iterator<E> iterator();
public abstract int size();
public boolean isEmpty() {
return size() == 0;
}
public boolean contains(Object o) {
Iterator<E> it = iterator();
if (o==null) {
while (it.hasNext())
if (it.next()==null)
return true;
} else {
while (it.hasNext())
if (o.equals(it.next()))
return true;
}
return false;
}
public Object[] toArray() {
Object[] r = new Object[size()];
Iterator<E> it = iterator();
for (int i = 0; i < r.length; i++) {
if (! it.hasNext()) // 当迭代过程中,remove了元素。 给数组“瘦身”
return Arrays.copyOf(r, i);
r[i] = it.next();
}
//finishToArray略
return it.hasNext() ? finishToArray(r, it) : r;
}
public <T> T[] toArray(T[] a) {
int size = size();
//当参数a[]的长度 < 数组实际长度时,则需要重新构建数组;
T[] r = a.length >= size ? a :
(T[])java.lang.reflect.Array
.newInstance(a.getClass().getComponentType(), size);
Iterator<E> it = iterator();
//略.....
return it.hasNext() ? finishToArray(r, it) : r;
}
public boolean add(E e) {
throw new UnsupportedOperationException();
}
public boolean remove(Object o) {
Iterator<E> it = iterator();
if (o==null) {
while (it.hasNext()) {
if (it.next()==null) {
it.remove();
return true;
}
}
} else {
while (it.hasNext()) {
if (o.equals(it.next())) {
it.remove();
return true;
}
}
}
return false;
}
public boolean containsAll(Collection<?> c) {
for (Object e : c)
if (!contains(e))
return false;
return true;
}
public boolean addAll(Collection<? extends E> c) {
boolean modified = false;
for (E e : c)
if (add(e))
modified = true;
return modified;
}
public boolean removeAll(Collection<?> c) {
Objects.requireNonNull(c);
boolean modified = false;
Iterator<?> it = iterator();
while (it.hasNext()) {
if (c.contains(it.next())) {
it.remove();
modified = true;
}
}
return modified;
}
//取交集
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;
}
public void clear() {
Iterator<E> it = iterator();
while (it.hasNext()) {
it.next();
it.remove();
}
}
// [item1,item2.....]
public String toString() {
//略..
}
}
AbstractCollection实现类Collection
的绝大多数方法,只有如下几个方法需子类实现(当然子类也可以通过重写覆盖父类的实现):
public abstract Iterator<E> iterator();
public abstract int size();
public boolean add(E e) {
throw new UnsupportedOperationException();
}