AbstractCollection抽象类源码解析
2016-07-15 10:04
141 查看
所在包
AbstractCollection抽象类实现Collection接口
默认构造器
获得迭代器
获得集合内元素的数量
判断元素时候为空,实现了Collection接口的isEmpty方法
实现Collection接口contains方法
实现toArray方法
实现toArray方法
集合元素数量最大值
静态finishToArray方法
当集合元素数量高于数组原始大小适合,利用finishToarray,完成后续复制
设置最大容量
添加元素
删除元素
判断集合是否包含集合c中的元素
集合c中元素全部添加到当前集合中
当前集合有修改返回true
在当前集合中,删除包含集合c中的元素
获得当前集合 与集合c的差集
当前集合中删除集合c不包含的元素
获得当前集合和集合c的交集
清空集合
集合元素转化成字符串输出
package java.util;
AbstractCollection抽象类实现Collection接口
public abstract class AbstractCollection<E> implements Collection<E>{ // 代码下面讲解 }
默认构造器
protected AbstractCollection() { }
获得迭代器
public abstract Iterator<E> iterator();
获得集合内元素的数量
public abstract int size();
判断元素时候为空,实现了Collection接口的isEmpty方法
public boolean isEmpty() { return size() == 0; }
实现Collection接口contains方法
/** * * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ 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; }
实现toArray方法
/** * * <p>This method is equivalent to: * * List<E> list = new ArrayList<E>(size()); * for (E e : this) * list.add(e); * return list.toArray(); * } */ 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()) // fewer elements than expected return Arrays.copyOf(r, i); r[i] = it.next(); } return it.hasNext() ? finishToArray(r, it) : r; }
实现toArray方法
/** * {@inheritDoc} * * * <p>This method is equivalent to: * * * List<E> list = new ArrayList<E>(size()); * for (E e : this) * list.add(e); * return list.toArray(a); * } * * @throws ArrayStoreException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ 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;
静态finishToArray方法
当集合元素数量高于数组原始大小适合,利用finishToarray,完成后续复制
/** * Reallocates the array being used within toArray when the iterator * returned more elements than expected, and finishes filling it from * the iterator. * * @param r the array, replete with previously stored elements * @param it the in-progress iterator over this collection * @return array containing the elements in the given array, plus any * further elements returned by the iterator, trimmed to size */ 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; }
添加元素
/** * {@inheritDoc} * * <p>This implementation always throws an * <tt>UnsupportedOperationException</tt>. * * @throws UnsupportedOperationException {@inheritDoc} * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} * @throws IllegalStateException {@inheritDoc} */ public boolean add(E e) { throw new UnsupportedOperationException(); }
删除元素
/** * {@inheritDoc} * * * <p>Note that this implementation throws an * <tt>UnsupportedOperationException</tt> if the iterator returned by this * collection's iterator method does not implement the <tt>remove</tt> * method and this collection contains the specified object. * * @throws UnsupportedOperationException {@inheritDoc} * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} */ 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; }
判断集合是否包含集合c中的元素
/** * * * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} * @see #contains(Object) */ public boolean containsAll(Collection<?> c) { for (Object e : c) if (!contains(e)) return false; return true; }
集合c中元素全部添加到当前集合中
当前集合有修改返回true
/** * * @throws UnsupportedOperationException {@inheritDoc} * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc} * @throws IllegalStateException {@inheritDoc} * * @see #add(Object) */ public boolean addAll(Collection<? extends E> c) { boolean modified = false; for (E e : c) if (add(e)) modified = true; return modified; }
在当前集合中,删除包含集合c中的元素
获得当前集合 与集合c的差集
/** * @throws UnsupportedOperationException {@inheritDoc} * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} * * @see #remove(Object) * @see #contains(Object) */ public boolean removeAll(Collection<?> c) { boolean modified = false; Iterator<?> it = iterator(); while (it.hasNext()) { if (c.contains(it.next())) { it.remove(); modified = true; } } return modified; }
当前集合中删除集合c不包含的元素
获得当前集合和集合c的交集
/** * @throws UnsupportedOperationException {@inheritDoc} * @throws ClassCastException {@inheritDoc} * @throws NullPointerException {@inheritDoc} * * @see #remove(Object) * @see #contains(Object) */ public boolean retainAll(Collection<?> c) { boolean modified = false; Iterator<E> it = iterator(); while (it.hasNext()) { if (!c.contains(it.next())) { it.remove(); modified = true; } } return modified; }
清空集合
/** * @throws UnsupportedOperationException {@inheritDoc} */ public void clear() { Iterator<E> it = iterator(); while (it.hasNext()) { it.next(); it.remove(); } }
集合元素转化成字符串输出
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(' '); } }
相关文章推荐
- FPGA基础知识20(FPGA设计异步时钟处理分类及百度文库资料)
- 猴年马月都到了
- 几何模板
- 【转】卷积神经网络全面解析
- 自定义类如何使用multiset进行排序
- android面试题总结加强再加强版(三)
- hihihihihihihi
- AutoCompleteTextView 使用
- C语言标准库函数qsort排序的介绍与使用
- 索引
- 块存储、文件存储、对象存储这三者的本质差别是什么?
- 从 Project Professional 中登录 Project Server
- 获取mysql表插入数据自增字段的值
- JavaWeb:报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- 怎么让网站在浏览器网址前面显示小图标?
- HDU 4768 Flyer
- 自定义控件_day03
- Java 开源分布式缓存框架Ehcache
- TCP/IP简介
- 解决maven依赖传递中的版本冲突问题