📄 abstractcollection.java
字号:
* collection's iterator method does not implement the <tt>remove</tt> * method and this collection contains the specified object. * * @param o element to be removed from this collection, if present. * @return <tt>true</tt> if the collection contained the specified * element. * @throws UnsupportedOperationException if the <tt>remove</tt> method is * not supported by this collection. */ public boolean remove(Object o) { Iterator e = iterator(); if (o==null) { while (e.hasNext()) { if (e.next()==null) { e.remove(); return true; } } } else { while (e.hasNext()) { if (o.equals(e.next())) { e.remove(); return true; } } } return false; } // Bulk Operations /** * Returns <tt>true</tt> if this collection contains all of the elements * in the specified collection. <p> * * This implementation iterates over the specified collection, checking * each element returned by the iterator in turn to see if it's * contained in this collection. If all elements are so contained * <tt>true</tt> is returned, otherwise <tt>false</tt>. * * @param c collection to be checked for containment in this collection. * @return <tt>true</tt> if this collection contains all of the elements * in the specified collection. * @throws NullPointerException if the specified collection is null. * * @see #contains(Object) */ public boolean containsAll(Collection c) { Iterator e = c.iterator(); while (e.hasNext()) if(!contains(e.next())) return false; return true; } /** * Adds all of the elements in the specified collection to this collection * (optional operation). The behavior of this operation is undefined if * the specified collection is modified while the operation is in * progress. (This implies that the behavior of this call is undefined if * the specified collection is this collection, and this collection is * nonempty.) <p> * * This implementation iterates over the specified collection, and adds * each object returned by the iterator to this collection, in turn.<p> * * Note that this implementation will throw an * <tt>UnsupportedOperationException</tt> unless <tt>add</tt> is * overridden (assuming the specified collection is non-empty). * * @param c collection whose elements are to be added to this collection. * @return <tt>true</tt> if this collection changed as a result of the * call. * @throws UnsupportedOperationException if this collection does not * support the <tt>addAll</tt> method. * @throws NullPointerException if the specified collection is null. * * @see #add(Object) */ public boolean addAll(Collection c) { boolean modified = false; Iterator e = c.iterator(); while (e.hasNext()) { if(add(e.next())) modified = true; } return modified; } /** * Removes from this collection all of its elements that are contained in * the specified collection (optional operation). <p> * * This implementation iterates over this collection, checking each * element returned by the iterator in turn to see if it's contained * in the specified collection. If it's so contained, it's removed from * this collection with the iterator's <tt>remove</tt> method.<p> * * Note that this implementation will throw an * <tt>UnsupportedOperationException</tt> if the iterator returned by the * <tt>iterator</tt> method does not implement the <tt>remove</tt> method * and this collection contains one or more elements in common with the * specified collection. * * @param c elements to be removed from this collection. * @return <tt>true</tt> if this collection changed as a result of the * call. * @throws UnsupportedOperationException if the <tt>removeAll</tt> method * is not supported by this collection. * @throws NullPointerException if the specified collection is null. * * @see #remove(Object) * @see #contains(Object) */ public boolean removeAll(Collection c) { boolean modified = false; Iterator e = iterator(); while (e.hasNext()) { if(c.contains(e.next())) { e.remove(); modified = true; } } return modified; } /** * Retains only the elements in this collection that are contained in the * specified collection (optional operation). In other words, removes * from this collection all of its elements that are not contained in the * specified collection. <p> * * This implementation iterates over this collection, checking each * element returned by the iterator in turn to see if it's contained * in the specified collection. If it's not so contained, it's removed * from this collection with the iterator's <tt>remove</tt> method.<p> * * Note that this implementation will throw an * <tt>UnsupportedOperationException</tt> if the iterator returned by the * <tt>iterator</tt> method does not implement the <tt>remove</tt> method * and this collection contains one or more elements not present in the * specified collection. * * @param c elements to be retained in this collection. * @return <tt>true</tt> if this collection changed as a result of the * call. * @throws UnsupportedOperationException if the <tt>retainAll</tt> method * is not supported by this Collection. * @throws NullPointerException if the specified collection is null. * * @see #remove(Object) * @see #contains(Object) */ public boolean retainAll(Collection c) { boolean modified = false; Iterator e = iterator(); while (e.hasNext()) { if(!c.contains(e.next())) { e.remove(); modified = true; } } return modified; } /** * Removes all of the elements from this collection (optional operation). * The collection will be empty after this call returns (unless it throws * an exception).<p> * * This implementation iterates over this collection, removing each * element using the <tt>Iterator.remove</tt> operation. Most * implementations will probably choose to override this method for * efficiency.<p> * * Note that this implementation will throw an * <tt>UnsupportedOperationException</tt> if the iterator returned by this * collection's <tt>iterator</tt> method does not implement the * <tt>remove</tt> method and this collection is non-empty. * * @throws UnsupportedOperationException if the <tt>clear</tt> method is * not supported by this collection. */ public void clear() { Iterator e = iterator(); while (e.hasNext()) { e.next(); e.remove(); } } // String conversion /** * Returns a string representation of this collection. The string * representation consists of a list of the collection's elements in the * order they are returned by its iterator, enclosed in square brackets * (<tt>"[]"</tt>). Adjacent elements are separated by the characters * <tt>", "</tt> (comma and space). Elements are converted to strings as * by <tt>String.valueOf(Object)</tt>.<p> * * This implementation creates an empty string buffer, appends a left * square bracket, and iterates over the collection appending the string * representation of each element in turn. After appending each element * except the last, the string <tt>", "</tt> is appended. Finally a right * bracket is appended. A string is obtained from the string buffer, and * returned. * * @return a string representation of this collection. */ public String toString() { StringBuffer buf = new StringBuffer(); buf.append("["); Iterator i = iterator(); boolean hasNext = i.hasNext(); while (hasNext) { Object o = i.next(); buf.append(o == this ? "(this Collection)" : String.valueOf(o)); hasNext = i.hasNext(); if (hasNext) buf.append(", "); } buf.append("]"); return buf.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -