1 /* 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package java.util; 27 28 /** 29 * This class provides a skeletal implementation of the <tt>Set</tt> 30 * interface to minimize the effort required to implement this 31 * interface. <p> 32 * 33 * The process of implementing a set by extending this class is identical 34 * to that of implementing a Collection by extending AbstractCollection, 35 * except that all of the methods and constructors in subclasses of this 36 * class must obey the additional constraints imposed by the <tt>Set</tt> 37 * interface (for instance, the add method must not permit addition of 38 * multiple instances of an object to a set).<p> 39 * 40 * Note that this class does not override any of the implementations from 41 * the <tt>AbstractCollection</tt> class. It merely adds implementations 42 * for <tt>equals</tt> and <tt>hashCode</tt>.<p> 43 * 44 * This class is a member of the 45 * <a href="https://docs.oracle.com/javase/8/docs/technotes/guides/collections/index.html"> 46 * Java Collections Framework</a>. 47 * 48 * @param <E> the type of elements maintained by this set 49 * 50 * @author Josh Bloch 51 * @author Neal Gafter 52 * @see Collection 53 * @see AbstractCollection 54 * @see Set 55 * @since 1.2 56 */ 57 58 public abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E> { 59 /** 60 * Sole constructor. (For invocation by subclass constructors, typically 61 * implicit.) 62 */ AbstractSet()63 protected AbstractSet() { 64 } 65 66 // Comparison and hashing 67 68 /** 69 * Compares the specified object with this set for equality. Returns 70 * <tt>true</tt> if the given object is also a set, the two sets have 71 * the same size, and every member of the given set is contained in 72 * this set. This ensures that the <tt>equals</tt> method works 73 * properly across different implementations of the <tt>Set</tt> 74 * interface.<p> 75 * 76 * This implementation first checks if the specified object is this 77 * set; if so it returns <tt>true</tt>. Then, it checks if the 78 * specified object is a set whose size is identical to the size of 79 * this set; if not, it returns false. If so, it returns 80 * <tt>containsAll((Collection) o)</tt>. 81 * 82 * @param o object to be compared for equality with this set 83 * @return <tt>true</tt> if the specified object is equal to this set 84 */ equals(Object o)85 public boolean equals(Object o) { 86 if (o == this) 87 return true; 88 89 if (!(o instanceof Set)) 90 return false; 91 Collection<?> c = (Collection<?>) o; 92 if (c.size() != size()) 93 return false; 94 try { 95 return containsAll(c); 96 } catch (ClassCastException unused) { 97 return false; 98 } catch (NullPointerException unused) { 99 return false; 100 } 101 } 102 103 /** 104 * Returns the hash code value for this set. The hash code of a set is 105 * defined to be the sum of the hash codes of the elements in the set, 106 * where the hash code of a <tt>null</tt> element is defined to be zero. 107 * This ensures that <tt>s1.equals(s2)</tt> implies that 108 * <tt>s1.hashCode()==s2.hashCode()</tt> for any two sets <tt>s1</tt> 109 * and <tt>s2</tt>, as required by the general contract of 110 * {@link Object#hashCode}. 111 * 112 * <p>This implementation iterates over the set, calling the 113 * <tt>hashCode</tt> method on each element in the set, and adding up 114 * the results. 115 * 116 * @return the hash code value for this set 117 * @see Object#equals(Object) 118 * @see Set#equals(Object) 119 */ hashCode()120 public int hashCode() { 121 int h = 0; 122 Iterator<E> i = iterator(); 123 while (i.hasNext()) { 124 E obj = i.next(); 125 if (obj != null) 126 h += obj.hashCode(); 127 } 128 return h; 129 } 130 131 /** 132 * Removes from this set all of its elements that are contained in the 133 * specified collection (optional operation). If the specified 134 * collection is also a set, this operation effectively modifies this 135 * set so that its value is the <i>asymmetric set difference</i> of 136 * the two sets. 137 * 138 * <p>This implementation determines which is the smaller of this set 139 * and the specified collection, by invoking the <tt>size</tt> 140 * method on each. If this set has fewer elements, then the 141 * implementation iterates over this set, checking each element 142 * returned by the iterator in turn to see if it is contained in 143 * the specified collection. If it is so contained, it is removed 144 * from this set with the iterator's <tt>remove</tt> method. If 145 * the specified collection has fewer elements, then the 146 * implementation iterates over the specified collection, removing 147 * from this set each element returned by the iterator, using this 148 * set's <tt>remove</tt> method. 149 * 150 * <p>Note that this implementation will throw an 151 * <tt>UnsupportedOperationException</tt> if the iterator returned by the 152 * <tt>iterator</tt> method does not implement the <tt>remove</tt> method. 153 * 154 * @param c collection containing elements to be removed from this set 155 * @return <tt>true</tt> if this set changed as a result of the call 156 * @throws UnsupportedOperationException if the <tt>removeAll</tt> operation 157 * is not supported by this set 158 * @throws ClassCastException if the class of an element of this set 159 * is incompatible with the specified collection 160 * (<a href="Collection.html#optional-restrictions">optional</a>) 161 * @throws NullPointerException if this set contains a null element and the 162 * specified collection does not permit null elements 163 * (<a href="Collection.html#optional-restrictions">optional</a>), 164 * or if the specified collection is null 165 * @see #remove(Object) 166 * @see #contains(Object) 167 */ removeAll(Collection<?> c)168 public boolean removeAll(Collection<?> c) { 169 Objects.requireNonNull(c); 170 boolean modified = false; 171 172 if (size() > c.size()) { 173 for (Iterator<?> i = c.iterator(); i.hasNext(); ) 174 modified |= remove(i.next()); 175 } else { 176 for (Iterator<?> i = iterator(); i.hasNext(); ) { 177 if (c.contains(i.next())) { 178 i.remove(); 179 modified = true; 180 } 181 } 182 } 183 return modified; 184 } 185 186 } 187