Java 7 source code analysis part 1-List set implementation based on linked List

Source: Internet
Author: User
Tags addall

The storage speed of linked list-based collections is definitely not as high as that of array-based collections. However, the biggest advantage of linked list implementation is that it is faster when nodes are frequently operated, for example, to delete a node, you do not need to call System like an array. the arraycopy () method moves the entire left. Let's take a look at the AbstractSequencialList abstract class:

public abstract class AbstractSequentialList
 
   extends AbstractList
  
    {    protected AbstractSequentialList() {    }    public E get(int index) {        try {            return listIterator(index).next();        } catch (NoSuchElementException exc) {            throw new IndexOutOfBoundsException("Index: "+index);        }    }    public E set(int index, E element) {        try {            ListIterator
   
     e = listIterator(index);            E oldVal = e.next();            e.set(element);            return oldVal;        } catch (NoSuchElementException exc) {            throw new IndexOutOfBoundsException("Index: "+index);        }    }    public void add(int index, E element) {        try {            listIterator(index).add(element);        } catch (NoSuchElementException exc) {            throw new IndexOutOfBoundsException("Index: "+index);        }    }    public E remove(int index) {        try {            ListIterator
    
      e = listIterator(index);            E outCast = e.next();            e.remove();            return outCast;        } catch (NoSuchElementException exc) {            throw new IndexOutOfBoundsException("Index: "+index);        }    }    // Bulk Operations    public boolean addAll(int index, Collection
      c) {        try {            boolean modified = false;            ListIterator
     
       e1 = listIterator(index);            Iterator
       e2 = c.iterator();            while (e2.hasNext()) {                e1.add(e2.next());                modified = true;            }            return modified;        } catch (NoSuchElementException exc) {            throw new IndexOutOfBoundsException("Index: "+index);        }    }    // Iterators    public Iterator
      
        iterator() { return listIterator(); } public abstract ListIterator
       
         listIterator(int index);}
       
      
     
    
   
  
 

This class uses a large number of Iterator for sequential traversal of sets. Because it cannot be randomly located, this class focuses on sequential traversal. If you want to implement a list set by yourself, it is best to inherit this abstract class, in this way, you don't have to bother yourself to implement so many methods.

Struct List is a reality of sub-structure List. And it implements other interfaces, such as Deque -Double ended queue bidirectional queue, including Cloneable, java. io. Serializable Kelon and Serializable structures, and the AbstractSequentialList subinterface in the List to obtain the structure in sequence. Next we will study the sort list class. This class is implemented based on a two-way linked list, so we can operate on both ends. This requires two pointers pointing to the first and last nodes:

Transient int size = 0; transient Node
 
  
First; transient Node
  
   
Last; // linked list Node private static class Node
   
    
{E item; Node
    
     
Next; Node
     
      
Prev; Node (Node
      
        Prev, E element, Node
       
         Next) {this. item = element; this. next = next; this. prev = prev ;}}
       
      
     
    
   
  
 
An internal private class is used to represent the Node of the linked list. This Node has a pointer to the front and back. Take a look at the constructor:

 public LinkedList() {   }    public LinkedList(Collection
  c) {        this();        addAll(c);    }
You can also convert the elements in other sets to the linked list structure. The following is of course a node operation on the two-way linked list. This includes operations such as deleting and querying the first and last nodes of a linked list, searching for a node, sequence, and traversing nodes in reverse order. The method is very simple. Interested readers can study it on their own. By the way, an array can be used to implement stack operations. The linked list can also be used to provide pop (), push (), and other methods in this class. If you want to perform stack operations, you can also call these methods. You only need to operate on one end of the double-stranded table.

Because this class also implements the Conable interface, the clone () method is implemented, but this method only implements the shortest clone. The source code is as follows:

Private region list
 
  
SuperClone () {try {return (writable list
  
   
) Super. clone ();} catch (CloneNotSupportedException e) {throw new InternalError () ;}/ *** Returns a shallow copy of this unknown list. (The elements * themselves are not cloned .) */public Object clone () {// clone only references the referers list
   
    
Clone = superClone (); // Put clone into "virgin" state clone. first = clone. last = null; clone. size = 0; clone. modCount = 0; // Initialize clone with our elements for (Node
    
     
X = first; x! = Null; x = x. next) clone. add (x. item); return clone ;}
    
   
  
 

So if the item in the node you operate on is of the basic type, the modification will not affect the two. If the item is of reference type, the values of the two will affect each other. In this case, you must use your own clone method. For example:

LinkedList
 
   list=new LinkedList<>();list.add(1);list.add(2);list.add(3);list.add(4);for(Number i:list){System.out.println(i);// 1 2 3 4 }LinkedList
  
    l=(LinkedList
   
    ) list.clone();l.add(5);l.set(2, 33);for(Number i:l){System.out.println(i);// 1 2 33 4 5}
   
  
 
Modifying the value in the Value list of l will not be affected. Another example is as follows:

String[] x={"a","c"};String[] y={"m","n"};LinkedList
 
   list=new LinkedList<>();list.add(x);list.add(y);LinkedList
  
    l=(LinkedList
   
    ) list.clone();l.get(1)[1]="ttttt";System.out.println(list.get(1)[1]);
   
  
 

At this time, the printed value is ttt, that is, the reference value after the clone is modified. The original value will also be modified. At this time, the cloned value will be used in depth. If you carefully read the source code, you will find that the method for cloning linked list elements is the same as the clone method, as shown below:

  public Object[] toArray() {        Object[] result = new Object[size];        int i = 0;        for (Node
 
   x = first; x != null; x = x.next)            result[i++] = x.item;        return result;    }
 
 public 
 
   T[] toArray(T[] a) {        if (a.length < size)            a = (T[])java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size);        int i = 0;        Object[] result = a;        for (Node
  
    x = first; x != null; x = x.next)            result[i++] = x.item;        if (a.length > size)            a[size] = null;        return a;    }
  
 

















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.