Java Study Notes 31, java Study Notes
We know that arrays store all array elements in a continuous memory area, so the array has the best performance in random access. All the interior are made of arrays.
It provides better performance for underlying collections during random access, while the underlying collections implemented by using linked lists provide better performance for insertion and deletion;
When performing iterative operations, using a linked list as the underlying set is better than using an array as the underlying set.
Let's look at the following program:
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) ;}inclulist inclu_list = new inclulist (); for (int I = 0; I <3333333; I ++) {inclu_list.add (I );} long iterator_start1 = System. currentTimeMillis (); for (int I = 0; I <10000; I ++) {array_list.get (I);} System. out. println ("time required to traverse the elements of the ArrayList set using the get () method:" + (System. currentTimeMillis ()-iterator_start1); long has _iterator_start1 = System. currentTimeMillis (); for (int I = 0; I <10000; I ++) {pai_list.get (I);} System. out. println ("time required to use the get () method to traverse the elements of the listing list set:" + (System. currentTimeMillis ()-linked_iterator_start1); Iterator 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 to iterate the elements of the ArrayList set:" + (System. currentTimeMillis ()-iterator_start); Iterator pai_list_iterator = pai_list.iterator (); long pai_iterator_start = System. currentTimeMillis (); while (pai_list_iterator.hasnext () {pai_list_iterator.next ();} System. out. println ("time required to iterate the elements of the sorted list set:" + (System. currentTimeMillis ()-cmd_iterator_start); long arraylist_remove = System. currentTimeMillis (); array_list.remove (34567); System. out. println ("time required for ArrayList to delete a set element:" + (System. currentTimeMillis ()-arraylist_remove); long wait list_remove = System. currentTimeMillis (); pai_list.remove (34567); System. out. println ("time required to delete a collection element in the sequence list:" + (System. currentTimeMillis ()-synchronized list_remove); long arraylist_add = System. currentTimeMillis (); array_list.add (23456, "a"); System. out. println ("ArrayList time required to insert a collection element:" + (System. currentTimeMillis ()-arraylist_add); long wait list_add = System. currentTimeMillis (); pai_list.add (23456, "a"); System. out. println ("the time required to insert a collection element in the sequence list:" + (System. currentTimeMillis ()-synchronized list_add ));}}
Output result:
Time required to traverse the elements of the ArrayList set using the get () method: 0
The time required to use the get () method to traverse the elements of the sorted list set: 1806
Time required to iterate the elements of the ArrayList set: 61
Time required to iterate the elements of the sorted list set: 187
Time required for ArrayList to delete a set element: 10
The time required by the sequence list to delete a set element: 2
Time required for ArrayList to insert a set element: 9
Required Time for inserting a set element in the sequence list: 2
From the above program, we can see that:
(1) It takes a lot of time to traverse the elements of a set by using the sorted List and ArrayList respectively. Therefore, when we want to traverse the elements of a List set
When using ArrayList to traverse, the performance will be much better. We recommend that you use an iterator to traverse collection elements for the sorted list.
(2) When we need to insert or delete collection elements frequently, we should use the explain list set because the ArrayList set needs to overwrite the allocation frequently.
The size of the internal array, which has a relatively high time overhead (of course, this is often said here. In actual development, if the ArrayList set is used, insert, delete
You can use ArrayList unless it is frequent ).
Reprinted please indicate the source: http://blog.csdn.net/hai_qing_xu_kong/article/details/44136165 sentiment control _