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

时间:2024-08-06 11:02:50

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

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

本文举例使用的是JDK8的API

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

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

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

 package com.FM.ArrayListStudy;

 import java.util.ArrayList;
import java.util.List; public class SubListInArrayListStudy {
public static void main(String[] args) {
// 初始化list
ArrayList<Integer> list01 = new ArrayList<Integer>();
for (int i = 0; i < 20; i++) {//除4无余数
list01.add(i + 1);
}
ArrayList<Integer> list02 = new ArrayList<Integer>();
for (int i = 0; i < 30; i++) {//除4有余数
list02.add(i + 1);
} int size1 = list01.size();
int last11 = size1/4;
int last12 = size1/4*2;
int last13 = size1/4*3; int size2 = list02.size();
int last21 = size2/4;
int last22 = size2/4*2;
int last23 = size2/4*3; List<Integer> subList11 = list01.subList(0, last11);
List<Integer> subList12 = list01.subList(last11, last12);
List<Integer> subList13 = list01.subList(last12, last13);
List<Integer> subList14 = list01.subList(last13, size1); List<Integer> subList21 = list02.subList(0, last21);
List<Integer> subList22 = list02.subList(last21, last22);
List<Integer> subList23 = list02.subList(last22, last23);
List<Integer> subList24 = list02.subList(last23, size2); System.out.println(subList11+" size: "+subList11.size());
System.out.println(subList12+" size: "+subList12.size());
System.out.println(subList13+" size: "+subList13.size());
System.out.println(subList14+" size: "+subList14.size());
System.out.println("-=-=-=-=-=-=-=");
System.out.println(subList21+" size: "+subList21.size());
System.out.println(subList22+" size: "+subList22.size());
System.out.println(subList23+" size: "+subList23.size());
System.out.println(subList24+" size: "+subList24.size()); }
}

运行结果

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

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

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

以上!!