Stream流的收集操作

对数据使用Stream流的方式操作完毕后,如果还想把流中的数据收集到集合中,该怎么办,如下

Stream流的收集方式:R collect(Collector collector)方法,即collector方法,其中collector方法的参数是Collector接口类型的 Collector接口有一个工具类Collectors,该工具类的作用是实现各种有用的还原操作的Collector

 

工具类Collectors提供了具体的收集方式,如下 1、public static Collector toList(),作用是把元素收集到List集合中 2、public static Collector toSet(),作用是把元素收集到Set集合中 3、public static Collector toMap(Function keyMapper,Function valueMapper),作用是把元素收集到Map集合中

 

Stream流收集操作的练习