AbstractCollection介绍,
分享于 点击 11220 次 点评:272
AbstractCollection介绍,
AbstractCollection介绍
/** *从Collection中继承过来的抽象方法 */ public abstract Iterator<E> iterator(); public abstract int size(); /** *直接返回 size() == 0,与size()的具体实现无关 */ public boolean isEmpty() { return size() == 0; } /** * 通过迭代器遍历所有元素,用equals方法比较,只要有一个符合条件的元素则返回true */ 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; } /** * 通过迭代器遍历集合中,转换为数组 * 等价于 * 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; } /** * 同上面的方法类似,比上面的方法常用,优点在于返回的数组不用做类型转换 * 当参数数组容量不够时,将返回一个新的数组 */ 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; } /** * 默认不支持add */ public boolean add(E e) { throw new UnsupportedOperationException(); } /** * 迭代器遍历集合,通过equals找到符合条件的元素,将其移除 * 对于不可变集合,其迭代器的remove方法中会抛出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; } /* * 迭代器循环遍历集合,调用自身的contains方法 */ public boolean containsAll(Collection<?> c) { for (Object e : c) if (!contains(e)) return false; return true; } /** * foreach循环,调用自身add方法 * 添加成功返回true,失败返回false */ 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(); } } /** * 重写了toString,使得集合可以直接打印出可读性很好的结果 */ 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(' '); } }
相关文章
- 暂无相关文章
用户点评