Source code analysis of shortlist

Source: Internet
Author: User

Simple source code analysis

 

Statement List

View plaincopy to clipboardprint? Public class category list <E>
Extends AbstractSequentialList <E>
Implements List <E>, Deque <E>/* This is a dual-end Queue interface. This interface extends the Queue interface and provides more methods, such as push and pop */, cloneable, java. io. serializable
Public class category list <E>
Extends AbstractSequentialList <E>
Implements List <E>, Deque <E>/* This is a dual-end Queue interface. This interface extends the Queue interface and provides more methods, such as push and pop */, cloneable, java. io. serializable

 

Therefore, the Queue list can be used as a Stack, Queue, and Deque.

 

Let's take a look at the definition of linked list nodes.

View plaincopy to clipboardprint? Private static class Entry <E> {
E element;
Entry <E> next;
Entry <E> previous; // you can see that the sorted list is a two-way linked list.

Entry (E element, Entry <E> next, Entry <E> previous ){
This. element = element;
This. next = next;
This. previous = previous;
}
Private static class Entry <E> {
E element;
Entry <E> next;
Entry <E> previous; // you can see that the sorted list is a two-way linked list.
 
Entry (E element, Entry <E> next, Entry <E> previous ){
This. element = element;
This. next = next;
This. previous = previous;
}


The following two instance variables are declared in the explain list:

// Header node, which is used for marking and does not record Elements

Private transient Entry <E> header = new Entry <E> (null, null, null );

// The size of the linked list, that is, the number of elements in the linked list

Private transient int size = 0;

 

We can see that both of these are declared as transient, so they will be ignored during serialization, but the serialization method writeObject (java. io. in ObjectOutputStream s), the size is serialized and all nodes except the header are written to the serialization file. Why should we declare the size as a transient .. ..

 

Several important methods:

View plaincopy to clipboardprint? /**
* Returns the indexed entry.
Determines whether to traverse the table from the beginning or from the end based on the given index value.
*/
Private Entry <E> entry (int index ){
If (index <0 | index> = size)
Throw new IndexOutOfBoundsException ("Index:" + index +
", Size:" + size );
Entry <E> e = header;
If (index <(size> 1) {// if it is closer to a header
For (int I = 0; I <= index; I ++)
E = e. next;
} Else {// closer to the end of the table
For (int I = size; I> index; I --)
E = e. previous;
}
Return e;
}
/**
* Returns the indexed entry.
Determines whether to traverse the table from the beginning or from the end based on the given index value.
*/
Private Entry <E> entry (int index ){
If (index <0 | index> = size)
Throw new IndexOutOfBoundsException ("Index:" + index +
", Size:" + size );
Entry <E> e = header;
If (index <(size> 1) {// if it is closer to a header
For (int I = 0; I <= index; I ++)
E = e. next;
} Else {// closer to the end of the table
For (int I = size; I> index; I --)
E = e. previous;
}
Return e;
}

View plaincopy to clipboardprint? /**
* Before adding element e to the entry Node
*/
Private Entry <E> addBefore (E e, Entry <E> entry ){
Entry <E> newEntry = new Entry <E> (e, entry, entry. previous );
NewEntry. previous. next = newEntry; // connect the new node with the front and back nodes.
NewEntry. next. previous = newEntry;
Size ++;
ModCount ++;
Return newEntry;
}
 
 
/**
* Delete a given node e.
*/
Private E remove (Entry <E> e ){
If (e = header)
Throw new NoSuchElementException ();
 
E result = e. element;
E. previous. next = e. next;
E. next. previous = e. previous;
E. next = e. previous = null;
E. element = null;
Size --;
ModCount ++;
Return result;
}
 
 
 
/**
* The traversal starts from the header and returns the first position of the element in the table.
*/
Public int indexOf (Object o ){
Int index = 0;
If (o = null) {// if the input element is null, you cannot call the eqauls Method for comparison.
For (Entry e = header. next; e! = Header; e = e. next ){
If (e. element = null)
Return index;
Index ++;
}
} Else {
For (Entry e = header. next; e! = Header; e = e. next ){
If (o. equals (e. element ))
Return index;
Index ++;
}
}
Return-1;
}
 
/**
* By default, the new element is added to the end of the table.
*/
Public boolean add (E e ){
AddBefore (e, header); // Add it to the header node, that is, the end of the table.
Return true;
}
 
/**
* The default Delete action is to delete the first element of the linked list. Therefore, by default, the shortlist function is actually a queue role.
*/
Public E remove (){
Return removeFirst ();
}
 
/**
* Returns the first element.
*/
Public E peek (){
If (size = 0)
Return null;
Return getFirst ();
}
/**
* Before adding element e to the entry Node
*/
Private Entry <E> addBefore (E e, Entry <E> entry ){
Entry <E> newEntry = new Entry <E> (e, entry, entry. previous );
NewEntry. previous. next = newEntry; // connect the new node with the front and back nodes.
NewEntry. next. previous = newEntry;
Size ++;
ModCount ++;
Return newEntry;
}


/**
* Delete a given node e.
*/
Private E remove (Entry <E> e ){
If (e = header)
Throw new NoSuchElementException ();

E result = e. element;
E. previous. next = e. next;
E. next. previous = e. previous;
E. next = e. previous = null;
E. element = null;
Size --;
ModCount ++;
Return result;
}

 

/**
* The traversal starts from the header and returns the first position of the element in the table.
*/
Public int indexOf (Object o ){
Int index = 0;
If (o = null) {// if the input element is null, you cannot call the eqauls Method for comparison.
For (Entry e = header. next; e! = Header; e = e. next ){
If (e. element = null)
Return index;
Index ++;
}
} Else {
For (Entry e = header. next; e! = Header; e = e. next ){
If (o. equals (e. element ))
Return index;
Index ++;
}
}
Return-1;
}

/**
* By default, the new element is added to the end of the table.
*/
Public boolean add (E e ){
AddBefore (e, header); // Add it to the header node, that is, the end of the table.
Return true;
}

/**
* The default Delete action is to delete the first element of the linked list. Therefore, by default, the shortlist function is actually a queue role.
*/
Public E remove (){
Return removeFirst ();
}

/**
* Returns the first element.
*/
Public E peek (){
If (size = 0)
Return null;
Return getFirst ();
}


It can be seen that if the table is empty, this method does not throw an exception, but returns null, while the traditional (declared in Collections) method throws an exception. Similar methods include poll, but note that the pop method throws an exception when the table is empty.

 

Note that if you first return the list Iterator and then add, delete, and modify the linked list, then the returned Iterator will throw ConcurrentModificationException.

 

 

Finally, let's take a look at how the serialization list is serialized and deserialized. If you have doubts about the two callback methods used in the two deserialization, you can view my blog http://blog.csdn.net/moreevan/article/details/6697777.

View plaincopy to clipboardprint? /**
* Save the state of this <tt> parameter list </tt> instance to a stream (that
* Is, serialize it ).
*
* @ SerialData The size of the list (the number of elements it
* Contains) is emitted (int), followed by all of its
* Elements (each an Object) in the proper order.
*/
Private void writeObject (java. io. ObjectOutputStream s)
Throws java. io. IOException {
// Write out any hidden serialization magic
S. defaultWriteObject ();
 
// Write out size
S. writeInt (size );
 
// Write out all elements in the proper order.
For (Entry e = header. next; e! = Header; e = e. next)
S. writeObject (e. element );
}
 
/**
* Reconstitute this <tt> shard list </tt> instance from a stream (that is
* Deserialize it ).
*/
Private void readObject (java. io. ObjectInputStream s)
Throws java. io. IOException, ClassNotFoundException {
// Read in any hidden serialization magic
S. defaultReadObject ();
 
// Read in size
Int size = s. readInt ();
 
// Initialize header
Header = new Entry <E> (null, null, null );
Header. next = header. previous = header;
 
// Read in all elements in the proper order.
For (int I = 0; I <size; I ++)
AddBefore (E) s. readObject (), header );
}
/**
* Save the state of this <tt> parameter list </tt> instance to a stream (that
* Is, serialize it ).
*
* @ SerialData The size of the list (the number of elements it
* Contains) is emitted (int), followed by all of its
* Elements (each an Object) in the proper order.
*/
Private void writeObject (java. io. ObjectOutputStream s)
Throws java. io. IOException {
// Write out any hidden serialization magic
S. defaultWriteObject ();

// Write out size
S. writeInt (size );

// Write out all elements in the proper order.
For (Entry e = header. next; e! = Header; e = e. next)
S. writeObject (e. element );
}

/**
* Reconstitute this <tt> shard list </tt> instance from a stream (that is
* Deserialize it ).
*/
Private void readObject (java. io. ObjectInputStream s)
Throws java. io. IOException, ClassNotFoundException {
// Read in any hidden serialization magic
S. defaultReadObject ();

// Read in size
Int size = s. readInt ();

// Initialize header
Header = new Entry <E> (null, null, null );
Header. next = header. previous = header;

// Read in all elements in the proper order.
For (int I = 0; I <size; I ++)
AddBefore (E) s. readObject (), header );
}

In summary, we can see that. A two-way linked list can be used as a stack, queue, or double-end queue. It also provides a random access element method, but the time complexity of this access is not as complex as that of the ArrayList is O (1), but O (n ). It deletes elements at a given position.

The efficiency is not higher than that of ArrayList. However, it is more efficient than ArrayList to delete a specific element.

Author: "KevinJom's column"
 

 


 

Related Article

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.