ArrayList business scenarios use considerations

background

        The product asked me to write a report to count the distribution and exchange of red envelopes in a certain province, each city, and the province. When the front-end is required to display and write the excel file, the province's records are placed in the first row of the table.
                                                                          as the picture shows
Insert picture description here

Ideas

        Report statistics generally choose to use the List collection, and choose one of ArrayList and LinkedList.

The choice should be based on specific business logic:

  1. The fields of each city are counted first, and then the whole province is counted. The province field is the sum of the fields of each city. After the fields of each city are settled, they are put into the List collection. After the fields of the province are counted, they must be added to the first position .
  2. The front-end display and writing excel file functions are generally to sequentially traverse and read the content in the List collection. An element of the List contains the red envelope issuance and redemption status of a city.
public void exportAsExcel(List<RedPaperExchange> list ){
    
    
	// 将红包发放和兑换的情况写入excel文件,用于导出
	List<RedPaperExchange> list=queryRedPaperExchange();
	sheet=excel.createSheet();
	for(int i=0;i<list.size();i++){
    
    
		//往excel的sheet填充数据,伪代码
		fillDataIntoExcel();
	}
}

private List<RedPaperExchange> queryRedPaperExchange(){
    
    
	 // 查询各地市和全省的红包发放和兑换情况,将结果用于前端显示和写入Excel
	 List<RedPaperExchange> list=new ArrayList<>(20);
	 for(){
    
    
	 	//先统计各地市的红包发放和兑换情况
	 	list.add(cityRedPaperExchange);
	 }
	 //将全省的数据放在第一行
	 list.add(0, provRedPaperExchange );
	 return list;
}

Then the Java collections we use must meet our functional points:

  1. Front display excel file and filling process are sequentially traversed read, traverse speed ArrayList LinkedList than the traversal speed , because the ArrayListContinuous in memory, Can reduce the performance overhead of reading memory.
  2. The province’s records will be inserted into the first position of the List after the statistics are completed. We know that ArrayList will copy the original array when list.add(0, element), and then place the original array in sequence from the second position. Element, finally let list[0]=element, which itself is very time-consuming, if one element is inserted into an ArrayList of tens of thousands of elements, then all the following elements have to be copied, and it will be even slower if you want to expand the capacity. .

The LinkedList is based on a doubly linked list, which can add new nodes directly to the head, and the time complexity is O(1)

  1. There are probably less than 20 cities in a province. If the ArrayList is created with the default initial capacity (10), it will need to be expanded twice later. Therefore, it is necessary to pass in initialCapacity=20 when the new ArrayList is used to reduce the number of expansions. The LinkedList expansion does not require a time-consuming operation such as array copy.

Results considered:

  1. ArrayList

       Advantage:

  • Sequentially traversing the collection and writing data to the Excel file is fast (Memory contiguous

        Disadvantages:

  • It takes time to insert the province’s data into the first row of the report
  • If the initial capacity is set too low, many prefecture-level cities in a certain province will need to expand the capacity multiple times

2. LinkedList
        advantages:

  • The time complexity of inserting the province’s data into the first row is O(1), which is fast
  • No need to worry about expansion

        Disadvantages:

  • Sequentially traversing the collection and writing data to the Excel file is inefficient because the space of the linked list is not continuous

Finally, although it seems that LinkedList has more advantages, ArrayList is still conservatively chosen haha. It is true that inserting elements and expansion of ArrayList takes time, but if the initial capacity of initialCapacity is set reasonably, the risk of multiple expansions can be avoided, and the size of an ArrayList containing red envelope exchange records in all regions and cities of the province does not exceed 50, which is the time brought by inserting data The consumption is acceptable, so ArrayList is chosen.

Guess you like

Origin blog.csdn.net/qq_44384533/article/details/108463571
Recommended