StreamUtil.of方法新增对 Iterator 支持

This commit is contained in:
青韵
2022-09-17 10:52:09 +08:00
parent 351f5305d9
commit 46daf6c1c0
2 changed files with 56 additions and 0 deletions

View File

@@ -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 ================
}