黑马程序员技术交流社区

标题: ... [打印本页]

作者: KevinJavaEE    时间: 2019-4-13 15:06
标题: ...
Stream<T>流(接口)

定义:非函数式接口,是一个集合元素的函数模型,既不是集合也不是数据结构,并不能存储任何元素。

流的获取:

        -- 根据Collection集合接口中的stream()方法

        --根据Map,需先转为Collection单列集合,再使用Collection中的stream()

        --根据数组,Stream流接口中的静态方法of

常用方法:延迟方法和终结方法

延迟方法

               

                    -- Stream filter(Predicate<> predicate)
   
                    --Stream<R> map(Function<? super T,?extends R> mapper)
   
                    --Stream limit(long maxSize)
   
                    --Stream skip(long num)
   
                    --static Stream concat(Stream s1,Stream s2)
   
                    --static Stream of(T...values) :若传递的数组,数组的类型应为对象。可变参数实际为一个形参,可传递多个实参,但是只能传递一个形参



终结方法

            --<R,A>R **collect**(Collector<? super T,A,R> collector),将流包装为集合(List / Set / Map)的方法
   
                    **Collectors工具类(用于Collector接口):**
   
                    static<T> Collector<T,?,List<T>>  toList()
   
                    static<T> Collector<T,?,Set<T>>  toSet()
   
                    static<T,K,U> Collector<T,?,Map<K,U>>  toMap(Function<? super T,? extends K> key,Function<? super T,? extends U> value )

            --void forEach(Consumer<? super T) action)
   
            --long count()






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2