List of collection

Source: Internet
Author: User

Here all the screenshots of the source code from JDK8

Look at a picture


First look at the basic interface of the collection class: Collection, which inherits the Iterable interface.


Look at the source of the time found this interface, since there is a method body,


This is due to the new features in JDK8: interfaces allow the definition of non-abstract methods, but they must be decorated with default.

There are two basic methods in collection.

Add (e e); with iterator<e> iterator ();

Add is adding an element to the collection, returning true successfully, and failing to return false (but there is no duplicate object allowed in this collection)

The iterator method is used to return an object of the iterator interface


This interface object has 3 of the methods we know: Hasnext,next,remove.

The next method allows us to individually access each element of the collection, but if we reach the end of the collection, the next method will have a nosuchelementexception exception, so we need to call the Hasnext method, Repeatedly call next to implement the traversal of the collection when Hasnext returns True (we can also see that the array accesses the element in the same way that the subscript is used, and the collection uses iterators). Let's take a look at an internal class in ArrayList to implement the iterator interface ITR


If the i>=size description has reached the end of the collection.

The concurrentmodificationexception exception may be because the external remove method modifies the value of the Modcount value modified by the Expectedmodcount
There has been a situation of inconsistency.

And then there's this phrase in the Java Core Technology book: Java iterators Find operations that are tightly connected to position changes, and the only way to find an element is by calling Next,
While performing a lookup operation, the position of the iterator moves forward as well, so the iterator should be considered to be between two elements, and when next is called, the iteration
The device crosses the next element and returns a reference that crosses the element.
The iterator interface Remove method deletes the element returned by the last call to the next method. So: If you delete two connected elements we need to call next to return the need to delete
The object.
Here's a description of the specific collection:
Up to now, the most ArrayList class you can use (dynamically growing and shrinking index sequences)
It inherits the Abstractlist class, and this abstract class inherits the Abstractcollection class, because collection has many methods, but every class is
Implementing so many methods loses its original meaning, so providing an abstract class to better implement collection this interface.
The disadvantage of ArrayList is also a flaw in the array: The cost of a river is paid for the removal of the operation, because the element after the deletion
All need to move forward, the insertion will have the same problem. But the list does not have this problem because each node in the list is doubly linked (the array is in a contiguous bit
Put the reference to the host object, it is because the node holds the reference to the predecessor, so it is very easy to delete and insert in the list, because only need to update the nearby
A reference to a node.
The book says: But the list is an ordered set, each object's position is very important, so,
The Linkedlist.add method inserts a value at the end of the list each time:


  You can see from the Linklast method that the new node is linked together at the last node each time, and if last = = NULL, it is the
  So how do you insert an element into the middle of a list, using an iterator (Listiterator), because the iterator is used to represent the position in the collection, so this dependent position
  The Add method should be responsible for the iterator
  There is a Listiterator method under the list interface.

This allows us to implement the LinkedList insert operation.

But the reality is:

I found that the Add method in LinkedList contains methods for adding value based on subscript (which may not be accurate), so look at the source code:


is if the insertion position is at the end, then the Linklast method is called directly to insert the end, but if it is not the end, then we need to perform some action:

First look at node (index): if and else are judged by the traversal way or from the end of the head.


Insert operation based on node position (in fact, the quality is still dependent on the position of the subscript)


Disadvantage of LinkedList: it's inefficient for random access traversal lists: Because every time you look up an element, you need to start searching from the head of the list. LinkedList objects

Does not do any caching of location information at all.

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.