This commit is contained in:
Looly 2022-07-29 23:10:25 +08:00
parent 15772f1ec1
commit 14f09c2141
2 changed files with 25 additions and 32 deletions

View File

@ -3,6 +3,7 @@ package cn.hutool.core.stream;
import cn.hutool.core.lang.Console;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.text.StrUtil;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
@ -56,6 +57,7 @@ public class FastStream<T> implements Stream<T>, Iterable<T> {
*/
public static <T> FastStreamBuilder<T> builder() {
return new FastStreamBuilder<T>() {
private static final long serialVersionUID = 1L;
private final Builder<T> streamBuilder = Stream.builder();
@Override
@ -133,9 +135,9 @@ public class FastStream<T> implements Stream<T>, Iterable<T> {
* </p>
*
* @param <T> 元素类型
* @param hasNext 条件值
* @param seed 初始值
* @param f 用上一个元素作为参数执行并返回一个新的元素
* @param hasNext 条件值
* @param next 用上一个元素作为参数执行并返回一个新的元素
* @return 无限有序流
*/
public static <T> FastStream<T> iterate(T seed, Predicate<? super T> hasNext, UnaryOperator<T> next) {
@ -650,6 +652,7 @@ public class FastStream<T> implements Stream<T>, Iterable<T> {
* }</pre>
*/
public <A> A[] toArray(IntFunction<A[]> generator) {
//noinspection SuspiciousToArrayCall
return stream.toArray(generator);
}
@ -1083,7 +1086,10 @@ public class FastStream<T> implements Stream<T>, Iterable<T> {
*/
@Override
public boolean equals(Object obj) {
return stream.equals(obj);
if (obj instanceof Stream) {
return stream.equals(obj);
}
return false;
}
/**
@ -1143,7 +1149,7 @@ public class FastStream<T> implements Stream<T>, Iterable<T> {
* @return 拼接后的字符串
*/
public String join() {
return join("");
return join(StrUtil.EMPTY);
}
/**
@ -1153,7 +1159,7 @@ public class FastStream<T> implements Stream<T>, Iterable<T> {
* @return 拼接后的字符串
*/
public String join(CharSequence delimiter) {
return join(delimiter, "", "");
return join(delimiter, StrUtil.EMPTY, StrUtil.EMPTY);
}
/**
@ -1336,16 +1342,6 @@ public class FastStream<T> implements Stream<T>, Iterable<T> {
public interface FastStreamBuilder<T> extends Consumer<T>, cn.hutool.core.builder.Builder<FastStream<T>> {
/**
* Adds an element to the stream being built.
*
* @param t the element to add
* @throws IllegalStateException if the builder has already transitioned to
* the built state
*/
@Override
void accept(T t);
/**
* Adds an element to the stream being built.
*
@ -1363,18 +1359,6 @@ public class FastStream<T> implements Stream<T>, Iterable<T> {
accept(t);
return this;
}
/**
* Builds the stream, transitioning this builder to the built state.
* An {@code IllegalStateException} is thrown if there are further attempts
* to operate on the builder after it has entered the built state.
*
* @return the built stream
* @throws IllegalStateException if the builder has already transitioned to
* the built state
*/
FastStream<T> build();
}
}

View File

@ -65,7 +65,10 @@ public class FastStreamTest {
List<Integer> orders = Arrays.asList(1, 2, 3);
List<String> list = Arrays.asList("dromara", "hutool", "sweet");
Map<Integer, String> toZip = FastStream.of(orders).toZip(list);
Assert.assertEquals(new HashMap<Integer, String>() {{
Assert.assertEquals(new HashMap<Integer, String>() {
private static final long serialVersionUID = 1L;
{
put(1, "dromara");
put(2, "hutool");
put(3, "sweet");
@ -85,7 +88,10 @@ public class FastStreamTest {
public void testToMap() {
List<Integer> list = Arrays.asList(1, 2, 3);
Map<String, Integer> identityMap = FastStream.of(list).toMap(String::valueOf);
Assert.assertEquals(new HashMap<String, Integer>() {{
Assert.assertEquals(new HashMap<String, Integer>() {
private static final long serialVersionUID = 1L;
{
put("1", 1);
put("2", 2);
put("3", 3);
@ -97,7 +103,10 @@ public class FastStreamTest {
List<Integer> list = Arrays.asList(1, 2, 3);
Map<String, List<Integer>> group = FastStream.of(list).group(String::valueOf);
Assert.assertEquals(
new HashMap<String, List<Integer>>() {{
new HashMap<String, List<Integer>>() {
private static final long serialVersionUID = 1L;
{
put("1", singletonList(1));
put("2", singletonList(2));
put("3", singletonList(3));
@ -270,7 +279,7 @@ public class FastStreamTest {
}
@Test
void testSub() {
public 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),
@ -280,7 +289,7 @@ public class FastStreamTest {
}
@Test
void testSubList() {
public 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),