mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
add methods
This commit is contained in:
parent
b0e62df7c6
commit
671b6fa6a9
@ -1,7 +1,15 @@
|
|||||||
package cn.hutool.core.stream;
|
package cn.hutool.core.stream;
|
||||||
|
|
||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
|
import cn.hutool.core.io.IORuntimeException;
|
||||||
|
import cn.hutool.core.lang.Assert;
|
||||||
|
import cn.hutool.core.util.CharsetUtil;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
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;
|
||||||
@ -17,11 +25,9 @@ import java.util.stream.StreamSupport;
|
|||||||
public class StreamUtil {
|
public class StreamUtil {
|
||||||
|
|
||||||
@SafeVarargs
|
@SafeVarargs
|
||||||
public static <T> Stream<T> of(T... values) {
|
public static <T> Stream<T> of(T... array) {
|
||||||
if (null == values) {
|
Assert.notNull(array, "Array must be not null!");
|
||||||
return null;
|
return Stream.of(array);
|
||||||
}
|
|
||||||
return Stream.of(values);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -44,14 +50,58 @@ public class StreamUtil {
|
|||||||
* @return {@link Stream}
|
* @return {@link Stream}
|
||||||
*/
|
*/
|
||||||
public static <T> Stream<T> of(Iterable<T> iterable, boolean parallel) {
|
public static <T> Stream<T> of(Iterable<T> iterable, boolean parallel) {
|
||||||
if (null == iterable) {
|
Assert.notNull(iterable, "Iterable must be not null!");
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return StreamSupport.stream(
|
return StreamSupport.stream(
|
||||||
Spliterators.spliterator(CollUtil.toCollection(iterable), 0),
|
Spliterators.spliterator(CollUtil.toCollection(iterable), 0),
|
||||||
parallel);
|
parallel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按行读取文件为{@link Stream}
|
||||||
|
*
|
||||||
|
* @param file 路径
|
||||||
|
* @return {@link Stream}
|
||||||
|
*/
|
||||||
|
public static Stream<String> of(File file) {
|
||||||
|
return of(file, CharsetUtil.CHARSET_UTF_8);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按行读取文件为{@link Stream}
|
||||||
|
*
|
||||||
|
* @param path 路径
|
||||||
|
* @return {@link Stream}
|
||||||
|
*/
|
||||||
|
public static Stream<String> of(Path path) {
|
||||||
|
return of(path, CharsetUtil.CHARSET_UTF_8);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按行读取文件为{@link Stream}
|
||||||
|
*
|
||||||
|
* @param charset 编码
|
||||||
|
* @return {@link Stream}
|
||||||
|
*/
|
||||||
|
public static Stream<String> of(File file, Charset charset) {
|
||||||
|
Assert.notNull(file, "File must be not null!");
|
||||||
|
return of(file.toPath(), charset);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 按行读取文件为{@link Stream}
|
||||||
|
*
|
||||||
|
* @param path 路径
|
||||||
|
* @param charset 编码
|
||||||
|
* @return {@link Stream}
|
||||||
|
*/
|
||||||
|
public static Stream<String> of(Path path, Charset charset) {
|
||||||
|
try {
|
||||||
|
return Files.lines(path, charset);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new IORuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 通过函数创建Stream
|
* 通过函数创建Stream
|
||||||
*
|
*
|
||||||
@ -68,9 +118,9 @@ public class StreamUtil {
|
|||||||
/**
|
/**
|
||||||
* 将Stream中所有元素以指定分隔符,合并为一个字符串,对象默认调用toString方法
|
* 将Stream中所有元素以指定分隔符,合并为一个字符串,对象默认调用toString方法
|
||||||
*
|
*
|
||||||
* @param stream {@link Stream}
|
* @param stream {@link Stream}
|
||||||
* @param delimiter 分隔符
|
* @param delimiter 分隔符
|
||||||
* @param <T> 元素类型
|
* @param <T> 元素类型
|
||||||
* @return 字符串
|
* @return 字符串
|
||||||
*/
|
*/
|
||||||
public static <T> String join(Stream<T> stream, CharSequence delimiter) {
|
public static <T> String join(Stream<T> stream, CharSequence delimiter) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user