java8 切割集合

java8 切割集合

private static final Integer MAX_NUMBER = 2;

public static void main(String[] args) {
    List numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
    ArrayList> mglist = new ArrayList>();
    int LIMIT = countStep(numbers.size());
    //使用并行流遍历操作
    Stream.iterate(0, n -> n + 1).limit(LIMIT).parallel().forEach(i -> {
        mglist.add((ArrayList) numbers.stream().skip(i * MAX_NUMBER).limit(MAX_NUMBER).collect(Collectors.toList()));
    });
    System.out.println(mglist);
}

private final static Integer countStep(Integer size) {
    Integer count = size / MAX_NUMBER;
    return size % 2 == 0 ? count : count + 1;
}

你可能感兴趣的:(java8)