mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
StreamUtil.of方法新增对 Iterator 支持;StreamUtil.of(Iterable) 方法优化
This commit is contained in:
parent
f897da013f
commit
1af96de3ae
@ -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修复
|
### 🐞Bug修复
|
||||||
* 【core 】 修复FileNameUtil.cleanInvalid无法去除换行符问题(issue#I5RMZV@Gitee)
|
* 【core 】 修复FileNameUtil.cleanInvalid无法去除换行符问题(issue#I5RMZV@Gitee)
|
||||||
|
@ -54,7 +54,7 @@ public class StreamUtil {
|
|||||||
Assert.notNull(iterable, "Iterable must be not null!");
|
Assert.notNull(iterable, "Iterable must be not null!");
|
||||||
|
|
||||||
return iterable instanceof Collection ?
|
return iterable instanceof Collection ?
|
||||||
((Collection<T>) iterable).stream() :
|
parallel ? ((Collection<T>) iterable).parallelStream() : ((Collection<T>) iterable).stream() :
|
||||||
StreamSupport.stream(iterable.spliterator(), parallel);
|
StreamSupport.stream(iterable.spliterator(), parallel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ import org.junit.Assert;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@ -25,21 +26,27 @@ public class StreamUtilTest {
|
|||||||
Assert.assertThrows(IllegalArgumentException.class, () -> StreamUtil.of((Iterator<Object>) null));
|
Assert.assertThrows(IllegalArgumentException.class, () -> StreamUtil.of((Iterator<Object>) null));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings({"RedundantOperationOnEmptyContainer", "RedundantCollectionOperation"})
|
||||||
@Test
|
@Test
|
||||||
public void streamTestEmptyIterator() {
|
public void streamTestEmptyListToIterator() {
|
||||||
assertStreamIsEmpty(StreamUtil.of(new ArrayList<>().iterator()));
|
assertStreamIsEmpty(StreamUtil.of(new ArrayList<>().iterator()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void streamTestEmptyIterator() {
|
||||||
|
assertStreamIsEmpty(StreamUtil.of(Collections.emptyIterator()));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void streamTestOrdinaryIterator() {
|
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());
|
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()));
|
Assert.assertEquals(hashSet, StreamUtil.of(hashSet.iterator()).collect(Collectors.toSet()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void assertStreamIsEmpty(Stream<?> stream) {
|
void assertStreamIsEmpty(final Stream<?> stream) {
|
||||||
Assert.assertNotNull(stream);
|
Assert.assertNotNull(stream);
|
||||||
Assert.assertEquals(0, stream.toArray().length);
|
Assert.assertEquals(0, stream.toArray().length);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user