Java 8 stream 中重要的Collector接口

接口Collector < T, A, R >:

T is the generic type of the items in the stream to be collected.
A is the type of the accumulator, the object on which the partial result will be accumulated during the collection process.
R is the type of the object (typically, but not always, the collection) resulting from the collect operation.

T 是要收集的流中的项的泛型类型。
A 是累加器的类型, 在收集过程中将在其上累积部分结果的对象。
R 是收集操作产生的对象 (通常但不总是集合) 的类型。

简单使用实例:


import java.util.*;
import java.util.function.*;
import java.util.stream.Collector;
import static java.util.stream.Collector.Characteristics.*;

public class ToListCollector<T> implements Collector<T, List<T>, List<T>> {

    @Override
    public Supplier<List<T>> supplier() {
        return () -> new ArrayList<T>();
    }

    @Override
    public BiConsumer<List<T>, T> accumulator() {
        return (list, item) -> list.add(item);
    }

    @Override
    public Function<List<T>, List<T>> finisher() {
        return i -> i;
    }

    @Override
    public BinaryOperator<List<T>> combiner() {
        return (list1, list2) -> {
            list1.addAll(list2);
            return list1;
        };
    }

    @Override
    public Set<Characteristics> characteristics() {
        return Collections.unmodifiableSet(EnumSet.of(IDENTITY_FINISH, CONCURRENT));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java 8引入了java.util.stream.Collectors类,它实现了java.util.stream.Collector接口,并提供了许多方法来对流的元素执行map和reduce操作,或者进行统计操作。 以下是java.util.stream.Collectors类的一些常用方法: 1. toList():将流的所有元素导出到一个列表。 ```java import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class ToListTest { public static void main(String[] args) { List<String> list = Stream.of("AA", "BB", "CC").collect(Collectors.toList()); list.forEach(s -> System.out.println(s)); } } ``` 2. toSet():将流的所有元素导出到一个集合,去除重复元素。 ```java import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; public class ToSetTest { public static void main(String[] args) { Set<String> set = Stream.of("AA", "BB", "CC").collect(Collectors.toSet()); set.forEach(s -> System.out.println(s)); } } ``` 3. toMap():将流的元素导出到一个Map,可以指定key和value的映射关系。 ```java import java.util.Map; import java.util.stream.Collectors; import java.util.stream.Stream; public class ToMapTest { public static void main(String[] args) { Map<String, Integer> map = Stream.of("AA", "BB", "CC").collect(Collectors.toMap(s -> s, s -> s.length())); map.forEach((k, v) -> System.out.println(k + ": " + v)); } } ``` 4. joining():将流的元素连接成一个字符串。 ```java import java.util.stream.Collectors; import java.util.stream.Stream; public class JoiningTest { public static void main(String[] args) { String result = Stream.of("AA", "BB", "CC").collect(Collectors.joining(", ")); System.out.println(result); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

生活中的思索

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值