Reading notes Source List

Foreword

List interface is one of the sub-interface interface Collection, Collection offers a collection of major general method, while List is a list of jdk for abstract / linear meter of this more specific collections. List data structure has the following characteristics:

1) elements can be retrieved by accessible location;

2) the user can insert data at the specified location;

3) elements may be null, can be repeated;

Is between 4) having opposed longitudinal elements between the order ordered (linear table element), and therefore a Class List are ordered collection set.

 

First, the class definition

public interface List<E> extends Collection<E>

Second, the method definition

Since it is inherited List Collection interface sub-interface, we mainly analyze its unique method can be. But also rewrote Collection List of spliterator () method:

    @Override
    default Spliterator<E> spliterator() {
        return Spliterators.spliterator(this, Spliterator.ORDERED);
    }

The method of setting the element characteristics Spliterator Spliterator.ORDERED, Collection of different.

Next, look at the unique method of List, List table for the linear structure is provided an operating position and a sub-table.

Additions (a) change search element

(1)

void add(int index, E element);

Specified element added to the list at the specified position, and all the elements at the position moved to the right rear of the specified element (i.e., they are 1's index)

When the list does not support this operation, it throws UnsupportedOperationException (In other words, this operation is optional);

When the specified element is prohibited to add to this collection, throws ClassCastException;

When the specified element is null and this collection does not support null elements Throws NullPointerException;

When certain characteristics of the specified block the elements are added to the collection, an IllegalArgumentException is thrown;

When the index is out of the specified range of the set (index <0 or index> size ()), an IndexOutOfBoundsException

(2)

E remove(int index);

Remove the specified positions of the elements in the list, all of the elements at a position behind and to the left of a mobile (i.e., their index minus 1)

When the list does not support this operation, it throws UnsupportedOperationException (In other words, this operation is optional);

When the index is out of the specified range of the set (index <0 or index> size ()), an IndexOutOfBoundsException

(3)

E set(int index, E element);

Update list element with the specified element at the specified position, and returns the old element at the position (element is replaced)

When the list does not support this operation, it throws UnsupportedOperationException (In other words, this operation is optional);

When the specified element is prohibited to add to this collection, throws ClassCastException;

When the specified element is null and this collection does not support null elements Throws NullPointerException;

When certain characteristics of the specified block the elements are added to the collection, an IllegalArgumentException is thrown;

When the index is out of the specified range of the set (index <0 or index> size ()), an IndexOutOfBoundsException

(4)

E get(int index);

Returns the element at the specified position in the list

When the index is out of the specified range of the set (index <0 or index> size ()), an IndexOutOfBoundsException

(5)

int indexOf(Object o);

Index query specified element first appears in the list

If there are specified element specifying the index of the first occurrence of an element in the list is returned; if not, or -1

In other words, this method returns either meet o == null get (i) == null:? O.equals (get (i)) the lowest index was established, or is no element of the above conditions are met, or -1

When the specified element is incompatible with the list, throws ClassCastException;

When the specified element is null and this list does not support null elements, is thrown NullPointerException.

(6)

int lastIndexOf(Object o);

The last time the index query specified element appears in the list

If this element is present, the last location of the specified elements that appear in the list is returned; if this element is not present, directly -1
In other words, this method returns either meet o == null get (i) == null:? O.equals (get (i)) the highest index was established, or is no element of the above conditions are met, or -1

When the specified element is incompatible with the list, throws ClassCastException;

When the specified element is null and this list does not support null elements, is thrown NullPointerException.

(B) bulk editing

(1)

boolean addAll(int index, Collection<? extends E> c);

Inserting all of the elements of the set at a prescribed position, and a certain increase in the size and all elements behind the index at the specified position, add specified in an iterative sequence iterator set when these elements are added.

When this method is called, the contents of the collection is changed, this method returns true.

We know, List interface extends the Collection interface method many bulk operations, such as, removeAll (), clear (), addAll (). addAll here () method in comparison with the inherited addAll () method, one more index parameter index, which is designated batch inserted. This is consistent with the characteristics of an orderly linear form, but this method is because there is no Collection Collection interface is an ordered collection interfaces total unordered collection of unordered set no index, so the index can not have associated methods Collection.

Note: When the procedure calls this method, while the specified collection is modified, then the result will not be controlled. For example, this method is called a set of all the elements of the collection insert itself, and this set is not empty.

When the list does not support this operation, throws UnsupportedOperationException;

When the elements of the specified collection is prohibited to add to this collection, throws ClassCastException;

When the specified collection contains null elements and this set does not support null elements, or specified set is null, a NullPointerException is thrown;

When certain characteristics of a set of specified element prevents the element is added to the collection, an IllegalArgumentException is thrown;

When the index is out of the specified range of the set (index <0 or index> size ()), an IndexOutOfBoundsException

(2)

    
    default void replaceAll(UnaryOperator<E> operator) {
        Objects.requireNonNull(operator);
        final ListIterator<E> li = this.listIterator();
        while (li.hasNext()) {
            li.set(operator.apply(li.next()));
        }
    }

The default implementation of the method, each element traversed by the iterator, and they are specified operation, and the result of the operation to replace the original elements.

Since the process is in place inside () method implemented by a set list iterator, so if the list if the list iterator does not support the set () operation, then throws UnsupportedOperationException.

(C) Other methods

(1)

    @SuppressWarnings({"unchecked", "rawtypes"})
    default void sort(Comparator<? super E> c) {
        Object[] a = this.toArray();
        Arrays.sort(a, (Comparator) c);
        ListIterator<E> i = this.listIterator();
        for (Object e : a) {
            i.next();
            i.set((E) e);
        }
    }

All elements of the list to be sorted.

This method sorts the list according to all of the elements specified in the current rule (specified comparator). It can be seen sorting process () method implemented by the sort Arrays. Further, sorting is performed in a new array using toArray () Returns above, then use the list iterator set () method, each element of the new array sorted into eleven update at the corresponding position in the list.

(2)

ListIterator <E> listIterator ();

Returns a list iterator over the elements of the list

ListIterator inherited from the Iterator, the main increase forward traversal function, as well as collection element set () operations update
(3)

ListIterator <E> listIterator (int index);

Returns the specified position from a beginning (default above method starting from 0), the list iterators above list elements

When the list is after the iterator is created, the first call to the list iterator pointing to the specified location next () method, the position of the first element returned. At the same time, the specified position is the first call of the list iterator previous () method when the position of the first element of the returned minus 1.

We can see, List interfaces in addition to the iterator inherited from the Collection interface () method, as well as listIterator () method, which is to meet the characteristics before and after traversing the linear form of the abstract operation.

(4)

List<E> subList(int fromIndex, int toIndex);

Returns a list of a subset in [fromIndex, toIndex) range, the original list of supported return a list of all operations. If fromIndex equal toIndex, it returns an empty list.

Sub-list is actually a return to the original list of view, that is still a shared data with the original list. Because the underlying data is shared, so the list of non-structural changes (e.g., updates the value of an element) will be reflected in the child list is returned; and vice versa, if the sub-list non-structural change occurs, then the original list also You will be changed.

This approach eliminates the explicit range operations, range operations requires any list are sub-lists can be used to call this method returns, rather than the direct operation of the entire list. For example: list.subList (from, to) .clear (); you can delete the list of elements between a range of positions.

The above example is modified by structural sub-list returned list (like list size change operation is a structural modification), however, if the original list structure modified in any manner, not by the child list is returned, then by semantic sub list returned by this method will become undefined (unclear).

When the index is out of the specified range of the set (index <0 or index> size ()), throw IndexOutOfBoundsException.

summary:

1, a so-called non-structural modification means to modify the size does not change the list. In contrast, structural modification refers to changing the size of the modified list.
1) non-structural changes will affect each other;
2) As for the structural modifications:
If there is a structural modification is a sub-list, the size of the list may also change;
If there is a structural modification of the original list (the list does not include the change due to the return of the child due), then the child is a list of semantically undefined.
In AbstractList (ArrayList parent, List subclass), the concrete manifestation of undefined is to throw a ConcurrentModificationException
Therefore, if you call after a sublist return to the sub-list, if you modify the size of the original list, then the sub-list will be generated before the failure becomes unavailable

2, and the original sub-list where the list is a bit of an outer relationship database and a mode three mode: mode is the logical structure of the data model of the entire application, including all data, the pattern is a partial view of the outer pattern, different applications / modules can only see the outer functional mode different from the corresponding data ranges.

 

Guess you like

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