package java.util;
//Collection接口的抽象实现类
public abstract class AbstractCollection<e> implements Collection<e> {</e></e>
protected AbstractCollection(){ } // Query Operations public abstract Iterator<E> iterator(); public abstract int size(); public boolean isEmpty(){ return size() == 0; } //利用一个迭代器遍历,判断是否包含对象o***********************************// 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(){ // Estimate size of array; be prepared to see more or fewer elements Object[] r = new Object[size()]; Iterator<E> it = iterator(); for(int i = 0; i < r.length; i++){ if(!it.hasNext()) // 迭代器数量少于size() return Arrays.copyOf(r, i); r[i] = it.next(); } return it.hasNext() ? finishToArray(r, it) : r; } // 转化为指定类型的数组 @SuppressWarnings("unchecked") 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; } //分配数组的最大容量 private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; // 数组扩容,当数组索引指向最后一个元素+1时, // 对数组进行扩容:即创建一个大小为(cap + cap/2 +1)的数组,然后将原数组的内容复制到新数组中 @SuppressWarnings("unchecked") 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; } // Modification Operations 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; } // Bulk Operations public boolean containsAll(Collection<?> c){ for(Object e : c){ if(!contains(e)) return false; } return true; } //** 把容器c 全部添加到新容器中// public boolean addAll(Collection<? extends E> c){ boolean modified = false; for(E e : c){ if(add(e)) modified = true; } return modified; } //** 求差集,迭代器遍历集合this,用contains判断是否存在于c,所以复杂度为o(n^2) 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; } //** 求交集,迭代器遍历集合this,contain判断是存在于c,不存在,则从移除该元素,存在则保留,复杂度o(n^2) 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; } // 利用迭代器移除this中的所有元素 public void clear(){ Iterator<E> it = iterator(); while (it.hasNext()) { it.next(); it.remove(); } } // String conversion //将容器转换为string类型,[元素1,元素2,------] 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(' '); } }
}