一点一点看JDK源码(五)java.util.ArrayList 后篇之SubList

 

liuyuhang原创,未经允许禁止转载

本文举例使用的是JDK8的API

 

目录:一点一点看JDK源码(〇)

 

SubList可以将一个数组根据index拆分为多个数组,使用多线程对于数组进行遍历

 

未使用多线程的代码比较简单,代码示例如下:

 1 package com.FM.ArrayListStudy;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 public class SubListInArrayListStudy {
 7     public static void main(String[] args) {
 8         // 初始化list
 9         ArrayList<Integer> list01 = new ArrayList<Integer>();
10         for (int i = 0; i < 20; i++) {//除4无余数
11             list01.add(i + 1);
12         }
13         ArrayList<Integer> list02 = new ArrayList<Integer>();
14         for (int i = 0; i < 30; i++) {//除4有余数
15             list02.add(i + 1);
16         }
17         
18         int size1 = list01.size();
19         int last11 = size1/4;
20         int last12 = size1/4*2;
21         int last13 = size1/4*3;
22         
23         int size2 = list02.size();
24         int last21 = size2/4;
25         int last22 = size2/4*2;
26         int last23 = size2/4*3;
27         
28         List<Integer> subList11 = list01.subList(0, last11);
29         List<Integer> subList12 = list01.subList(last11, last12);
30         List<Integer> subList13 = list01.subList(last12, last13);
31         List<Integer> subList14 = list01.subList(last13, size1);
32         
33         List<Integer> subList21 = list02.subList(0, last21);
34         List<Integer> subList22 = list02.subList(last21, last22);
35         List<Integer> subList23 = list02.subList(last22, last23);
36         List<Integer> subList24 = list02.subList(last23, size2);
37         
38         System.out.println(subList11+" size: "+subList11.size());
39         System.out.println(subList12+" size: "+subList12.size());
40         System.out.println(subList13+" size: "+subList13.size());
41         System.out.println(subList14+" size: "+subList14.size());
42         System.out.println("-=-=-=-=-=-=-=");
43         System.out.println(subList21+" size: "+subList21.size());
44         System.out.println(subList22+" size: "+subList22.size());
45         System.out.println(subList23+" size: "+subList23.size());
46         System.out.println(subList24+" size: "+subList24.size());
47         
48         
49     }
50 }

 

运行结果

 

 

使用多线程方式的代码连接在此,只是并非使用subList,自行修改

 一点一点看JDK源码(五)java.util.ArrayList 后篇之Spliterator

 

以上!!