概要
Collection类图
Collection 简介
Collection 的定义
public interface Collection<E> extends Iterable<E>{}
Collection是一个接口,是高度抽象的集合,其包含了集合的基本操作,如添加,删除,遍历,是否为空,集合转为数组,集合大小,集合之间的合并。
其API如下:
// Collection的API
abstract boolean add(E object) //添加元素
abstract boolean addAll(Collection<? extends E> collection) //添加集合
abstract void clear() //删除元素
abstract boolean contains(Object object) //是否包含特定元素
abstract boolean containsAll(Collection<?> collection) //是否包含某个集合
abstract boolean equals(Object object) //
abstract int hashCode() //获取集合的hashCode值
abstract boolean isEmpty() //判断集合是否为空
abstract Iterator<E> iterator() //获取集合的迭代器
abstract boolean remove(Object object) //移除元素
abstract boolean removeAll(Collection<?> collection)
abstract boolean retainAll(Collection<?> collection)
abstract int size() //获取集合的大小
abstract <T> T[] toArray(T[] array) //集合转化成数组
abstract Object[] toArray() //集合转化成数组
List 简介
List的定义
public interface List<E> extends Collection<E>{}
List 是一个继承于Collection的接口,即List是集合中的一种接口。所以,Collection 接口有的方法List中也有。
List 是一个有序队列,集合的初始索引是0,以后每增加一个元素索引+1。List中允许有重复元素。
其增加的API如下:(与Collection相同的API不重复列出)
abstract void add(int location, E object) //在指定位置添加元素
abstract boolean addAll(int location, Collection<? extends E> collection) //在指定位置添加集合
abstract E get(int location) //获取指定位置的元素
abstract int indexOf(Object object)//获取元素的索引值
abstract int lastIndexOf(Object object)
abstract ListIterator<E> listIterator(int location)
abstract ListIterator<E> listIterator()
abstract E remove(int location) //移除指定位置的元素
abstract E set(int location, E object)
abstract List<E> subList(int start, int end) //获取子集合
Set 简介
Set的定义
public interface Set<E> extends Collection<E>{}
Set是一个继承于Collection的接口,即List是集合中的一种接口。 不允许有重复元素。其API与Collection的API完全一样。再次不再赘述。
Queue 简介
AbstractCollection
AbstractCollection的定义是
public abstract class AbstractCollection<E> implements Collection<E>{}
AbstractCollection是一个实现了Collection的抽象类,其实现了Collection中除了iterator和size方法之外的方法。所以,继承与AbstractCollection的其他实现类,只需要实现AbstractCollection类没有实现的方法以及按照需求重写相关方法。
public abstract Iterator<E> iterator();
public abstract int size();
1
2
具体分析下AbstractCollection类下的相关方法的实现。
public boolean isEmpty() {
return size() == 0;
}
//检查集合是否包含特定元素
public boolean contains(Object o) {
Iterator<E> it = iterator();
if (o==null) { //任何非空集合都包含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() {
// Estimate size of array; be prepared to see more or fewer elements
Object[] r = new Object[size()]; //定义一个与集合等大的数组
Iterator<E> it = iterator();
//循环将集合中的元素copy到数组r中
for (int i = 0; i < r.length; i++) {
if (! it.hasNext()) // fewer elements than expected
return Arrays.copyOf(r, i);
r[i] = it.next();
}
return it.hasNext() ? finishToArray(r, it) : r;
}
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
private static <T> T[] finishToArray(T[] r, Iterator<?> it) {
int i = r.length;
while (it.hasNext()) {
int cap = r.length;
if (i == cap) {
int newCap = cap + (cap >> 1) + 1;
// overflow-conscious code
if (newCap - MAX_ARRAY_SIZE > 0)
newCap = hugeCapacity(cap + 1);
r = Arrays.copyOf(r, newCap);
}
r[i++] = (T)it.next();
}
// trim if overallocated
return (i == r.length) ? r : Arrays.copyOf(r, i);
}
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError
("Required array size too large");
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
public <T> T[] toArray(T[] a) {
// Estimate size of array; be prepared to see more or fewer elements
int size = size();
T[] r = a.length >= size ? a :
(T[])java.lang.reflect.Array
.newInstance(a.getClass().getComponentType(), size);
Iterator<E> it = iterator();
for (int i = 0; i < r.length; i++) {
if (! it.hasNext()) { // fewer elements than expected
if (a == r) {
r[i] = null; // null-terminate
} else if (a.length < i) {
return Arrays.copyOf(r, i);
} else {
System.arraycopy(r, 0, a, 0, i);
if (a.length > i) {
a[i] = null;
}
}
return a;
}
r[i] = (T)it.next();
}
// more elements than expected
return it.hasNext() ? finishToArray(r, it) : r;
}
// Modification Operations
public boolean add(E e) {
throw new UnsupportedOperationException();
}
//删除元素o
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;
}
// Bulk Operations
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;
}
//删除集合c中所有元素(如果存在的话)
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;
}
//删除不在集合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;
}
// 清空
public void clear() {
Iterator<E> it = iterator();
while (it.hasNext()) {
it.next();
it.remove();
}
}
// String conversion
// 将List 转成[String] biao'shi
public String toString() {
Iterator<E> it = iterator();
if (! it.hasNext())
return "[]";
StringBuilder sb = new StringBuilder();
sb.append('[');
for (;;) {
E e = it.next();
sb.append(e == this ? "(this Collection)" : e);
if (! it.hasNext())
return sb.append(']').toString();
sb.append(',').append(' ');
}
}
。。。。。。。。。。。。。。。。。
版权原因,完整文章,请参考如下:Java 集合框架02---Collection的源码分析