mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
:trollface: 增强EasyStream#zip对并行流的支持,提供CollectorUtil.entryToMap对Entry转map提供支持,完善javadoc
This commit is contained in:
parent
b7d924b34d
commit
492a281cf9
@ -134,7 +134,7 @@ public class CollectorUtil {
|
|||||||
* @return {@link Collector}
|
* @return {@link Collector}
|
||||||
*/
|
*/
|
||||||
public static <T, K, A, D> Collector<T, ?, Map<K, D>> groupingBy(final Function<? super T, ? extends K> classifier,
|
public static <T, K, A, D> Collector<T, ?, Map<K, D>> groupingBy(final Function<? super T, ? extends K> classifier,
|
||||||
final Collector<? super T, A, D> downstream) {
|
final Collector<? super T, A, D> downstream) {
|
||||||
return groupingBy(classifier, HashMap::new, downstream);
|
return groupingBy(classifier, HashMap::new, downstream);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -273,14 +273,14 @@ public class CollectorUtil {
|
|||||||
/**
|
/**
|
||||||
* 将流转为{@link EntryStream}
|
* 将流转为{@link EntryStream}
|
||||||
*
|
*
|
||||||
* @param keyMapper 键的映射方法
|
* @param keyMapper 键的映射方法
|
||||||
* @param <T> 输入元素类型
|
* @param <T> 输入元素类型
|
||||||
* @param <K> 元素的键类型
|
* @param <K> 元素的键类型
|
||||||
* @return 收集器
|
* @return 收集器
|
||||||
* @since 6.0.0
|
* @since 6.0.0
|
||||||
*/
|
*/
|
||||||
public static <T, K> Collector<T, List<T>, EntryStream<K, T>> toEntryStream(
|
public static <T, K> Collector<T, List<T>, EntryStream<K, T>> toEntryStream(
|
||||||
Function<? super T, ? extends K> keyMapper) {
|
Function<? super T, ? extends K> keyMapper) {
|
||||||
return toEntryStream(keyMapper, Function.identity());
|
return toEntryStream(keyMapper, Function.identity());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -361,4 +361,14 @@ public class CollectorUtil {
|
|||||||
return transform(ArrayList::new, mapper);
|
return transform(ArrayList::new, mapper);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用于{@code Stream<Entry>} 转 Map 的情况
|
||||||
|
*
|
||||||
|
* @param <K> key类型
|
||||||
|
* @param <V> value类型
|
||||||
|
* @return map
|
||||||
|
*/
|
||||||
|
public static <K, V> Collector<Map.Entry<K, V>, ?, Map<K, V>> entryToMap() {
|
||||||
|
return toMap(Map.Entry::getKey, Map.Entry::getValue);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,52 +17,52 @@ import java.util.stream.Stream;
|
|||||||
*
|
*
|
||||||
* @param <T> 流中的元素类型
|
* @param <T> 流中的元素类型
|
||||||
* @param <S> {@link TerminableWrappedStream}的实现类类型
|
* @param <S> {@link TerminableWrappedStream}的实现类类型
|
||||||
* @author huangchengxing
|
* @author huangchengxing VampireAchao
|
||||||
* @since 6.0.0
|
* @since 6.0.0
|
||||||
*/
|
*/
|
||||||
public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T, S>> extends WrappedStream<T, S> {
|
public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T, S>> extends WrappedStream<T, S> {
|
||||||
|
|
||||||
// region ============ to collection ============
|
// region ============ to collection ============
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 转换为{@link ArrayList}
|
* 转换为{@link ArrayList}
|
||||||
*
|
*
|
||||||
* @return 集合
|
* @return 集合
|
||||||
* @see #toColl(Supplier)
|
|
||||||
*/
|
|
||||||
default List<T> toList() {
|
|
||||||
return this.toColl(ArrayList::new);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 换为不可变集合
|
|
||||||
*
|
|
||||||
* @return 集合
|
|
||||||
* @see #toColl(Supplier)
|
* @see #toColl(Supplier)
|
||||||
*/
|
*/
|
||||||
default List<T> toUnmodifiableList() {
|
default List<T> toList() {
|
||||||
return Collections.unmodifiableList(this.toList());
|
return this.toColl(ArrayList::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 转换为HashSet
|
* 换为不可变集合
|
||||||
*
|
*
|
||||||
* @return 集合
|
* @return 集合
|
||||||
* @see #toColl(Supplier)
|
* @see #toColl(Supplier)
|
||||||
*/
|
*/
|
||||||
default Set<T> toSet() {
|
default List<T> toUnmodifiableList() {
|
||||||
return this.toColl(HashSet::new);
|
return Collections.unmodifiableList(this.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 换为不可变集合
|
* 转换为HashSet
|
||||||
*
|
*
|
||||||
* @return 集合
|
* @return 集合
|
||||||
* @see #toColl(Supplier)
|
* @see #toColl(Supplier)
|
||||||
*/
|
*/
|
||||||
default Set<T> toUnmodifiableSet() {
|
default Set<T> toSet() {
|
||||||
return Collections.unmodifiableSet(this.toSet());
|
return this.toColl(HashSet::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 换为不可变集合
|
||||||
|
*
|
||||||
|
* @return 集合
|
||||||
|
* @see #toColl(Supplier)
|
||||||
|
*/
|
||||||
|
default Set<T> toUnmodifiableSet() {
|
||||||
|
return Collections.unmodifiableSet(this.toSet());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 转换成集合
|
* 转换成集合
|
||||||
@ -76,145 +76,145 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
|
|||||||
return unwrap().collect(Collectors.toCollection(collectionFactory));
|
return unwrap().collect(Collectors.toCollection(collectionFactory));
|
||||||
}
|
}
|
||||||
|
|
||||||
// endregion
|
// endregion
|
||||||
|
|
||||||
// region ============ to map ============
|
// region ============ to map ============
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 转换为map,key为给定操作执行后的返回值,value为当前元素
|
* 转换为map,key为给定操作执行后的返回值,value为当前元素
|
||||||
*
|
*
|
||||||
* @param keyMapper 指定的key操作
|
* @param keyMapper 指定的key操作
|
||||||
* @param <K> key类型
|
* @param <K> key类型
|
||||||
* @return map
|
* @return map
|
||||||
* @see #toMap(Function, Function, BinaryOperator, Supplier)
|
* @see #toMap(Function, Function, BinaryOperator, Supplier)
|
||||||
*/
|
*/
|
||||||
default <K> Map<K, T> toMap(final Function<? super T, ? extends K> keyMapper) {
|
default <K> Map<K, T> toMap(final Function<? super T, ? extends K> keyMapper) {
|
||||||
return this.toMap(keyMapper, Function.identity());
|
return this.toMap(keyMapper, Function.identity());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 转换为map,key,value为给定操作执行后的返回值
|
* 转换为map,key,value为给定操作执行后的返回值
|
||||||
*
|
*
|
||||||
* @param keyMapper 指定的key操作
|
* @param keyMapper 指定的key操作
|
||||||
* @param valueMapper 指定value操作
|
* @param valueMapper 指定value操作
|
||||||
* @param <K> key类型
|
* @param <K> key类型
|
||||||
* @param <U> value类型
|
* @param <U> value类型
|
||||||
* @return map
|
* @return map
|
||||||
* @see #toMap(Function, Function, BinaryOperator, Supplier)
|
* @see #toMap(Function, Function, BinaryOperator, Supplier)
|
||||||
*/
|
*/
|
||||||
default <K, U> Map<K, U> toMap(
|
default <K, U> Map<K, U> toMap(
|
||||||
final Function<? super T, ? extends K> keyMapper, final Function<? super T, ? extends U> valueMapper) {
|
final Function<? super T, ? extends K> keyMapper, final Function<? super T, ? extends U> valueMapper) {
|
||||||
return this.toMap(keyMapper, valueMapper, (l, r) -> r);
|
return this.toMap(keyMapper, valueMapper, (l, r) -> r);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 转换为不可变map,key,value为给定操作执行后的返回值
|
* 转换为不可变map,key,value为给定操作执行后的返回值
|
||||||
*
|
*
|
||||||
* @param keyMapper 指定的key操作
|
* @param keyMapper 指定的key操作
|
||||||
* @param valueMapper 指定value操作
|
* @param valueMapper 指定value操作
|
||||||
* @param <K> key类型
|
* @param <K> key类型
|
||||||
* @param <U> value类型
|
* @param <U> value类型
|
||||||
* @return map
|
* @return map
|
||||||
* @see #toMap(Function, Function, BinaryOperator, Supplier)
|
* @see #toMap(Function, Function, BinaryOperator, Supplier)
|
||||||
*/
|
*/
|
||||||
default <K, U> Map<K, U> toUnmodifiableMap(
|
default <K, U> Map<K, U> toUnmodifiableMap(
|
||||||
final Function<? super T, ? extends K> keyMapper, final Function<? super T, ? extends U> valueMapper) {
|
final Function<? super T, ? extends K> keyMapper, final Function<? super T, ? extends U> valueMapper) {
|
||||||
return Collections.unmodifiableMap(this.toMap(keyMapper, valueMapper));
|
return Collections.unmodifiableMap(this.toMap(keyMapper, valueMapper));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 转换为map,key,value为给定操作执行后的返回值
|
* 转换为map,key,value为给定操作执行后的返回值
|
||||||
*
|
*
|
||||||
* @param keyMapper 指定的key操作
|
* @param keyMapper 指定的key操作
|
||||||
* @param valueMapper 指定value操作
|
* @param valueMapper 指定value操作
|
||||||
* @param mergeFunction 合并操作
|
* @param mergeFunction 合并操作
|
||||||
* @param <K> key类型
|
* @param <K> key类型
|
||||||
* @param <U> value类型
|
* @param <U> value类型
|
||||||
* @return map
|
* @return map
|
||||||
* @see #toMap(Function, Function, BinaryOperator, Supplier)
|
* @see #toMap(Function, Function, BinaryOperator, Supplier)
|
||||||
*/
|
*/
|
||||||
default <K, U> Map<K, U> toMap(
|
default <K, U> Map<K, U> toMap(
|
||||||
final Function<? super T, ? extends K> keyMapper,
|
final Function<? super T, ? extends K> keyMapper,
|
||||||
final Function<? super T, ? extends U> valueMapper,
|
final Function<? super T, ? extends U> valueMapper,
|
||||||
final BinaryOperator<U> mergeFunction) {
|
final BinaryOperator<U> mergeFunction) {
|
||||||
return this.toMap(keyMapper, valueMapper, mergeFunction, HashMap::new);
|
return this.toMap(keyMapper, valueMapper, mergeFunction, HashMap::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 转换为不可变map,key,value为给定操作执行后的返回值
|
* 转换为不可变map,key,value为给定操作执行后的返回值
|
||||||
*
|
*
|
||||||
* @param keyMapper 指定的key操作
|
* @param keyMapper 指定的key操作
|
||||||
* @param valueMapper 指定value操作
|
* @param valueMapper 指定value操作
|
||||||
* @param mergeFunction 合并操作
|
* @param mergeFunction 合并操作
|
||||||
* @param <K> key类型
|
* @param <K> key类型
|
||||||
* @param <U> value类型
|
* @param <U> value类型
|
||||||
* @return map
|
* @return map
|
||||||
* @see #toMap(Function, Function, BinaryOperator, Supplier)
|
* @see #toMap(Function, Function, BinaryOperator, Supplier)
|
||||||
*/
|
*/
|
||||||
default <K, U> Map<K, U> toUnmodifiableMap(
|
default <K, U> Map<K, U> toUnmodifiableMap(
|
||||||
final Function<? super T, ? extends K> keyMapper,
|
final Function<? super T, ? extends K> keyMapper,
|
||||||
final Function<? super T, ? extends U> valueMapper,
|
final Function<? super T, ? extends U> valueMapper,
|
||||||
final BinaryOperator<U> mergeFunction) {
|
final BinaryOperator<U> mergeFunction) {
|
||||||
return Collections.unmodifiableMap(
|
return Collections.unmodifiableMap(
|
||||||
this.toMap(keyMapper, valueMapper, mergeFunction, HashMap::new)
|
this.toMap(keyMapper, valueMapper, mergeFunction, HashMap::new)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 转换为map,key,value为给定操作执行后的返回值
|
* 转换为map,key,value为给定操作执行后的返回值
|
||||||
*
|
*
|
||||||
* @param keyMapper 指定的key操作
|
* @param keyMapper 指定的key操作
|
||||||
* @param valueMapper 指定value操作
|
* @param valueMapper 指定value操作
|
||||||
* @param mergeFunction 合并操作
|
* @param mergeFunction 合并操作
|
||||||
* @param mapSupplier map工厂
|
* @param mapSupplier map工厂
|
||||||
* @param <K> key类型
|
* @param <K> key类型
|
||||||
* @param <U> value类型
|
* @param <U> value类型
|
||||||
* @param <M> map类型
|
* @param <M> map类型
|
||||||
* @return map
|
* @return map
|
||||||
*/
|
*/
|
||||||
default <K, U, M extends Map<K, U>> M toMap(
|
default <K, U, M extends Map<K, U>> M toMap(
|
||||||
final Function<? super T, ? extends K> keyMapper,
|
final Function<? super T, ? extends K> keyMapper,
|
||||||
final Function<? super T, ? extends U> valueMapper,
|
final Function<? super T, ? extends U> valueMapper,
|
||||||
final BinaryOperator<U> mergeFunction,
|
final BinaryOperator<U> mergeFunction,
|
||||||
Supplier<M> mapSupplier) {
|
Supplier<M> mapSupplier) {
|
||||||
Objects.requireNonNull(keyMapper);
|
Objects.requireNonNull(keyMapper);
|
||||||
Objects.requireNonNull(valueMapper);
|
Objects.requireNonNull(valueMapper);
|
||||||
Objects.requireNonNull(mergeFunction);
|
Objects.requireNonNull(mergeFunction);
|
||||||
Objects.requireNonNull(mapSupplier);
|
Objects.requireNonNull(mapSupplier);
|
||||||
return unwrap().collect(Collectors.toMap(keyMapper, valueMapper, mergeFunction, mapSupplier));
|
return unwrap().collect(Collectors.toMap(keyMapper, valueMapper, mergeFunction, mapSupplier));
|
||||||
}
|
}
|
||||||
|
|
||||||
// endregion
|
// endregion
|
||||||
|
|
||||||
// region ============ to zip ============
|
// region ============ to zip ============
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 与给定的可迭代对象转换成map,key为现有元素,value为给定可迭代对象迭代的元素<br>
|
* 与给定的可迭代对象转换成map,key为现有元素,value为给定可迭代对象迭代的元素<br>
|
||||||
* 至少包含全部的key,如果对应位置上的value不存在,则为null
|
* 至少包含全部的key,如果对应位置上的value不存在,则为null
|
||||||
*
|
*
|
||||||
* @param other 可迭代对象
|
* @param other 可迭代对象
|
||||||
* @param <R> 可迭代对象迭代的元素类型
|
* @param <R> 可迭代对象迭代的元素类型
|
||||||
* @return map,key为现有元素,value为给定可迭代对象迭代的元素;<br>
|
* @return map,key为现有元素,value为给定可迭代对象迭代的元素;<br>
|
||||||
* 至少包含全部的key,如果对应位置上的value不存在,则为null;<br>
|
* 至少包含全部的key,如果对应位置上的value不存在,则为null;<br>
|
||||||
* 如果key重复, 则保留最后一个关联的value;<br>
|
* 如果key重复, 则保留最后一个关联的value;<br>
|
||||||
*/
|
*/
|
||||||
default <R> Map<T, R> toZip(final Iterable<R> other) {
|
default <R> Map<T, R> toZip(final Iterable<R> other) {
|
||||||
Objects.requireNonNull(other);
|
Objects.requireNonNull(other);
|
||||||
// value对象迭代器
|
// value对象迭代器
|
||||||
final Iterator<R> iterator = Opt.ofNullable(other).map(Iterable::iterator).orElseGet(Collections::emptyIterator);
|
final Iterator<R> iterator = Opt.ofNullable(other).map(Iterable::iterator).orElseGet(Collections::emptyIterator);
|
||||||
if (this.isParallel()) {
|
if (this.isParallel()) {
|
||||||
final List<T> keyList = toList();
|
final List<T> keyList = toList();
|
||||||
final Map<T, R> map = new HashMap<>(keyList.size());
|
final Map<T, R> map = new HashMap<>(keyList.size());
|
||||||
for (T key : keyList) {
|
for (T key : keyList) {
|
||||||
map.put(key, iterator.hasNext() ? iterator.next() : null);
|
map.put(key, iterator.hasNext() ? iterator.next() : null);
|
||||||
}
|
}
|
||||||
return map;
|
return map;
|
||||||
} else {
|
} else {
|
||||||
return this.toMap(Function.identity(), e -> iterator.hasNext() ? iterator.next() : null);
|
return this.toMap(Function.identity(), e -> iterator.hasNext() ? iterator.next() : null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// endregion
|
// endregion
|
||||||
|
|
||||||
// region ============ to optional ============
|
// region ============ to optional ============
|
||||||
|
|
||||||
@ -408,7 +408,7 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
|
|||||||
* @see #group(Function, Supplier, Collector)
|
* @see #group(Function, Supplier, Collector)
|
||||||
*/
|
*/
|
||||||
default <K, A, D> Map<K, D> group(
|
default <K, A, D> Map<K, D> group(
|
||||||
final Function<? super T, ? extends K> classifier, final Collector<? super T, A, D> downstream) {
|
final Function<? super T, ? extends K> classifier, final Collector<? super T, A, D> downstream) {
|
||||||
return this.group(classifier, HashMap::new, downstream);
|
return this.group(classifier, HashMap::new, downstream);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -426,9 +426,9 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
|
|||||||
* @see CollectorUtil#groupingBy(Function, Supplier, Collector)
|
* @see CollectorUtil#groupingBy(Function, Supplier, Collector)
|
||||||
*/
|
*/
|
||||||
default <K, D, A, M extends Map<K, D>> M group(
|
default <K, D, A, M extends Map<K, D>> M group(
|
||||||
final Function<? super T, ? extends K> classifier,
|
final Function<? super T, ? extends K> classifier,
|
||||||
final Supplier<M> mapFactory,
|
final Supplier<M> mapFactory,
|
||||||
final Collector<? super T, A, D> downstream) {
|
final Collector<? super T, A, D> downstream) {
|
||||||
Objects.requireNonNull(classifier);
|
Objects.requireNonNull(classifier);
|
||||||
Objects.requireNonNull(mapFactory);
|
Objects.requireNonNull(mapFactory);
|
||||||
Objects.requireNonNull(downstream);
|
Objects.requireNonNull(downstream);
|
||||||
@ -449,8 +449,9 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
|
|||||||
/**
|
/**
|
||||||
* 根据给定判断条件分组
|
* 根据给定判断条件分组
|
||||||
*
|
*
|
||||||
* @param predicate 判断条件
|
* @param predicate 判断条件
|
||||||
* @param collFactory 提供的集合
|
* @param collFactory 提供的集合
|
||||||
|
* @param <C> 集合类型
|
||||||
* @return map
|
* @return map
|
||||||
* @see #partition(Predicate, Collector)
|
* @see #partition(Predicate, Collector)
|
||||||
*/
|
*/
|
||||||
@ -463,7 +464,7 @@ public interface TerminableWrappedStream<T, S extends TerminableWrappedStream<T,
|
|||||||
*
|
*
|
||||||
* @param predicate 判断条件
|
* @param predicate 判断条件
|
||||||
* @param downstream 下游操作
|
* @param downstream 下游操作
|
||||||
* @param <R> 返回值类型
|
* @param <R> 返回值类型
|
||||||
* @return map
|
* @return map
|
||||||
*/
|
*/
|
||||||
default <R> Map<Boolean, R> partition(final Predicate<T> predicate, final Collector<T, ?, R> downstream) {
|
default <R> Map<Boolean, R> partition(final Predicate<T> predicate, final Collector<T, ?, R> downstream) {
|
||||||
|
@ -3,7 +3,6 @@ package cn.hutool.core.stream;
|
|||||||
import cn.hutool.core.collection.ListUtil;
|
import cn.hutool.core.collection.ListUtil;
|
||||||
import cn.hutool.core.collection.iter.IterUtil;
|
import cn.hutool.core.collection.iter.IterUtil;
|
||||||
import cn.hutool.core.lang.Console;
|
import cn.hutool.core.lang.Console;
|
||||||
import cn.hutool.core.lang.Opt;
|
|
||||||
import cn.hutool.core.lang.mutable.MutableInt;
|
import cn.hutool.core.lang.mutable.MutableInt;
|
||||||
import cn.hutool.core.lang.mutable.MutableObj;
|
import cn.hutool.core.lang.mutable.MutableObj;
|
||||||
import cn.hutool.core.map.MapUtil;
|
import cn.hutool.core.map.MapUtil;
|
||||||
@ -24,7 +23,7 @@ import java.util.stream.StreamSupport;
|
|||||||
*
|
*
|
||||||
* @param <T> 流中的元素类型
|
* @param <T> 流中的元素类型
|
||||||
* @param <S> {@link TransformableWrappedStream}的实现类类型
|
* @param <S> {@link TransformableWrappedStream}的实现类类型
|
||||||
* @author huangchengxing
|
* @author huangchengxing VampireAchao
|
||||||
* @since 6.0.0
|
* @since 6.0.0
|
||||||
*/
|
*/
|
||||||
public interface TransformableWrappedStream<T, S extends TransformableWrappedStream<T, S>> extends WrappedStream<T, S> {
|
public interface TransformableWrappedStream<T, S extends TransformableWrappedStream<T, S>> extends WrappedStream<T, S> {
|
||||||
@ -40,24 +39,15 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
|
|||||||
* @return 合并后的结果对象的流
|
* @return 合并后的结果对象的流
|
||||||
*/
|
*/
|
||||||
default <U, R> EasyStream<R> zip(
|
default <U, R> EasyStream<R> zip(
|
||||||
final Iterable<U> other,
|
final Iterable<U> other,
|
||||||
final BiFunction<? super T, ? super U, ? extends R> zipper) {
|
final BiFunction<? super T, ? super U, ? extends R> zipper) {
|
||||||
Objects.requireNonNull(zipper);
|
Objects.requireNonNull(zipper);
|
||||||
final Spliterator<T> keys = spliterator();
|
Map<Integer, T> idxIdentityMap = mapIdx((e, idx) -> MapUtil.entry(idx, e)).collect(CollectorUtil.entryToMap());
|
||||||
final Spliterator<U> values = Opt.ofNullable(other).map(Iterable::spliterator).orElseGet(Spliterators::emptySpliterator);
|
Map<Integer, U> idxOtherMap = EasyStream.of(other).mapIdx((e, idx) -> MapUtil.entry(idx, e)).collect(CollectorUtil.entryToMap());
|
||||||
// 获取两个Spliterator的中较小的数量
|
if (idxIdentityMap.size() <= idxOtherMap.size()) {
|
||||||
// 如果Spliterator经过流操作, getExactSizeIfKnown()可能会返回-1, 所以默认大小为 ArrayList.DEFAULT_CAPACITY
|
return EasyStream.of(idxIdentityMap.keySet(), isParallel()).map(k -> zipper.apply(idxIdentityMap.get(k), idxOtherMap.get(k)));
|
||||||
final int sizeIfKnown = (int) Math.max(Math.min(keys.getExactSizeIfKnown(), values.getExactSizeIfKnown()), 10);
|
|
||||||
final List<R> list = new ArrayList<>(sizeIfKnown);
|
|
||||||
// 保存第一个Spliterator的值
|
|
||||||
final MutableObj<T> key = new MutableObj<>();
|
|
||||||
// 保存第二个Spliterator的值
|
|
||||||
final MutableObj<U> value = new MutableObj<>();
|
|
||||||
// 当两个Spliterator中都还有剩余元素时
|
|
||||||
while (keys.tryAdvance(key::set) && values.tryAdvance(value::set)) {
|
|
||||||
list.add(zipper.apply(key.get(), value.get()));
|
|
||||||
}
|
}
|
||||||
return EasyStream.of(list).parallel(isParallel()).onClose(unwrap()::close);
|
return EasyStream.of(idxOtherMap.keySet(), isParallel()).map(k -> zipper.apply(idxIdentityMap.get(k), idxOtherMap.get(k)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -78,9 +68,9 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
|
|||||||
return EasyStream.<EasyStream<T>>of(EasyStream.of(list, isParallel()));
|
return EasyStream.<EasyStream<T>>of(EasyStream.of(list, isParallel()));
|
||||||
}
|
}
|
||||||
return EasyStream.iterate(0, i -> i < size, i -> i + batchSize)
|
return EasyStream.iterate(0, i -> i < size, i -> i + batchSize)
|
||||||
.map(skip -> EasyStream.of(list.subList(skip, Math.min(size, skip + batchSize)), isParallel()))
|
.map(skip -> EasyStream.of(list.subList(skip, Math.min(size, skip + batchSize)), isParallel()))
|
||||||
.parallel(isParallel())
|
.parallel(isParallel())
|
||||||
.onClose(unwrap()::close);
|
.onClose(unwrap()::close);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -114,8 +104,8 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
|
|||||||
/**
|
/**
|
||||||
* 将当前流转为键值对流
|
* 将当前流转为键值对流
|
||||||
*
|
*
|
||||||
* @param keyMapper 键的映射方法
|
* @param keyMapper 键的映射方法
|
||||||
* @param <K> 键类型
|
* @param <K> 键类型
|
||||||
* @return {@link EntryStream}实例
|
* @return {@link EntryStream}实例
|
||||||
*/
|
*/
|
||||||
default <K> EntryStream<K, T> toEntries(final Function<T, K> keyMapper) {
|
default <K> EntryStream<K, T> toEntries(final Function<T, K> keyMapper) {
|
||||||
@ -158,7 +148,7 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
|
|||||||
default S splice(final int start, final int deleteCount, final T... items) {
|
default S splice(final int start, final int deleteCount, final T... items) {
|
||||||
final List<T> elements = unwrap().collect(Collectors.toList());
|
final List<T> elements = unwrap().collect(Collectors.toList());
|
||||||
return wrap(ListUtil.splice(elements, start, deleteCount, items).stream())
|
return wrap(ListUtil.splice(elements, start, deleteCount, items).stream())
|
||||||
.parallel(isParallel());
|
.parallel(isParallel());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -191,15 +181,15 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <<p>删除流中与断言匹配的元素,当遇到第一个不匹配的元素时终止,返回由剩余不匹配的元素组成的流。<br>
|
* 删除流中与断言匹配的元素,当遇到第一个不匹配的元素时终止,返回由剩余不匹配的元素组成的流。
|
||||||
* eg:
|
* eg:
|
||||||
* <pre>{@code
|
* <pre>{@code
|
||||||
* EasyStream.of(1, 2, 3, 4, 5)
|
* EasyStream.of(1, 2, 3, 4, 5)
|
||||||
* .dropWhile(i -> !Objects.equals(3, i)) // 删除不为3的元素,一直到遇到第一个3为止
|
* .dropWhile(i -> !Objects.equals(3, i)) // 删除不为3的元素,一直到遇到第一个3为止
|
||||||
* .toList(); // = [3, 4, 5]
|
* .toList(); // = [3, 4, 5]
|
||||||
* }</pre>
|
* }</pre>
|
||||||
*
|
* <p>
|
||||||
* <p>与{@code JDK9}中的{@code dropWhile}方法不太一样,此操作为顺序且有状态的中间操作。
|
* 与{@code JDK9}中的{@code dropWhile}方法不太一样,此操作为顺序且有状态的中间操作。
|
||||||
* 即使在并行流中,该操作仍然是顺序执行的,并且不影响后续的并行操作:
|
* 即使在并行流中,该操作仍然是顺序执行的,并且不影响后续的并行操作:
|
||||||
* <pre>{@code
|
* <pre>{@code
|
||||||
* EasyStream.iterate(1, i -> i + 1)
|
* EasyStream.iterate(1, i -> i + 1)
|
||||||
@ -261,6 +251,7 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
|
|||||||
/**
|
/**
|
||||||
* 返回与指定函数将元素作为参数执行后组成的流。操作带下标,并行流时下标永远为-1
|
* 返回与指定函数将元素作为参数执行后组成的流。操作带下标,并行流时下标永远为-1
|
||||||
* 这是一个无状态中间操作
|
* 这是一个无状态中间操作
|
||||||
|
*
|
||||||
* @param action 指定的函数
|
* @param action 指定的函数
|
||||||
* @return 返回叠加操作后的FastStream
|
* @return 返回叠加操作后的FastStream
|
||||||
* @apiNote 该方法存在的意义主要是用来调试
|
* @apiNote 该方法存在的意义主要是用来调试
|
||||||
@ -490,8 +481,8 @@ public interface TransformableWrappedStream<T, S extends TransformableWrappedStr
|
|||||||
Objects.requireNonNull(childrenSetter);
|
Objects.requireNonNull(childrenSetter);
|
||||||
final MutableObj<Function<T, EasyStream<T>>> recursiveRef = new MutableObj<>();
|
final MutableObj<Function<T, EasyStream<T>>> recursiveRef = new MutableObj<>();
|
||||||
final Function<T, EasyStream<T>> recursive = e -> EasyStream.of(childrenGetter.apply(e))
|
final Function<T, EasyStream<T>> recursive = e -> EasyStream.of(childrenGetter.apply(e))
|
||||||
.flat(recursiveRef.get())
|
.flat(recursiveRef.get())
|
||||||
.unshift(e);
|
.unshift(e);
|
||||||
recursiveRef.set(recursive);
|
recursiveRef.set(recursive);
|
||||||
return wrap(flatMap(recursive).peek(e -> childrenSetter.accept(e, null)));
|
return wrap(flatMap(recursive).peek(e -> childrenSetter.accept(e, null)));
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ public class AbstractEnhancedWrappedStreamTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testToUnmodifiableList() {
|
public void testToUnmodifiableList() {
|
||||||
final List<Integer> list = wrap(1, 2, 3)
|
final List<Integer> list = wrap(1, 2, 3)
|
||||||
.toUnmodifiableList();
|
.toUnmodifiableList();
|
||||||
Assert.assertThrows(UnsupportedOperationException.class, () -> list.remove(0));
|
Assert.assertThrows(UnsupportedOperationException.class, () -> list.remove(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,7 +47,7 @@ public class AbstractEnhancedWrappedStreamTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testToUnmodifiableSet() {
|
public void testToUnmodifiableSet() {
|
||||||
final Set<Integer> set = wrap(1, 2, 3)
|
final Set<Integer> set = wrap(1, 2, 3)
|
||||||
.toUnmodifiableSet();
|
.toUnmodifiableSet();
|
||||||
Assert.assertThrows(UnsupportedOperationException.class, () -> set.remove(0));
|
Assert.assertThrows(UnsupportedOperationException.class, () -> set.remove(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -98,8 +98,8 @@ public class AbstractEnhancedWrappedStreamTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testFindFirst() {
|
public void testFindFirst() {
|
||||||
final List<Integer> list = asList(1, 2, 3);
|
final List<Integer> list = asList(1, 2, 3);
|
||||||
Assert.assertEquals((Integer)1, wrap(list).findFirst(t -> (t & 1) == 1).orElse(null));
|
Assert.assertEquals((Integer) 1, wrap(list).findFirst(t -> (t & 1) == 1).orElse(null));
|
||||||
Assert.assertEquals((Integer)1, wrap(list).filter(t -> (t & 1) == 1).findFirst().orElse(null));
|
Assert.assertEquals((Integer) 1, wrap(list).filter(t -> (t & 1) == 1).findFirst().orElse(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -111,7 +111,7 @@ public class AbstractEnhancedWrappedStreamTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testFindLast() {
|
public void testFindLast() {
|
||||||
final List<Integer> list = asList(1, 2, 3);
|
final List<Integer> list = asList(1, 2, 3);
|
||||||
Assert.assertEquals((Integer)3, wrap(list).findLast(t -> (t & 1) == 1).orElse(null));
|
Assert.assertEquals((Integer) 3, wrap(list).findLast(t -> (t & 1) == 1).orElse(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -123,7 +123,7 @@ public class AbstractEnhancedWrappedStreamTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testAt() {
|
public void testAt() {
|
||||||
final List<Integer> list = asList(1, 2, 3);
|
final List<Integer> list = asList(1, 2, 3);
|
||||||
Assert.assertEquals((Integer)3, wrap(list).at(2).orElse(null));
|
Assert.assertEquals((Integer) 3, wrap(list).at(2).orElse(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -219,13 +219,13 @@ public class AbstractEnhancedWrappedStreamTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testMapToInt() {
|
public void testMapToInt() {
|
||||||
final int[] array = wrap(1, 2, 3).mapToInt(Integer::intValue).toArray();
|
final int[] array = wrap(1, 2, 3).mapToInt(Integer::intValue).toArray();
|
||||||
Assert.assertArrayEquals(new int[] {1, 2, 3}, array);
|
Assert.assertArrayEquals(new int[]{1, 2, 3}, array);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMapToLong() {
|
public void testMapToLong() {
|
||||||
final long[] array = wrap(1L, 2L, 3L).mapToLong(Long::intValue).toArray();
|
final long[] array = wrap(1L, 2L, 3L).mapToLong(Long::intValue).toArray();
|
||||||
Assert.assertArrayEquals(new long[] {1L, 2L, 3L}, array);
|
Assert.assertArrayEquals(new long[]{1L, 2L, 3L}, array);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -239,13 +239,13 @@ public class AbstractEnhancedWrappedStreamTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testFlatMapToInt() {
|
public void testFlatMapToInt() {
|
||||||
final int[] array = wrap(1, 2, 3).flatMapToInt(IntStream::of).toArray();
|
final int[] array = wrap(1, 2, 3).flatMapToInt(IntStream::of).toArray();
|
||||||
Assert.assertArrayEquals(new int[] {1, 2, 3}, array);
|
Assert.assertArrayEquals(new int[]{1, 2, 3}, array);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFlatMapToLong() {
|
public void testFlatMapToLong() {
|
||||||
final long[] array = wrap(1L, 2L, 3L).flatMapToLong(LongStream::of).toArray();
|
final long[] array = wrap(1L, 2L, 3L).flatMapToLong(LongStream::of).toArray();
|
||||||
Assert.assertArrayEquals(new long[] {1L, 2L, 3L}, array);
|
Assert.assertArrayEquals(new long[]{1L, 2L, 3L}, array);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -312,28 +312,28 @@ public class AbstractEnhancedWrappedStreamTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testReduce() {
|
public void testReduce() {
|
||||||
Assert.assertEquals((Integer)6, wrap(1, 2, 3).reduce(Integer::sum).orElse(null));
|
Assert.assertEquals((Integer) 6, wrap(1, 2, 3).reduce(Integer::sum).orElse(null));
|
||||||
Assert.assertEquals((Integer)6, wrap(1, 2, 3).reduce(0, Integer::sum));
|
Assert.assertEquals((Integer) 6, wrap(1, 2, 3).reduce(0, Integer::sum));
|
||||||
Assert.assertEquals((Integer)6, wrap(1, 2, 3).reduce(0, Integer::sum, Integer::sum));
|
Assert.assertEquals((Integer) 6, wrap(1, 2, 3).reduce(0, Integer::sum, Integer::sum));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCollect() {
|
public void testCollect() {
|
||||||
Assert.assertEquals(asList(1, 2, 3), wrap(1, 2, 3).collect(Collectors.toList()));
|
Assert.assertEquals(asList(1, 2, 3), wrap(1, 2, 3).collect(Collectors.toList()));
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
asList(1, 2, 3),
|
asList(1, 2, 3),
|
||||||
wrap(1, 2, 3).collect(ArrayList::new, List::add, List::addAll)
|
wrap(1, 2, 3).collect(ArrayList::new, List::add, List::addAll)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMin() {
|
public void testMin() {
|
||||||
Assert.assertEquals((Integer)1, wrap(1, 2, 3).min(Comparator.comparingInt(Integer::intValue)).orElse(null));
|
Assert.assertEquals((Integer) 1, wrap(1, 2, 3).min(Comparator.comparingInt(Integer::intValue)).orElse(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMax() {
|
public void testMax() {
|
||||||
Assert.assertEquals((Integer)3, wrap(1, 2, 3).max(Comparator.comparingInt(Integer::intValue)).orElse(null));
|
Assert.assertEquals((Integer) 3, wrap(1, 2, 3).max(Comparator.comparingInt(Integer::intValue)).orElse(null));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -377,7 +377,7 @@ public class AbstractEnhancedWrappedStreamTest {
|
|||||||
public void testSpliterator() {
|
public void testSpliterator() {
|
||||||
final Spliterator<Integer> iter1 = Stream.of(1, 2, 3).spliterator();
|
final Spliterator<Integer> iter1 = Stream.of(1, 2, 3).spliterator();
|
||||||
final Spliterator<Integer> iter2 = wrap(1, 2, 3).spliterator();
|
final Spliterator<Integer> iter2 = wrap(1, 2, 3).spliterator();
|
||||||
Assert.assertEquals(iter1.trySplit().estimateSize(), iter2.trySplit().estimateSize());
|
Assert.assertEquals(iter1.trySplit().estimateSize(), iter2.trySplit().estimateSize());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -412,7 +412,7 @@ public class AbstractEnhancedWrappedStreamTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testReverse() {
|
public void testReverse() {
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
asList(3, 2, 1), wrap(1, 2, 3).reverse().toList()
|
asList(3, 2, 1), wrap(1, 2, 3).reverse().toList()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -424,15 +424,15 @@ public class AbstractEnhancedWrappedStreamTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testSplice() {
|
public void testSplice() {
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
asList(1, 4, 5), wrap(1, 2, 3).splice(1, 2, 4, 5).toList()
|
asList(1, 4, 5), wrap(1, 2, 3).splice(1, 2, 4, 5).toList()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testTakeWhile() {
|
public void testTakeWhile() {
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
asList(1, 2),
|
asList(1, 2),
|
||||||
wrap(1, 2, 3, 4).takeWhile(i -> !Objects.equals(i, 3)).toList()
|
wrap(1, 2, 3, 4).takeWhile(i -> !Objects.equals(i, 3)).toList()
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -440,15 +440,15 @@ public class AbstractEnhancedWrappedStreamTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testDropWhile() {
|
public void testDropWhile() {
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
asList(3, 4),
|
asList(3, 4),
|
||||||
wrap(1, 2, 3, 4).dropWhile(i -> !Objects.equals(i, 3)).toList()
|
wrap(1, 2, 3, 4).dropWhile(i -> !Objects.equals(i, 3)).toList()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDistinct() {
|
public void testDistinct() {
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
asList(1, 2, 3), wrap(1, 1, 2, 3).distinct().toList()
|
asList(1, 2, 3), wrap(1, 1, 2, 3).distinct().toList()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -460,41 +460,41 @@ public class AbstractEnhancedWrappedStreamTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testPush() {
|
public void testPush() {
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
asList(1, 2, 3), wrap(1).push(2, 3).toList()
|
asList(1, 2, 3), wrap(1).push(2, 3).toList()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testUnshift() {
|
public void testUnshift() {
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
asList(1, 2, 3), wrap(3).unshift(1, 2).toList()
|
asList(1, 2, 3), wrap(3).unshift(1, 2).toList()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAppend() {
|
public void testAppend() {
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
asList(1, 2, 3), wrap(1).append(asList(2, 3)).toList()
|
asList(1, 2, 3), wrap(1).append(asList(2, 3)).toList()
|
||||||
);
|
);
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
asList(1, 2, 3), wrap(1, 2, 3).append(null).toList()
|
asList(1, 2, 3), wrap(1, 2, 3).append(null).toList()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPrepend() {
|
public void testPrepend() {
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
asList(1, 2, 3), wrap(3).prepend(asList(1, 2)).toList()
|
asList(1, 2, 3), wrap(3).prepend(asList(1, 2)).toList()
|
||||||
);
|
);
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
asList(1, 2, 3), wrap(1, 2, 3).prepend(null).toList()
|
asList(1, 2, 3), wrap(1, 2, 3).prepend(null).toList()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNonNull() {
|
public void testNonNull() {
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
asList(1, 3), wrap(1, null, 3).nonNull().toList()
|
asList(1, 3), wrap(1, null, 3).nonNull().toList()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -502,11 +502,11 @@ public class AbstractEnhancedWrappedStreamTest {
|
|||||||
public void testFilterIdx() {
|
public void testFilterIdx() {
|
||||||
final List<Integer> indexes = new ArrayList<>();
|
final List<Integer> indexes = new ArrayList<>();
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
asList(1, 3),
|
asList(1, 3),
|
||||||
wrap(1, 2, 3).filterIdx((t, i) -> {
|
wrap(1, 2, 3).filterIdx((t, i) -> {
|
||||||
indexes.add(i);
|
indexes.add(i);
|
||||||
return (t & 1) == 1;
|
return (t & 1) == 1;
|
||||||
}).toList()
|
}).toList()
|
||||||
);
|
);
|
||||||
Assert.assertEquals(asList(0, 1, 2), indexes);
|
Assert.assertEquals(asList(0, 1, 2), indexes);
|
||||||
}
|
}
|
||||||
@ -514,14 +514,14 @@ public class AbstractEnhancedWrappedStreamTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testFilter() {
|
public void testFilter() {
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
asList(1, 3), wrap(1, 2, 3).filter(i -> (i & 1) == 1).toList()
|
asList(1, 3), wrap(1, 2, 3).filter(i -> (i & 1) == 1).toList()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFlatMap() {
|
public void testFlatMap() {
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
asList(1, 2, 3), wrap(1, 2, 3).flatMap(Stream::of).toList()
|
asList(1, 2, 3), wrap(1, 2, 3).flatMap(Stream::of).toList()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -529,10 +529,10 @@ public class AbstractEnhancedWrappedStreamTest {
|
|||||||
public void testFlatMapIdx() {
|
public void testFlatMapIdx() {
|
||||||
final List<Integer> indexes = new ArrayList<>();
|
final List<Integer> indexes = new ArrayList<>();
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
asList(1, 2, 3), wrap(1, 2, 3).flatMapIdx((t, i) -> {
|
asList(1, 2, 3), wrap(1, 2, 3).flatMapIdx((t, i) -> {
|
||||||
indexes.add(i);
|
indexes.add(i);
|
||||||
return Stream.of(t);
|
return Stream.of(t);
|
||||||
}).toList()
|
}).toList()
|
||||||
);
|
);
|
||||||
Assert.assertEquals(asList(0, 1, 2), indexes);
|
Assert.assertEquals(asList(0, 1, 2), indexes);
|
||||||
}
|
}
|
||||||
@ -540,14 +540,14 @@ public class AbstractEnhancedWrappedStreamTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testFlat() {
|
public void testFlat() {
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
asList(1, 2, 3), wrap(1, 2, 3).flat(Collections::singletonList).toList()
|
asList(1, 2, 3), wrap(1, 2, 3).flat(Collections::singletonList).toList()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFlatNonNull() {
|
public void testFlatNonNull() {
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
asList(2, 3), wrap(null, 2, 3).flatNonNull(Collections::singletonList).toList()
|
asList(2, 3), wrap(null, 2, 3).flatNonNull(Collections::singletonList).toList()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -560,14 +560,14 @@ public class AbstractEnhancedWrappedStreamTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testMap() {
|
public void testMap() {
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
asList("1", "2", "3"), wrap(1, 2, 3).map(String::valueOf).toList()
|
asList("1", "2", "3"), wrap(1, 2, 3).map(String::valueOf).toList()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMapNonNull() {
|
public void testMapNonNull() {
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
asList("3"), wrap(null, 2, 3, 4).mapNonNull(t -> ((t & 1) == 0) ? null : String.valueOf(t)).toList()
|
asList("3"), wrap(null, 2, 3, 4).mapNonNull(t -> ((t & 1) == 0) ? null : String.valueOf(t)).toList()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -575,10 +575,10 @@ public class AbstractEnhancedWrappedStreamTest {
|
|||||||
public void testMapIdx() {
|
public void testMapIdx() {
|
||||||
final List<Integer> indexes = new ArrayList<>();
|
final List<Integer> indexes = new ArrayList<>();
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
asList("1", "2", "3"), wrap(1, 2, 3).mapIdx((t, i) -> {
|
asList("1", "2", "3"), wrap(1, 2, 3).mapIdx((t, i) -> {
|
||||||
indexes.add(i);
|
indexes.add(i);
|
||||||
return String.valueOf(t);
|
return String.valueOf(t);
|
||||||
}).toList()
|
}).toList()
|
||||||
);
|
);
|
||||||
Assert.assertEquals(asList(0, 1, 2), indexes);
|
Assert.assertEquals(asList(0, 1, 2), indexes);
|
||||||
}
|
}
|
||||||
@ -586,10 +586,10 @@ public class AbstractEnhancedWrappedStreamTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testMapMulti() {
|
public void testMapMulti() {
|
||||||
Assert.assertEquals(
|
Assert.assertEquals(
|
||||||
asList(1, 2, 3),
|
asList(1, 2, 3),
|
||||||
wrap(1, 2, 3).mapMulti((t, builder) -> {
|
wrap(1, 2, 3).mapMulti((t, builder) -> {
|
||||||
builder.accept(t);
|
builder.accept(t);
|
||||||
}).toList()
|
}).toList()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -613,19 +613,19 @@ public class AbstractEnhancedWrappedStreamTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testToEntries() {
|
public void testToEntries() {
|
||||||
final Map<Integer, Integer> expect = new HashMap<Integer, Integer>(){{
|
final Map<Integer, Integer> expect = new HashMap<Integer, Integer>() {{
|
||||||
put(1, 1);
|
put(1, 1);
|
||||||
put(2, 2);
|
put(2, 2);
|
||||||
put(3, 3);
|
put(3, 3);
|
||||||
}};
|
}};
|
||||||
Map<Integer, Integer> map = EasyStream.of(1, 2, 3)
|
Map<Integer, Integer> map = EasyStream.of(1, 2, 3)
|
||||||
.toEntries(Function.identity(), Function.identity())
|
.toEntries(Function.identity(), Function.identity())
|
||||||
.toMap();
|
.toMap();
|
||||||
Assert.assertEquals(expect, map);
|
Assert.assertEquals(expect, map);
|
||||||
|
|
||||||
map = EasyStream.of(1, 2, 3)
|
map = EasyStream.of(1, 2, 3)
|
||||||
.toEntries(Function.identity())
|
.toEntries(Function.identity())
|
||||||
.toMap();
|
.toMap();
|
||||||
Assert.assertEquals(expect, map);
|
Assert.assertEquals(expect, map);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -636,7 +636,7 @@ public class AbstractEnhancedWrappedStreamTest {
|
|||||||
List<String> zip = wrap(orders).zip(list, (e1, e2) -> e1 + "." + e2).toList();
|
List<String> zip = wrap(orders).zip(list, (e1, e2) -> e1 + "." + e2).toList();
|
||||||
Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), zip);
|
Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), zip);
|
||||||
|
|
||||||
zip = wrap((Stream<? extends Object>)EasyStream.iterate(1, i -> i + 1)).zip(list, (e1, e2) -> e1 + "." + e2).toList();
|
zip = wrap((Stream<? extends Object>) EasyStream.iterate(1, i -> i + 1)).limit(10).zip(list, (e1, e2) -> e1 + "." + e2).toList();
|
||||||
Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), zip);
|
Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), zip);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user