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.