mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
!807 StreamUtil.of方法新增对 Iterator 支持;StreamUtil.of(Iterable) 方法优化
Merge pull request !807 from 这是一个腊鸡/v5-dev
This commit is contained in:
commit
f897da013f
@ -1,6 +1,5 @@
|
|||||||
package cn.hutool.core.stream;
|
package cn.hutool.core.stream;
|
||||||
|
|
||||||
import cn.hutool.core.collection.CollUtil;
|
|
||||||
import cn.hutool.core.io.IORuntimeException;
|
import cn.hutool.core.io.IORuntimeException;
|
||||||
import cn.hutool.core.lang.Assert;
|
import cn.hutool.core.lang.Assert;
|
||||||
import cn.hutool.core.util.CharsetUtil;
|
import cn.hutool.core.util.CharsetUtil;
|
||||||
@ -10,6 +9,8 @@ import java.io.IOException;
|
|||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.Spliterators;
|
import java.util.Spliterators;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.function.UnaryOperator;
|
import java.util.function.UnaryOperator;
|
||||||
@ -51,9 +52,34 @@ public class StreamUtil {
|
|||||||
*/
|
*/
|
||||||
public static <T> Stream<T> of(Iterable<T> iterable, boolean parallel) {
|
public static <T> Stream<T> of(Iterable<T> iterable, boolean parallel) {
|
||||||
Assert.notNull(iterable, "Iterable must be not null!");
|
Assert.notNull(iterable, "Iterable must be not null!");
|
||||||
return StreamSupport.stream(
|
|
||||||
Spliterators.spliterator(CollUtil.toCollection(iterable), 0),
|
return iterable instanceof Collection ?
|
||||||
parallel);
|
((Collection<T>) iterable).stream() :
|
||||||
|
StreamSupport.stream(iterable.spliterator(), parallel);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link Iterator} 转换为 {@link Stream}
|
||||||
|
* @param iterator 迭代器
|
||||||
|
* @param <T> 集合元素类型
|
||||||
|
* @return {@link Stream}
|
||||||
|
* @throws IllegalArgumentException 如果iterator为null,抛出该异常
|
||||||
|
*/
|
||||||
|
public static <T> Stream<T> of(Iterator<T> iterator) {
|
||||||
|
return of(iterator, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link Iterator} 转换为 {@link Stream}
|
||||||
|
* @param iterator 迭代器
|
||||||
|
* @param parallel 是否并行
|
||||||
|
* @param <T> 集合元素类型
|
||||||
|
* @return {@link Stream}
|
||||||
|
* @throws IllegalArgumentException 如果iterator为null,抛出该异常
|
||||||
|
*/
|
||||||
|
public static <T> Stream<T> of(Iterator<T> iterator, boolean parallel) {
|
||||||
|
Assert.notNull(iterator, "iterator must not be null!");
|
||||||
|
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, 0), parallel);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,8 +1,13 @@
|
|||||||
package cn.hutool.core.stream;
|
package cn.hutool.core.stream;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
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;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
public class StreamUtilTest {
|
public class StreamUtilTest {
|
||||||
@ -13,4 +18,30 @@ public class StreamUtilTest {
|
|||||||
final String result = stream.collect(CollectorUtil.joining(","));
|
final String result = stream.collect(CollectorUtil.joining(","));
|
||||||
Assert.assertEquals("2,4,8,16", result);
|
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 ================
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user