Read AbstractList source

Foreword

AbstractList implement the List interface is an abstract class, abstract class and relationship AbstractList List interface similar to the relationship AbstractCollection Collection abstract classes and interfaces. AbstractList and AbstractCollection, just as some of the default method is achieved by simplifying our efforts to prepare a list of the List interface class required to pay.

Implement a list of classes to keep in mind:

1) To achieve an unmodifiable set subclassing the class, and implements get (int), size () method;
2) To implement a modifiable collection, you must also override the set (int, E) method, the default method throws an exception. If the set size is dynamically adjusted, must also be rewritten add (int, E), remove (int) Method

 

First, the class definition

public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E>

In addition to implementing the List interface, AbstractList also inherited AbstractCollection abstract class.

 

Second, the method definition

(A) Constructor

    protected AbstractList() {
    }

A null constructor to call the constructor subclass (usually implicit call)

Deletions (two) elements change search

(1)

    public boolean add(E e) {
        add(size(), e);
        return true;
    }

Adds the specified element to the end of the list. This method may throw an IndexOutOfBoundsException.

Internal method is implemented by calling the add (int, E) method.

(2)

    public void add(int index, E element) {
        throw new UnsupportedOperationException();
    }

Adds the specified element at the specified location to the list.

Internal method throws UnsupportedOperationException default. As the Preamble to speak, if we are to achieve an unmodifiable list of classes, then when calling Add method does not support adding operation is thrown exception; if we are to achieve a list of classes can be modified, you must override this method . After override this method, this method may throw ClassCastException, NullPointerException, IllegalArgumentException, IndexOutOfBoundsException other anomalies during the call.

(3)

    public E remove(int index) {
        throw new UnsupportedOperationException();
    }

Delete list specified element location.

Internal method throws UnsupportedOperationException default. For the same reason.

(4)

    public E set(int index, E element) {
        throw new UnsupportedOperationException();
    }

Element at the specified location with the updated list of specified elements

Internal method throws UnsupportedOperationException default. Same as above, if we are to achieve an unmodifiable list of classes, you do not need to implement this method; on the contrary, to override this method. After override this method, this method may also throw ClassCastException, NullPointerException, IllegalArgumentException, IndexOutOfBoundsException other abnormalities.

(5)

abstract public E get(int index);

Find a list element at the specified location. This method may throw an IndexOutOfBoundsException.

(6)

    public int indexOf(Object o) {
        ListIterator <E> it = listIterator ();
        if (o==null) {
            while (it.hasNext())
                if (it.next()==null)
                    return it.previousIndex();
        } else {
            while (it.hasNext())
                if (o.equals(it.next()))
                    return it.previousIndex();
        }
        return -1;
    }

Finds the index of the first occurrence of the specified element in the list.

Achieved by iterating over a list iterator.

(7)

    public int lastIndexOf(Object o) {
        ListIterator<E> it = listIterator(size());
        if (o==null) {
            while (it.hasPrevious())
                if (it.previous()==null)
                    return it.nextIndex();
        } else {
            while (it.hasPrevious())
                if (o.equals(it.previous()))
                    return it.nextIndex();
        }
        return -1;
    }

Find index of last occurrence of the specified element in the list.

Iteration achieved through a list iterator.

(C) the bulk operations

(1)

    public boolean addAll(int index, Collection<? extends E> c) {
        rangeCheckForAdd(index);
        boolean modified = false;
        for (E e : c) {
            add(index++, e);
            modified = true;
        }
        return modified;
    }

Adds the specified collection of all of the elements to the list.

First () method to determine the insertion position is legitimate, lawful add elements by rangeCheckForAdd; otherwise, an exception is thrown.

    private void rangeCheckForAdd(int index) {
        if (index < 0 || index > size())
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

When the index is out of range, rangeCheckForAdd () method throws an IndexOutOfBoundsException.

    private String outOfBoundsMsg(int index) {
        return "Index: "+index+", Size: "+size();
    }

When an exception is thrown, use outOfBoundsMsg () method prompts more information on exceptions.

(2)

    public void clear() {
        removeRange(0, size());
    }

Clear the list, namely, to delete a list of all the elements

RemoveRange, the interior is achieved by () method

    protected void removeRange(int fromIndex, int toIndex) {
        ListIterator<E> it = listIterator(fromIndex);
        for (int i=0, n=toIndex-fromIndex; i<n; i++) {
            it.next();
            it.remove();
        }
    }

(Iv) equal to the hash

(1)

    public boolean equals(Object o) {
        if (o == this)
            return true;
        if (!(o instanceof List))
            return false;
        ListIterator <E> e1 = listIterator ();
        ListIterator<?> e2 = ((List<?>) o).listIterator();
        while (e1.hasNext() && e2.hasNext()) {
            And e1.next o1 = ();
            Object o2 = e2.next();
            if (!(o1==null ? o2==null : o1.equals(o2)))
                return false;
        }
        return !(e1.hasNext() || e2.hasNext());
    }

Determining whether the object is equal to the specified list, the judgment here is equal to a value, instead of the same object.

Sentence like the following logic:

1) If the two references to an object is the same object, then the value is certainly equal, returns true; otherwise, continue to determine

2) If the specified object is not a list, such as specifying a set of objects is set, then return false; otherwise, continue to determine

3) Get a list, a list of the specified object iterator, through each element, and determines the position of the same element values ​​are equal, if there is a range, then return false; otherwise, all elements described in another iterator iterator occurs, and appears the same position, if one of the iterator is not present at the remaining elements, that directly returns true

(2)

    public int hashCode() {
        int hashCode = 1;
        for (E e : this)
            hashCode = 31*hashCode + (e==null ? 0 : e.hashCode());
        return hashCode;
    }

Get the hash code value list

Internal method implemented by traversing the list of each element, to obtain the hash value of each element of the code (if the element is null, then the hash code value 0; if not null, then by a method of acquiring element hashCode () Ha Xi code value), and then follow hashCode = 31 * hashCode + (e == null 0:? e.hashCode ()); hash code value calculating rule list, wherein the initial hashCode = 1. When all the elements traverse is completed, the hash code value of hashCode is the final list, it can return.

(E) the relevant iterator

(1)

    public Iterator<E> iterator() {
        return new Itr();
    }

 

(2)

    public ListIterator <E> listIterator () {
        return listIterator (0);
    }

 

(3)

    public ListIterator<E> listIterator(final int index) {
        rangeCheckForAdd(index);
        return new ListItr(index);
    }

 

Guess you like

Origin www.cnblogs.com/JeremyChan/p/11141232.html