We know that the array holds all the array elements in a contiguous memory area, so the array has the best performance at random access. All internal arrays are
The set that is implemented for the bottom layer performs well at random access, while the internal set of lists as the underlying implementation has better performance when inserting and deleting operations;
When iterating, the collection with the linked list as the underlying implementation is better than the collection with the array as the underlying implementation.
Let's take a look at the following procedures:
public class Main {public static void main (string[] args) {ArrayList array_list=new ArrayList (); for (int i=0;i<3333333; i++) {array_list.add (i);} LinkedList linked_list=new LinkedList (); for (int i=0;i<3333333;i++) {linked_list.add (i);} Long Iterator_start1=system.currenttimemillis (); for (int i=0;i<10000;i++) {array_list.get (i);} System.out.println ("The time required to traverse the elements of the ArrayList collection using the Get () method:" + (System.currenttimemillis ()-iterator_start1); Long linked_ Iterator_start1=system.currenttimemillis (); for (int i=0;i<10000;i++) {linked_list.get (i);} System.out.println ("The time required to traverse the elements of the LinkedList collection using the Get () method:" + (System.currenttimemillis ()-linked_iterator_start1)); Terator Array_list_iterator=array_list.iterator (); Long Iterator_start=system.currenttimemillis (); while (Array_ List_iterator.hasnext ()) {Array_list_iterator.next ();} System.out.println ("Time required for iterating over elements of the ArrayList collection:" + (System.currenttimemillis ()-iterator_start); iterator Linked_list_ Iterator=linked_list.iterator (); Long Linked_iterator_start=system.currenttimemillis (); while(Linked_list_iterator.hasnext ()) {Linked_list_iterator.next ();} System.out.println ("Time required for iterating over elements of the LinkedList collection:" + (System.currenttimemillis ()-linked_iterator_start); Long arraylist_ Remove=system.currenttimemillis (); Array_list.remove (34567); System.out.println ("ArrayList the time required to delete the collection element:" + (System.currenttimemillis ()-arraylist_remove); Long Linkedlist_remove =system.currenttimemillis (); Linked_list.remove (34567); System.out.println ("LinkedList the time required to delete the collection element:" + (System.currenttimemillis ()-linkedlist_remove); Long arraylist_add= System.currenttimemillis (); Array_list.add (23456, "a"); System.out.println ("ArrayList the time required to insert the collection element:" + (System.currenttimemillis ()-arraylist_add); Long linkedlist_add= System.currenttimemillis (); Linked_list.add (23456, "a"); System.out.println ("LinkedList time to insert the collection element:" + (System.currenttimemillis ()-linkedlist_add);}}
Output Result:
Time required to traverse the elements of the ArrayList collection using the Get () method: 0
Time required to traverse the elements of the LinkedList collection using the Get () method: 1806
Iterate over elements of ArrayList collection time required: 61
Iterate over elements of LinkedList collection time required: 187
ArrayList time to delete a collection element: 10
LinkedList time to delete a collection element: 2
ArrayList inserting a Collection element time: 9
LinkedList inserting a collection element time: 2
As you can see from the program above:
(1) The time difference between using LinkedList and ArrayList to traverse a collection element is very large, so when we are going to traverse the list collection element
, using ArrayList to traverse performance is much better, and for LinkedList it is recommended to use iterators to iterate through the collection elements.
(2) We should use the LinkedList collection when we need to perform frequent insertions and deletions of collection elements, because the ArrayList collection needs to be rewritten frequently to allocate
The size of the internal array, the time overhead is relatively large (of course, it is said frequently, in the actual development if the use of ArrayList collection, do insert, delete
You can use ArrayList in addition to not being very frequent).
Reprint Please specify source: http://blog.csdn.net/hai_qing_xu_kong/article/details/44136165 Emotional Control _
Java Learning Note 31