< prev index next >

src/java.base/share/classes/java/util/AbstractSet.java

Print this page
rev 48431 : 8193128: Reduce number of implementation classes returned by List/Set/Map.of()
8191418: List.of().indexOf(null) doesn't throw NullPointerException
Reviewed-by: smarks, jrose, martin, plevart


  76      * This implementation first checks if the specified object is this
  77      * set; if so it returns {@code true}.  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      * {@code containsAll((Collection) o)}.
  81      *
  82      * @param o object to be compared for equality with this set
  83      * @return {@code true} if the specified object is equal to this set
  84      */
  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 {@code null} element is defined to be zero.
 107      * This ensures that {@code s1.equals(s2)} implies that
 108      * {@code s1.hashCode()==s2.hashCode()} for any two sets {@code s1}
 109      * and {@code s2}, as required by the general contract of
 110      * {@link Object#hashCode}.
 111      *
 112      * <p>This implementation iterates over the set, calling the
 113      * {@code hashCode} 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)




  76      * This implementation first checks if the specified object is this
  77      * set; if so it returns {@code true}.  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      * {@code containsAll((Collection) o)}.
  81      *
  82      * @param o object to be compared for equality with this set
  83      * @return {@code true} if the specified object is equal to this set
  84      */
  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 | NullPointerException unused) {


  97             return false;
  98         }
  99     }
 100 
 101     /**
 102      * Returns the hash code value for this set.  The hash code of a set is
 103      * defined to be the sum of the hash codes of the elements in the set,
 104      * where the hash code of a {@code null} element is defined to be zero.
 105      * This ensures that {@code s1.equals(s2)} implies that
 106      * {@code s1.hashCode()==s2.hashCode()} for any two sets {@code s1}
 107      * and {@code s2}, as required by the general contract of
 108      * {@link Object#hashCode}.
 109      *
 110      * <p>This implementation iterates over the set, calling the
 111      * {@code hashCode} method on each element in the set, and adding up
 112      * the results.
 113      *
 114      * @return the hash code value for this set
 115      * @see Object#equals(Object)
 116      * @see Set#equals(Object)


< prev index next >