On the question of list set and set set set, please instruct the great god

  java, question

When Daniel list and set call the contains method, do they compare the addresses of the two elements or call their equals method?

public class Foo {
 int value;
 public Foo(int value) {
 this.value = value;
 bracket
 public boolean equals(Object obj) {
 if (obj instanceof Foo) {
 Foo foo = (Foo) obj;
 return value == foo.value;
 } else {
 return false;
 bracket
 bracket
 public static void main(String[] args) {
 //Run the following program segment:
 ArrayList list = new ArrayList();
 HashSet set = new HashSet();
 list.add(new Foo(1));
 set.add(new Foo(1));
 System.out.println (list.contains (newfoo (1)) plus ","add set.contains(new Foo(1)));
 //or
 Out.println (newfoo (1). equals (newfoo (1)) plus ","plus set.contains(new Foo(1)));
 bracket
 bracket

What I want to ask is why the output is true,false

I checked the source code, I thinkHashSetTheHashThere are articles here.

  • 1. First look at the process of adding elements
    //HashSet code

/**
     * Adds the specified element to this set if it is not already present.
     * More formally, adds the specified element <tt>e</tt> to this set if
     * this set contains no element <tt>e2</tt> such that
     * <tt>(e==null&nbsp;?&nbsp;e2==null&nbsp;:&nbsp;e.equals(e2))</tt>.
     * If this set already contains the element, the call leaves the set
     * unchanged and returns <tt>false</tt>.
     *
     * @param e element to be added to this set
     * @return <tt>true</tt> if this set did not already contain the specified
     * element
     */
    public boolean add(E e) {
        return map.put(e, PRESENT)==null;
    }

ToHashSetInternally maintainedMapAdd new element
//HashMap code

/**
     * Associates the specified value with the specified key in this map.
     * If the map previously contained a mapping for the key, the old
     * value is replaced.
     *
     * @param key key with which the specified value is to be associated
     * @param value value to be associated with the specified key
     * @return the previous value associated with <tt>key</tt>, or
     *         <tt>null</tt> if there was no mapping for <tt>key</tt>.
     *         (A <tt>null</tt> return can also indicate that the map
     *         previously associated <tt>null</tt> with <tt>key</tt>.)
     */
    public V put(K key, V value) {
        if (table == EMPTY_TABLE) {
            inflateTable(threshold);
        }
        if (key == null)
            return putForNullKey(value);
        int hash = hash(key);
        int i = indexFor(hash, table.length);
        for (Entry<K,V> e = table[i]; e != null; e = e.next) {
            Object k;
            if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
                V oldValue = e.value;
                e.value = value;
                e.recordAccess(this);
                return oldValue;
            }
        }

        modCount加加;
        addEntry(hash, key, value, i);
        return null;
    }

It can be seen that it will be rightElementDo hash, so if you addFoo(1)If so, save it toHashSetThis link is also indispensable in the process.

    1. Let’s look at the comparison
      //HashSet code

/**
     * Returns <tt>true</tt> if this set contains the specified element.
     * More formally, returns <tt>true</tt> if and only if this set
     * contains an element <tt>e</tt> such that
     * <tt>(o==null&nbsp;?&nbsp;e==null&nbsp;:&nbsp;o.equals(e))</tt>.
     *
     * @param o element whose presence in this set is to be tested
     * @return <tt>true</tt> if this set contains the specified element
     */
    public boolean contains(Object o) {
        return map.containsKey(o);
    }

//HashMap code

/**
 * Returns <tt>true</tt> if this map contains a mapping for the
 * specified key.
 *
 * @param   key   The key whose presence in this map is to be tested
 * @return <tt>true</tt> if this map contains a mapping for the specified
 * key.
 */
 public boolean containsKey(Object key) {
 return getEntry(key) !  = null;
 bracket
/**
     * Returns the entry associated with the specified key in the
     * HashMap.  Returns null if the HashMap contains no mapping
     * for the key.
     */
    final Entry<K,V> getEntry(Object key) {
        if (size == 0) {
            return null;
        }

        int hash = (key == null) ? 0 : hash(key);
        for (Entry<K,V> e = table[indexFor(hash, table.length)];
             e != null;
             e = e.next) {
            Object k;
            if (e.hash == hash &&
                ((k = e.key) == key || (key != null && key.equals(k))))
                return e;
        }
        return null;
    }
    1. Conclusion
      InsertHashSetWhen the container hashes the element, it also hashes the element when judging whether the container contains an element. They compare the hash values of the element.