Collection class, java Collection class

Source: Internet
Author: User

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:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.