修复注释里的Steam为FastStream,以及sub方法

This commit is contained in:
achao 2022-07-29 23:04:54 +08:00 committed by VampireAchao
parent d66618651d
commit c51ed6d3a1
2 changed files with 47 additions and 2 deletions

View File

@ -522,7 +522,7 @@ public class FastStream<T> implements Stream<T>, Iterable<T> {
* 这是一个无状态中间操作
*
* @param action 指定的函数
* @return 返回叠加操作后的Steam
* @return 返回叠加操作后的FastStream
* @apiNote 该方法存在的意义主要是用来调试
* 当你需要查看经过操作管道某处的元素可以执行以下操作:
* <pre>{@code
@ -542,7 +542,7 @@ public class FastStream<T> implements Stream<T>, Iterable<T> {
/**
* 返回叠加调用{@link Console#log(Object)}打印出结果的流
*
* @return 返回叠加操作后的Steam
* @return 返回叠加操作后的FastStream
*/
public FastStream<T> log() {
return peek(Console::log);
@ -1309,6 +1309,31 @@ public class FastStream<T> implements Stream<T>, Iterable<T> {
return FastStream.of(list);
}
/**
* 按指定长度切分为双层流
*
* @param batchSize 指定长度
* @return 切好的流
*/
public FastStream<FastStream<T>> sub(int batchSize) {
List<T> list = toList();
if (list.size() <= batchSize) {
return FastStream.<FastStream<T>>of(FastStream.of(list)).parallel(isParallel());
}
return FastStream.iterate(0, i -> i < list.size(), i -> i + batchSize)
.map(skip -> FastStream.of(list).skip(skip).limit(batchSize)).parallel(isParallel());
}
/**
* 按指定长度切分为元素为list的流
*
* @param batchSize 指定长度
* @return 切好的流
*/
public FastStream<List<T>> subList(int batchSize) {
return sub(batchSize).map(FastStream::toList);
}
public interface FastStreamBuilder<T> extends Consumer<T>, cn.hutool.core.builder.Builder<FastStream<T>> {
/**

View File

@ -268,4 +268,24 @@ public class FastStreamTest {
List<String> zip = FastStream.of(orders).zip(list, (e1, e2) -> e1 + "." + e2).toList();
Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), zip);
}
@Test
void testSub() {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
List<List<Integer>> lists = FastStream.of(list).sub(2).map(FastStream::toList).toList();
Assert.assertEquals(Arrays.asList(Arrays.asList(1, 2),
Arrays.asList(3, 4),
singletonList(5)
), lists);
}
@Test
void testSubList() {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
List<List<Integer>> lists = FastStream.of(list).subList(2).toList();
Assert.assertEquals(Arrays.asList(Arrays.asList(1, 2),
Arrays.asList(3, 4),
singletonList(5)
), lists);
}
}