My jdk source code: skeleton implementation class of abstractset family

Keywords: less JDK Java

1, Overview

AbstractSet class is the skeleton implementation class in Set family. It constructs a layer of abstraction between interface and implementation class. Its purpose is to reuse some common functions and facilitate extension, and provide common method templates for subclasses. The AbstractSet class has much less content than the AbstractList and AbstractMap classes.

2, Source code analysis

(1) class declaration. The source code is as follows:

public abstract class AbstractSet<E> extends AbstractCollection<E> implements Set<E>

AbstractSet class, which provides a skeleton of Set implementation, makes it possible to write a lot of the same code repeatedly when implementing the Set interface. As a collaboration, Set should also have some properties (or restrictions) and operations that the collaboration should have, and do not need to write by itself, directly inherit, and also to not write code repeatedly.

(2) the source code of the equeals () method is as follows:

    public boolean equals(Object o) {
        //Judge whether it is
        if (o == this)
            return true;
        //Judge whether it is Set type. If not, return false directly
        if (!(o instanceof Set))
            return false;
        //Parameter object to Collection
        Collection<?> c = (Collection<?>) o;
        //Compare capacity, return false if inconsistent
        if (c.size() != size())
            return false;
        //If all the above are satisfied, then compare the content
        try {
            //Call the containsAll() method of the AbstractCollection class to compare
            return containsAll(c);
        } catch (ClassCastException unused)   {
            return false;
        } catch (NullPointerException unused) {
            return false;
        }
    }

The logic of equals() method of AbstractSet class is relatively simple, similar to that of equals() of AbstractMap class. Only when comparing the content, the equals() method of AbstractSet class is the containsAll() method of AbstractCollection class which is called directly.

(3) hashCode() method, the source code is as follows:

    public int hashCode() {
        int h = 0;
        Iterator<E> i = iterator();
        while (i.hasNext()) {
            E obj = i.next();
            if (obj != null)
                h += obj.hashCode();
        }
        return h;
    }

The hash value of AbstractSet is to add the hash values of all elements (objects), ensuring that the same Set must have the same hash value, which is consistent with the AbstractMap class.

(4) removeAll() method, the source code is as follows:

    //Remove all elements contained in the parameter from this Set
    public boolean removeAll(Collection<?> c) {
        Objects.requireNonNull(c);
        //Whether the mark is modified
        boolean modified = false;

        if (size() > c.size()) {
            for (Iterator<?> i = c.iterator(); i.hasNext(); )
                //If there are few elements in the parameter, then all elements in the parameter call remove to find. If they are found and deleted, modified will change to true
                modified |= remove(i.next());
        } else {
            for (Iterator<?> i = iterator(); i.hasNext(); ) {
                //There are few elements in the Set. Iterate over the elements of the Set to see whether they are included in the elements of the parameter. If so, remove
                if (c.contains(i.next())) {
                    i.remove();
                    modified = true;
                }
            }
        }
        return modified;
    }

3, Summary

The AbstractSetl class has few methods. It depends on its subclass. Please look forward to my jdk source code (11): ArrayList class. For more exciting content, please scan the bottom two dimensional code, and pay attention to my WeChat official account, Java.

Posted by colB on Sun, 24 May 2020 07:31:30 -0700