mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
!807 StreamUtil.of方法新增对 Iterator 支持;StreamUtil.of(Iterable) 方法优化
Merge pull request !807 from 这是一个腊鸡/v5-dev
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
package cn.hutool.core.stream;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class StreamUtilTest {
|
||||
@@ -13,4 +18,30 @@ public class StreamUtilTest {
|
||||
final String result = stream.collect(CollectorUtil.joining(","));
|
||||
Assert.assertEquals("2,4,8,16", result);
|
||||
}
|
||||
|
||||
// === iterator ===
|
||||
@Test
|
||||
public void streamTestNullIterator() {
|
||||
Assert.assertThrows(IllegalArgumentException.class, () -> StreamUtil.of((Iterator<Object>) null));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void streamTestEmptyIterator() {
|
||||
assertStreamIsEmpty(StreamUtil.of(new ArrayList<>().iterator()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void streamTestOrdinaryIterator() {
|
||||
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);
|
||||
Assert.assertEquals(hashSet, StreamUtil.of(hashSet.iterator()).collect(Collectors.toSet()));
|
||||
}
|
||||
|
||||
void assertStreamIsEmpty(Stream<?> stream) {
|
||||
Assert.assertNotNull(stream);
|
||||
Assert.assertEquals(0, stream.toArray().length);
|
||||
}
|
||||
// ================ stream test end ================
|
||||
}
|
||||
|
Reference in New Issue
Block a user