List每次取100条进行处理

左手的ㄟ右手 2024-04-17 14:28 149阅读 0赞
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. public class DealListByBatch {
  4. /**
  5. * 通过list的 subList(int fromIndex, int toIndex)方法实现
  6. * @param sourList 源list
  7. * @param batchCount 分组条数
  8. */
  9. public static void dealBySubList(List<Object> sourList, int batchCount){
  10. int sourListSize = sourList.size();
  11. int subCount = sourListSize%batchCount==0 ? sourListSize/batchCount : sourListSize/batchCount+1;
  12. int startIndext = 0;
  13. int stopIndext = 0;
  14. for(int i=0;i<subCount;i++){
  15. stopIndext = (i==subCount-1) ? stopIndext + sourListSize%batchCount : stopIndext + batchCount;
  16. List<Object> tempList = new ArrayList<Object>(sourList.subList(startIndext, stopIndext));
  17. printList(tempList);
  18. startIndext = stopIndext;
  19. }
  20. }
  21. /**
  22. * 通过源list数据的逐条转移实现
  23. * @param sourList 源list
  24. * @param batchCount 分组条数
  25. */
  26. public static void dealByRemove(List<Object> sourList, int batchCount){
  27. List<Object> tempList = new ArrayList<Object>();
  28. for (int i = 0; i < sourList.size(); i++) {
  29. tempList.add(sourList.get(i));
  30. if((i+1)%batchCount==0 || (i+1)==sourList.size()){
  31. printList(tempList);
  32. tempList.clear();
  33. }
  34. }
  35. }
  36. /**
  37. * 打印方法 充当list每批次数据的处理方法
  38. * @param sourList
  39. */
  40. public static void printList(List<Object> sourList){
  41. for(int j=0;j<sourList.size();j++){
  42. System.out.println(sourList.get(j));
  43. }
  44. System.out.println("------------------------");
  45. }
  46. /**
  47. * 测试主方法
  48. * @param args
  49. */
  50. public static void main(String[] args) {
  51. List<Object> list = new ArrayList<Object>();
  52. for (int i = 0; i < 260; i++) {
  53. list.add(i);
  54. }
  55. long start = System.nanoTime();
  56. dealBySubList(list, 100 );
  57. // dealByRemove(list, 10);
  58. long end = System.nanoTime();
  59. System.out.println("The elapsed time :" + (end-start));
  60. }
  61. }

发表评论

表情:
评论列表 (有 0 条评论,149人围观)

还没有评论,来说两句吧...

相关阅读