Some pit used Java collections

ArrayList stepped pit

 

 
 

List <String> TEMP = new new the ArrayList ();
// number of acquired data
List <String> All the getData = ();
for (String STR: All)
{
temp.add (STR);
}

 

First we look at this code thing what is the problem?

In fact, in most cases, this problem is nothing, nothing more than to cycle ArrayListwrite data only.

However, in exceptional circumstances, such as where the return is enormous follow-up data will be a problem.getData()temp.add(str)

For example, we reviewfound that sometimes this data will be returned when the code up to 2000W, then the ArrayListquestion written on the highlights out.



Filled pit Guide

We all know that ArrayList is implemented by the array, and the limited length of the data; the need for expansion of the array at the right time.
The new length of the original length = 1.5 *


the amount of data is particularly large, multiple expansion will lead to the bottom of efficiency, it is recommended to initialize the array of a length. This will minimize expansion.
If the business scenarios require more than read written words, consider using LinkedList


writing efficiency is higher than LinkedList ArrayList, so writing is greater than the read time is ideal for LinkedList.


Guess you like

Origin www.cnblogs.com/pengthrree/p/12143826.html