Collection class, java Collection class
The green dotted lines in the figure represent implementation, the green solid lines represent inheritance between interfaces, and the blue solid lines represent inheritance between classes.
1) AbstractCollection: provides a large number of Collectin implementations.
Two Abstract METHODS:
Public abstract Iterator <E> iterator ();
Public abstract int size ();
2) random actlist: random access list. The Iterator and ListIterator iterators are implemented internally, including Itr and ListItr (inheriting Itr). ListItr has more forward and set operations.
Private class Itr implements Iterator <E> {
// Similar to the cursor int cursor = 0; // int lastRet =-1 in the last iteration; // modCount is a global variable, int expectedModCount = modCount; public boolean hasNext () {return cursor! = Size ();} public E next (){
// This method checks whether modCount and expectedModCount are equal, and whether there are concurrent modification operations checkForComodification (); try {int I = cursor;
// Get (I) is the abstract method E next = get (I );
// Record the location of the last iteration lastRet = I; cursor = I + 1; return next;} catch (IndexOutOfBoundsException e) {checkForComodification (); throw new NoSuchElementException ();}} public void remove () {if (lastRet <0) throw new IllegalStateException (); checkForComodification (); try {policactlist. this. remove (lastRet); if (lastRet <cursor) cursor --; lastRet =-1; expectedModCount = modCount;} catch (IndexOutOfBound SException e) {throw new ConcurrentModificationException () ;}} final void checkForComodification () {if (modCount! = ExpectedModCount) throw new ConcurrentModificationException ();}}
There are also two classes: SubList and RandomAccessSubList. What are the functions of these two classes?
In AbstractList, there is a method like subList.
public List<E> subList(int fromIndex, int toIndex) { return (this instanceof RandomAccess ? new RandomAccessSubList<>(this, fromIndex, toIndex) : new SubList<>(this, fromIndex, toIndex));}
RandomAccess: identifies whether a class supports random access. Here, it is used to determine whether the List is a RandomAccess instance.
RandomAccessSubList: only the RandomAccess ID is added. For more information, see SubList.
1 class RandomAccessSubList<E> extends SubList<E> implements RandomAccess {2 RandomAccessSubList(AbstractList<E> list, int fromIndex, int toIndex) {3 super(list, fromIndex, toIndex);4 }5 6 public List<E> subList(int fromIndex, int toIndex) {7 return new RandomAccessSubList<>(this, fromIndex, toIndex);8 }9 }
SubList: