|
| 1 | +/* |
| 2 | + * Copyright (C) 2021 vikadata |
| 3 | + * |
| 4 | + * This program is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; either version 2 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License along |
| 15 | + * with this program; if not, write to the Free Software Foundation, Inc., |
| 16 | + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 17 | + */ |
| 18 | + |
| 19 | +package cn.vika.core.utils; |
| 20 | + |
| 21 | +import java.util.List; |
| 22 | +import java.util.stream.Collectors; |
| 23 | +import java.util.stream.Stream; |
| 24 | + |
| 25 | +/** |
| 26 | + * Utils for Collection |
| 27 | + * @author Shawn Deng |
| 28 | + * @date 2021-05-19 23:52:38 |
| 29 | + */ |
| 30 | +public class CollectionUtil { |
| 31 | + |
| 32 | + /** |
| 33 | + * Split collection by size using parallel stream |
| 34 | + * @param list List |
| 35 | + * @param splitSize split size |
| 36 | + * @param <T> Class Type |
| 37 | + * @return Split List Collection |
| 38 | + */ |
| 39 | + public static <T> List<List<T>> splitListParallel(List<T> list, int splitSize) { |
| 40 | + int limit = (list.size() + splitSize - 1) / splitSize; |
| 41 | + return Stream.iterate(0, n -> n + 1) |
| 42 | + .limit(limit).parallel().map(item -> |
| 43 | + list.stream().skip((long) item * splitSize) |
| 44 | + .limit(splitSize).parallel() |
| 45 | + .collect(Collectors.toList())) |
| 46 | + .collect(Collectors.toList()); |
| 47 | + } |
| 48 | +} |
0 commit comments