Requirements: A total of 1000 data, the first batch insert 100, the second batch Insert 101 to 200, insert data in turn;
The way of implementation here choose two commonly used methods, are using the list operation;
The first way to realize the idea is as follows:
<1> the list for the original storage data is recordlist, and the number of batch processing is obtained;
<2> Create a list for list, and after the loop, put the recordlist of the first MaxValue data into the list;
<3> Call the batch processing method, call Recordlist's RemoveAll method to remove the data from the list from the recordlist;
<4> call the List.clear method to erase the data from the list itself;
/*** BULK Insert for example: A total of 1000 data, the first batch insert 100, the second batch Insert 101 to 200, insert data in turn; *@paramRecordlist *@parammaxValue Number of batches processed, such as 1000 *@return */ Private voidBatchaddrecords (list<string> recordlist,intmaxValue) {List<String> list =NewArraylist<string>(); intSize =recordlist.size (); intTotal = size/MaxValue; if(size% MaxValue! = 0) { Total+ = 1; } for(inti = 0; I < total; i++) { if(i = = Total-1) {MaxValue= Size-(I *maxValue); } for(intj = 0; J < MaxValue; J + +) {List.add (Recordlist.get (j)); } //Methods for batch processingprint (list); Recordlist.removeall (list); List.clear (); } }
The second way to achieve this is similar:
By calling Recordlist's Sublist method, the captured content is saved to the list, then the batch processing method is called, and finally the clear method of the list is called to clear the data in list and recordlist.
Note that the Sublist method of the list is a bit special, and it operates on the same piece of memory that the list and the original list refer to, so be aware of it.
Private voidBatchAddRecords2 (list<string> recordlist,intmaxValue) {List<String> list =NewArraylist<string>(); intSize =recordlist.size (); intTotal = size/MaxValue; if(size% MaxValue! = 0) { Total+ = 1; } for(inti = 0; I < total; i++) { if(i = = Total-1) {MaxValue= Size-(I *maxValue); } list= Recordlist.sublist (0, MaxValue); //Methods for batch processingprint (list); List.clear (); } }
Java Batch processing data