Java shortlist arraylist random access efficiency list. Get (INT index)

Source: Internet
Author: User

In theory, it is certain that the primary list is less efficient than the random access efficiency of arraylist, and then the primary list is faster than the inserted and deleted elements of arraylist.

 

I suddenly remembered writing a notebook.ProgramIs to use the sorted list + Map Index as the database. Map records the correspondence between the index and the date of each diary in the daily list. Obtain the index of the log corresponding to a date from the map, and then sort list and get (index ).

 

 

  Integer  =     1  ;
Revoke list = New Vertex list ();
For ( Int I = 0 ; I < 2000000 ; I ++ ){
List. Add ();
}
System. Out. println (list. Size ());
Long Start = System. nanotime ();
List. Get ( 1000000 );
Long End = System. nanotime ();
System. Out. println (end - Start );

Previous sectionCode, We can see several things:

 

1. the random access speed of the shortlist is indeed almost the same, which takes about 17 milliseconds. The following will post the comment list for Random AccessSource codeThat is why 1000000 is selected.

2. the java stack and heap are limited. If you add 5000000 items at a time in the list, the memory will overflow.

(Exception in thread "Main" Java. Lang. outofmemoryerror: Java heap space ).

But it's a bit strange, isn't it new in the memory heap area? The memory heap area will also crash ~~

 

Below is the source code randomly accessed by the shortlistAnd performs an int comparison in each loop.

 

    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 )){
For ( Int I = 0 ; I <= Index; I ++ )
E = E. Next;
} Else {
For ( Int I = Size; I > Index; I -- )
E = E. Previous;
}
Return E;
}

 

 

If arraylist is changed, the addition of 5000000 items will not burst, but the increase will continue to burst ~~

Random Access efficiency is indeed much higher. It only takes about 16 microseconds,Faster than 1 thousand timesAnd has nothing to do with get index.

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.