Java類比資料量過大時批量處理資料實現

來源:互聯網
上載者:User

標籤:class   time   次數   integer   for   bsp   inf   問題   new   

代碼:

import java.util.ArrayList;import java.util.List;/** * 類比批量處理資料 * 當資料量過大過多導致逾時等問題可以將資料進行分批處理 * @author 【】 * */public class BatchUtil {public static void listBatchUtil(List<Integer> lists) {System.out.println(lists);// 定義批處理的資料數量(即批處理條件)int num = 10;// 判斷集合數量,如果小於等於定義的數量(即未達到批處理條件),直接進行處理if (lists.size() <= num) {System.out.println(lists.size());System.out.println(lists.toString().substring(1, lists.toString().lastIndexOf("]")));return;}// 如果大於定義的數量,按定義數量進行批處理int  times = lists.size()/num + 1;System.out.println("一共要進行"+times+"次批處理");// 遍曆分批處理次數,並進行批處理for (int i = 0; i < times; i++) {// 定義要進行批處理的臨時集合List<Integer> tempList = new ArrayList<>();// 將要批處理資料放入臨時集合中for (int j = i*num; j < lists.size(); j++) {tempList.add(lists.get(j));if (tempList.size() == num) {break;}}// 進行批處理System.out.println("======================進行第"+(i+1)+"次批處理=======================");System.out.println(tempList.size());System.out.println(tempList.toString().substring(1, tempList.toString().lastIndexOf("]")));System.out.println("=========================================================");}}public static void main(String[] args) {List<Integer> lists = new ArrayList<>();for (int i = 1; i <= 26; i++) {lists.add(i);}listBatchUtil(lists);}}

 實現效果:

 

Java類比資料量過大時批量處理資料實現

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.