Java 8个分区列表
Java 8 partition list
是否可以将纯 Jdk8 中的列表划分为相等的块(子列表)。
我知道可以使用 Guava Lists class,但我们可以使用纯 Jdk 吗?我不想向我的项目添加新的 jar,只是为了一个用例。
解决方案:
迄今为止最好的解决方案是由 提出的:
我也找到了three other possibilities,但只有少数情况是:
1.Collectors.partitioningBy() 将列表拆分为 2 个子列表——如下所示:
intList.stream().collect(Collectors.partitioningBy(s -> s > 6));
List<List<Integer>> subSets = new ArrayList<List<Integer>>(groups.values());
2.Collectors.groupingBy() 将我们的列表拆分为多个分区:
Map<Integer, List<Integer>> groups =
intList.stream().collect(Collectors.groupingBy(s -> (s - 1) / 3));
List<List<Integer>> subSets = new ArrayList<List<Integer>>(groups.values());
3.Split 分隔符:
List<Integer> intList = Lists.newArrayList(1, 2, 3, 0, 4, 5, 6, 0, 7, 8);
int[] indexes =
Stream.of(IntStream.of(-1), IntStream.range(0, intList.size())
.filter(i -> intList.get(i) == 0), IntStream.of(intList.size()))
.flatMapToInt(s -> s).toArray();
List<List<Integer>> subSets =
IntStream.range(0, indexes.length - 1)
.mapToObj(i -> intList.subList(indexes[i] + 1, indexes[i + 1]))
.collect(Collectors.toList());
4.Using 流 + 计数器 source:
final List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7);
final int chunkSize = 3;
final AtomicInteger counter = new AtomicInteger();
final Collection<List<Integer>> result = numbers.stream()
.collect(Collectors.groupingBy(it -> counter.getAndIncrement() / chunkSize))
.values();
这可以使用 subList()
方法轻松完成:
List<String> collection = new ArrayList<>(21);
// fill collection
int chunkSize = 10;
List<List<String>> lists = new ArrayList<>();
for (int i = 0; i < collection.size(); i += chunkSize) {
int end = Math.min(collection.size(), i + chunkSize);
lists.add(collection.subList(i, end));
}
尝试使用此代码,它使用 Java 8:
public static Collection<List<Integer>> splitListBySize(List<Integer> intList, int size) {
if (!intList.isEmpty() && size > 0) {
final AtomicInteger counter = new AtomicInteger(0);
return intList.stream().collect(Collectors.groupingBy(it -> counter.getAndIncrement() / size)).values();
}
return null;
}
我已经用定制的 Collector 尝试了自己的解决方案。我希望有人会发现它有用,或者帮助我改进它。
class PartitioningCollector<T> implements Collector<T, List<List<T>>, List<List<T>>> {
private final int batchSize;
private final List<T> batch;
public PartitioningCollector(int batchSize) {
this.batchSize = batchSize;
this.batch = new ArrayList<>(batchSize);
}
@Override
public Supplier<List<List<T>>> supplier() {
return LinkedList::new;
}
@Override
public BiConsumer<List<List<T>>, T> accumulator() {
return (total, element) -> {
batch.add(element);
if (batch.size() >= batchSize) {
total.add(new ArrayList<>(batch));
batch.clear();
}
};
}
@Override
public BinaryOperator<List<List<T>>> combiner() {
return (left, right) -> {
List<List<T>> result = new ArrayList<>();
result.addAll(left);
result.addAll(left);
return result;
};
}
@Override
public Function<List<List<T>>, List<List<T>>> finisher() {
return result -> {
if (!batch.isEmpty()) {
result.add(new ArrayList<>(batch));
batch.clear();
}
return result;
};
}
@Override
public Set<Characteristics> characteristics() {
return emptySet();
}
}
private final String dataSheet = "103343262,6478342944, 103426540,84528784843, 103278808,263716791426, 103426733,27736529279,
103426000,27718159078, 103218982,19855201547, 103427376,27717278645,
103243034,81667273413";
final int chunk = 2;
AtomicInteger counter = new AtomicInteger();
Collection<List<String>> chuncks= Arrays.stream(dataSheet.split(","))
.map(String::trim)
.collect(Collectors.groupingBy(i->counter.getAndIncrement()/chunk))
.values();
result:
pairs =
"103218982" -> "19855201547"
"103278808" -> "263716791426"
"103243034" -> "81667273413"
"103426733" -> "27736529279"
"103426540" -> "84528784843"
"103427376" -> "27717278645"
"103426000" -> "27718159078"
"103343262" -> "6478342944"
We need to group each 2 elements into key, value pairs, so will partion the list into chunks of 2, (counter.getAndIncrement() / 2) will result same number each 2 hits ex:
IntStream.range(0,6).forEach((i)->System.out.println(counter.getAndIncrement()/2));
prints:
0
0
1
1
2
2
You may ajust chunk sizee to partition lists sizes.
Guava Lists class 有一个 partition() 方法可以做到这一点。参见 https://guava.dev/releases/21.0/api/docs/com/google/common/collect/Lists.html#partition-java.util.List-int-
是否可以将纯 Jdk8 中的列表划分为相等的块(子列表)。
我知道可以使用 Guava Lists class,但我们可以使用纯 Jdk 吗?我不想向我的项目添加新的 jar,只是为了一个用例。
解决方案:
迄今为止最好的解决方案是由
我也找到了three other possibilities,但只有少数情况是:
1.Collectors.partitioningBy() 将列表拆分为 2 个子列表——如下所示:
intList.stream().collect(Collectors.partitioningBy(s -> s > 6));
List<List<Integer>> subSets = new ArrayList<List<Integer>>(groups.values());
2.Collectors.groupingBy() 将我们的列表拆分为多个分区:
Map<Integer, List<Integer>> groups =
intList.stream().collect(Collectors.groupingBy(s -> (s - 1) / 3));
List<List<Integer>> subSets = new ArrayList<List<Integer>>(groups.values());
3.Split 分隔符:
List<Integer> intList = Lists.newArrayList(1, 2, 3, 0, 4, 5, 6, 0, 7, 8);
int[] indexes =
Stream.of(IntStream.of(-1), IntStream.range(0, intList.size())
.filter(i -> intList.get(i) == 0), IntStream.of(intList.size()))
.flatMapToInt(s -> s).toArray();
List<List<Integer>> subSets =
IntStream.range(0, indexes.length - 1)
.mapToObj(i -> intList.subList(indexes[i] + 1, indexes[i + 1]))
.collect(Collectors.toList());
4.Using 流 + 计数器 source:
final List<Integer> numbers = Arrays.asList(1,2,3,4,5,6,7);
final int chunkSize = 3;
final AtomicInteger counter = new AtomicInteger();
final Collection<List<Integer>> result = numbers.stream()
.collect(Collectors.groupingBy(it -> counter.getAndIncrement() / chunkSize))
.values();
这可以使用 subList()
方法轻松完成:
List<String> collection = new ArrayList<>(21);
// fill collection
int chunkSize = 10;
List<List<String>> lists = new ArrayList<>();
for (int i = 0; i < collection.size(); i += chunkSize) {
int end = Math.min(collection.size(), i + chunkSize);
lists.add(collection.subList(i, end));
}
尝试使用此代码,它使用 Java 8:
public static Collection<List<Integer>> splitListBySize(List<Integer> intList, int size) {
if (!intList.isEmpty() && size > 0) {
final AtomicInteger counter = new AtomicInteger(0);
return intList.stream().collect(Collectors.groupingBy(it -> counter.getAndIncrement() / size)).values();
}
return null;
}
我已经用定制的 Collector 尝试了自己的解决方案。我希望有人会发现它有用,或者帮助我改进它。
class PartitioningCollector<T> implements Collector<T, List<List<T>>, List<List<T>>> {
private final int batchSize;
private final List<T> batch;
public PartitioningCollector(int batchSize) {
this.batchSize = batchSize;
this.batch = new ArrayList<>(batchSize);
}
@Override
public Supplier<List<List<T>>> supplier() {
return LinkedList::new;
}
@Override
public BiConsumer<List<List<T>>, T> accumulator() {
return (total, element) -> {
batch.add(element);
if (batch.size() >= batchSize) {
total.add(new ArrayList<>(batch));
batch.clear();
}
};
}
@Override
public BinaryOperator<List<List<T>>> combiner() {
return (left, right) -> {
List<List<T>> result = new ArrayList<>();
result.addAll(left);
result.addAll(left);
return result;
};
}
@Override
public Function<List<List<T>>, List<List<T>>> finisher() {
return result -> {
if (!batch.isEmpty()) {
result.add(new ArrayList<>(batch));
batch.clear();
}
return result;
};
}
@Override
public Set<Characteristics> characteristics() {
return emptySet();
}
}
private final String dataSheet = "103343262,6478342944, 103426540,84528784843, 103278808,263716791426, 103426733,27736529279,
103426000,27718159078, 103218982,19855201547, 103427376,27717278645,
103243034,81667273413";
final int chunk = 2;
AtomicInteger counter = new AtomicInteger();
Collection<List<String>> chuncks= Arrays.stream(dataSheet.split(","))
.map(String::trim)
.collect(Collectors.groupingBy(i->counter.getAndIncrement()/chunk))
.values();
result:
pairs =
"103218982" -> "19855201547"
"103278808" -> "263716791426"
"103243034" -> "81667273413"
"103426733" -> "27736529279"
"103426540" -> "84528784843"
"103427376" -> "27717278645"
"103426000" -> "27718159078"
"103343262" -> "6478342944"
We need to group each 2 elements into key, value pairs, so will partion the list into chunks of 2, (counter.getAndIncrement() / 2) will result same number each 2 hits ex:
IntStream.range(0,6).forEach((i)->System.out.println(counter.getAndIncrement()/2));
prints:
0
0
1
1
2
2
You may ajust chunk sizee to partition lists sizes.
Guava Lists class 有一个 partition() 方法可以做到这一点。参见 https://guava.dev/releases/21.0/api/docs/com/google/common/collect/Lists.html#partition-java.util.List-int-