Path: blob/master/src/java.base/share/classes/java/util/AbstractSet.java
41152 views
/*1* Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.util;2627/**28* This class provides a skeletal implementation of the {@code Set}29* interface to minimize the effort required to implement this30* interface. <p>31*32* The process of implementing a set by extending this class is identical33* to that of implementing a Collection by extending AbstractCollection,34* except that all of the methods and constructors in subclasses of this35* class must obey the additional constraints imposed by the {@code Set}36* interface (for instance, the add method must not permit addition of37* multiple instances of an object to a set).<p>38*39* Note that this class does not override any of the implementations from40* the {@code AbstractCollection} class. It merely adds implementations41* for {@code equals} and {@code hashCode}.<p>42*43* This class is a member of the44* <a href="{@docRoot}/java.base/java/util/package-summary.html#CollectionsFramework">45* Java Collections Framework</a>.46*47* @param <E> the type of elements maintained by this set48*49* @author Josh Bloch50* @author Neal Gafter51* @see Collection52* @see AbstractCollection53* @see Set54* @since 1.255*/5657public abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E> {58/**59* Sole constructor. (For invocation by subclass constructors, typically60* implicit.)61*/62protected AbstractSet() {63}6465// Comparison and hashing6667/**68* Compares the specified object with this set for equality. Returns69* {@code true} if the given object is also a set, the two sets have70* the same size, and every member of the given set is contained in71* this set. This ensures that the {@code equals} method works72* properly across different implementations of the {@code Set}73* interface.<p>74*75* This implementation first checks if the specified object is this76* set; if so it returns {@code true}. Then, it checks if the77* specified object is a set whose size is identical to the size of78* this set; if not, it returns false. If so, it returns79* {@code containsAll((Collection) o)}.80*81* @param o object to be compared for equality with this set82* @return {@code true} if the specified object is equal to this set83*/84public boolean equals(Object o) {85if (o == this)86return true;8788if (!(o instanceof Set))89return false;90Collection<?> c = (Collection<?>) o;91if (c.size() != size())92return false;93try {94return containsAll(c);95} catch (ClassCastException | NullPointerException unused) {96return false;97}98}99100/**101* Returns the hash code value for this set. The hash code of a set is102* defined to be the sum of the hash codes of the elements in the set,103* where the hash code of a {@code null} element is defined to be zero.104* This ensures that {@code s1.equals(s2)} implies that105* {@code s1.hashCode()==s2.hashCode()} for any two sets {@code s1}106* and {@code s2}, as required by the general contract of107* {@link Object#hashCode}.108*109* <p>This implementation iterates over the set, calling the110* {@code hashCode} method on each element in the set, and adding up111* the results.112*113* @return the hash code value for this set114* @see Object#equals(Object)115* @see Set#equals(Object)116*/117public int hashCode() {118int h = 0;119Iterator<E> i = iterator();120while (i.hasNext()) {121E obj = i.next();122if (obj != null)123h += obj.hashCode();124}125return h;126}127128/**129* Removes from this set all of its elements that are contained in the130* specified collection (optional operation). If the specified131* collection is also a set, this operation effectively modifies this132* set so that its value is the <i>asymmetric set difference</i> of133* the two sets.134*135* <p>This implementation determines which is the smaller of this set136* and the specified collection, by invoking the {@code size}137* method on each. If this set has fewer elements, then the138* implementation iterates over this set, checking each element139* returned by the iterator in turn to see if it is contained in140* the specified collection. If it is so contained, it is removed141* from this set with the iterator's {@code remove} method. If142* the specified collection has fewer elements, then the143* implementation iterates over the specified collection, removing144* from this set each element returned by the iterator, using this145* set's {@code remove} method.146*147* <p>Note that this implementation will throw an148* {@code UnsupportedOperationException} if the iterator returned by the149* {@code iterator} method does not implement the {@code remove} method.150*151* @param c collection containing elements to be removed from this set152* @return {@code true} if this set changed as a result of the call153* @throws UnsupportedOperationException if the {@code removeAll} operation154* is not supported by this set155* @throws ClassCastException if the class of an element of this set156* is incompatible with the specified collection157* (<a href="Collection.html#optional-restrictions">optional</a>)158* @throws NullPointerException if this set contains a null element and the159* specified collection does not permit null elements160* (<a href="Collection.html#optional-restrictions">optional</a>),161* or if the specified collection is null162* @see #remove(Object)163* @see #contains(Object)164*/165public boolean removeAll(Collection<?> c) {166Objects.requireNonNull(c);167boolean modified = false;168169if (size() > c.size()) {170for (Object e : c)171modified |= remove(e);172} else {173for (Iterator<?> i = iterator(); i.hasNext(); ) {174if (c.contains(i.next())) {175i.remove();176modified = true;177}178}179}180return modified;181}182183}184185186