The difference between the implementation class arraylist,linkedlist,vector of the Java list interface

Source: Internet
Author: User

The Java list interface has 3 implementation classes, ArrayList, LinkedList, and vectors, which are used to store multiple elements, maintain the order of elements, and allow elements to be duplicated.

The differences between the 3 specific implementation classes are as follows:

1. ArrayList is the most commonly used list implementation class, implemented internally by an array, which allows for fast random access to elements. The disadvantage of an array is that there can be no interval between each element, and when the array size does not meet the need to increase storage capacity, the data that already has the array is copied into the new storage space. When inserting or deleting elements from the middle of a ArrayList, it is necessary to copy, move, and cost the array. Therefore, it is suitable for random lookups and traversal, not suitable for insertions and deletions, allowing empty elements

2. Vectors, like ArrayList, are also implemented by arrays, except that it supports thread synchronization, where only one thread can write vectors at a time, avoiding inconsistencies caused by simultaneous writing of multiple threads, but achieving synchronization requires a high cost, so Accessing it is slower than accessing ArrayList.

3. LinkedList is used to store data in a linked list structure, which is suitable for dynamic insertion and deletion of data, and is slow in random access and traversal. In addition, there is no method get,remove,insertlist defined in the interface, specifically for manipulating the header and footer elements, which can be used as stacks, queues, and bidirectional queues. LinkedList There is no synchronization method. If multiple threads access a list at the same time, you must implement access synchronization yourself. One workaround is to construct a synchronized list when the list is created:
List List = Collections.synchronizedlist (new LinkedList (...));

Look at the Java source code, and find that when the size of the array is not enough, you need to re-establish the array, and then copy the elements into the new array, the size of the ArrayList and vector extended array is different.

In ArrayList:

1  Public BooleanAdd (e e) {2  3Ensurecapacity (size + 1);//add elements to determine if they can be accommodated. Create a new array if you can't.4   5elementdata[size++] =e;6  7      return true;8  9  }Ten   One    Public voidEnsurecapacity (intmincapacity) { A   -modcount++;  -   the      intOldcapacity =elementdata.length; -   -      if(Mincapacity >oldcapacity) { -   +Object olddata[] = elementdata;//This trip did not see the usefulness, do not know what the developers think -    +          intNewcapacity = (oldcapacity * 3)/2 + 1;//increase the size of the new array A    at          if(Newcapacity <mincapacity) -   -Newcapacity =mincapacity; -   -              //mincapacity is usually close to size, so this is a win: -    inElementdata =arrays.copyof (Elementdata, newcapacity); -   to      } +   -}

In Vector:

1 Private voidEnsurecapacityhelper (intmincapacity) {2  3      intOldcapacity =elementdata.length;4  5      if(Mincapacity >oldcapacity) {6  7object[] OldData =Elementdata;8  9          intnewcapacity = (capacityincrement > 0)?Ten   One(Oldcapacity + capacityincrement): (Oldcapacity * 2); A   -          if(Newcapacity <mincapacity) { -   theNewcapacity =mincapacity; -   -          } -   +Elementdata =arrays.copyof (Elementdata, newcapacity); -   +      } A   at}

The differences between ArrayList and vectors are as follows:

1. ArrayList the default extension is 50% + 1 when insufficient memory, vector is 1 time times the default expansion.

2. Vector provides indexof (obj, start) interface, ArrayList not.

3. Vectors are thread-safe, but in most cases do not use vectors because thread safety requires greater overhead.

The difference between the implementation class arraylist,linkedlist,vector of the Java list interface

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.