StreamUtil.of方法新增对 Iterator 支持;StreamUtil.of(Iterable) 方法优化

This commit is contained in:
Looly 2022-09-19 09:52:40 +08:00
parent f897da013f
commit 1af96de3ae
3 changed files with 14 additions and 6 deletions

View File

@ -3,9 +3,10 @@
-------------------------------------------------------------------------------------------------------------
# 5.8.8.M1 (2022-09-17)
# 5.8.8.M1 (2022-09-19)
### 🐣新特性
* 【core 】 StreamUtil.of方法新增对 Iterator 支持StreamUtil.of(Iterable) 方法优化pr#807@Gitee
### 🐞Bug修复
* 【core 】 修复FileNameUtil.cleanInvalid无法去除换行符问题issue#I5RMZV@Gitee

View File

@ -54,7 +54,7 @@ public class StreamUtil {
Assert.notNull(iterable, "Iterable must be not null!");
return iterable instanceof Collection ?
((Collection<T>) iterable).stream() :
parallel ? ((Collection<T>) iterable).parallelStream() : ((Collection<T>) iterable).stream() :
StreamSupport.stream(iterable.spliterator(), parallel);
}

View File

@ -5,6 +5,7 @@ import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.stream.Collectors;
@ -25,21 +26,27 @@ public class StreamUtilTest {
Assert.assertThrows(IllegalArgumentException.class, () -> StreamUtil.of((Iterator<Object>) null));
}
@SuppressWarnings({"RedundantOperationOnEmptyContainer", "RedundantCollectionOperation"})
@Test
public void streamTestEmptyIterator() {
public void streamTestEmptyListToIterator() {
assertStreamIsEmpty(StreamUtil.of(new ArrayList<>().iterator()));
}
@Test
public void streamTestEmptyIterator() {
assertStreamIsEmpty(StreamUtil.of(Collections.emptyIterator()));
}
@Test
public void streamTestOrdinaryIterator() {
ArrayList<Integer> arrayList = CollUtil.newArrayList(1, 2, 3);
final ArrayList<Integer> arrayList = CollUtil.newArrayList(1, 2, 3);
Assert.assertArrayEquals(new Integer[]{1, 2, 3}, StreamUtil.of(arrayList.iterator()).toArray());
HashSet<Integer> hashSet = CollUtil.newHashSet(1, 2, 3);
final HashSet<Integer> hashSet = CollUtil.newHashSet(1, 2, 3);
Assert.assertEquals(hashSet, StreamUtil.of(hashSet.iterator()).collect(Collectors.toSet()));
}
void assertStreamIsEmpty(Stream<?> stream) {
void assertStreamIsEmpty(final Stream<?> stream) {
Assert.assertNotNull(stream);
Assert.assertEquals(0, stream.toArray().length);
}