匿名類:
package l8;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Collectors;
public class CollectorToList2 {
public static void main(String[] args) {
/**
*
<T> – the type of input elements to the reduction operation
<A> – the mutable accumulation type of the reduction operation (often hidden as an implementation detail)
<R> – the result type of the reduction operation 最終返回結(jié)果的類型
*/
Collector<Integer, List<Integer>, List<String>> toList = new Collector<Integer, List<Integer>, List<String>>() {
// 初始一個容器,用于做為累加的容器
@Override
public Supplier<List<Integer>> supplier() {
return () -> new ArrayList<>();
}
/**
* 元素累加
* @return
*/
@Override
public BiConsumer<List<Integer>, Integer> accumulator() {
return List::add;
}
/**
* 將多個容器進(jìn)行合并(應(yīng)該是在并行Stream時使用的)
* @return
*/
@Override
public BinaryOperator<List<Integer>> combiner() {
return (a, b) -> {
System.out.println("combiner call");
a.addAll(b);
return a;
};
}
/**
* 最終類型轉(zhuǎn)換
* @return
*/
@Override
public Function<List<Integer>, List<String>> finisher() {
return list -> list.stream()
.map(e -> e + "").collect(Collectors.toList());
}
@Override
public Set<Characteristics> characteristics() {
return Collections.singleton(Characteristics.UNORDERED);
}
};
List<String> collect = Arrays.asList(1, 2, 3).stream().collect(toList);
System.out.println(collect);
collect = Arrays.asList(1, 2, 3).parallelStream().collect(toList);
System.out.println(collect);
}
}
javascript:void(0)
Combiner:
應(yīng)用:
優(yōu)化初始容器的容量:
/**
* <T> – the type of input elements to the reduction operation
* <A> – the mutable accumulation type of the reduction operation (often hidden as an implementation detail)
* <R> – the result type of the reduction operation 最終返回結(jié)果的類型
*/
class ToListWithInitialCapacity implements Collector<Integer, List<Integer>, List<String>> {
private int initialCapacity;
public ToListWithInitialCapacity(int initialCapacity) {
this.initialCapacity = initialCapacity;
}
// 初始一個容器,用于做為累加的容器
@Override
public Supplier<List<Integer>> supplier() {
return () -> new ArrayList<>(initialCapacity);
}
/**
* 元素累加
*
* @return
*/
@Override
public BiConsumer<List<Integer>, Integer> accumulator() {
return List::add;
}
/**
* 將多個容器進(jìn)行合并(應(yīng)該是在并行Stream時使用的)
*
* @return
*/
@Override
public BinaryOperator<List<Integer>> combiner() {
return (a, b) -> {
System.out.println("combiner call");
a.addAll(b);
return a;
};
}
/**
* 最終類型轉(zhuǎn)換
*
* @return
*/
@Override
public Function<List<Integer>, List<String>> finisher() {
return list -> list.stream()
.map(e -> e + "").collect(Collectors.toList());
}
@Override
public Set<Characteristics> characteristics() {
return Collections.singleton(Characteristics.UNORDERED);
}
}
Jdk toList默認(rèn)實(shí)現(xiàn):文章來源:http://www.zghlxwxcb.cn/news/detail-790064.html
/**
* Returns a {@code Collector} that accumulates the input elements into a
* new {@code List}. There are no guarantees on the type, mutability,
* serializability, or thread-safety of the {@code List} returned; if more
* control over the returned {@code List} is required, use {@link #toCollection(Supplier)}.
*
* @param <T> the type of the input elements
* @return a {@code Collector} which collects all the input elements into a
* {@code List}, in encounter order
*/
public static <T>
Collector<T, ?, List<T>> toList() {
return new CollectorImpl<>(ArrayList::new, List::add,
(left, right) -> { left.addAll(right); return left; },
CH_ID);
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-790064.html
到了這里,關(guān)于Java lambda表達(dá)式如何自定義一個toList Collector的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!