java8中的函数式数据处理

1、在流中只能遍历一次,否则会提示流已经操作或者关闭的错误

List title = Arrays.asList("Java8", "In", "Action");

        Stream stream = title.stream();

        stream.forEach(System.out::println);

     stream.forEach(System.out::println);

2、操作类型分为中间操作和终端操作

中间操作有

操作

类型

返回类型

操作参数

函数描述符

filter

中间

Stream

Predicate

T -> boolean

map

中间

Stream

Function

T -> R

sorted

中间

Stream

Comparator

(T,T) -> int

limit

中间

Stream

 

 

distinct

中间

Stream

 

 

skip

中间

Stream

 

 

终端操作有

操作

类型

返回类型

操作参数

函数描述符

forEach

终端

void

Consumer

T -> void

count

终端

long

 

 

collect

终端

R

Collector

 

allMatch

终端

boolean

Predicate

T -> boolean

anyMatch

终端

boolean

Predicate

T -> boolean

noneMatch

终端

boolean

Predicate

T -> boolean

findAny

终端

Optional

 

 

findFirst

终端

Optional

 

 

reduce

终端

Optinal

BinaryOperator

(T,T)->T

3、原始类型流物化

IntStream, DoubleStream, LongStream

映射到数值流,使用mapToInt,mapToDouble, mapToLong

转换为对象流使用boxed

生成范围使用range和rangeClosed,第一个参数表示起始值,第二个参数表示结束值,range不包含结束值,rangeClosed包含

4、创建流

(1)通过值创建流,使用静态方法Stream.of,使用Stream.empty()创建空流

(2)数组创建流,使用静态方法Arrays.stream

(3)由文件创建流,Files.lines

(4)函数创建流,使用静态方法Stream.iterate和Stream.generate

5、收集器

toList

toSet

joining

joining(CharSequence delimiter)

joining(CharSequence delimiter, CharSequence prefix, CharSequence suffix)

counting

minBy(Comparator comparator)

maxBy(Comparator comparator)

summingInt(ToIntFunction mapper)

summingLong(ToLongFunction mapper)

summingDouble(ToDoubleFunction mapper)

averagingInt(ToIntFunction mapper)

averagingLong(ToLongFunction mapper)

averagingDouble(ToDoubleFunction mapper)

reducing(T identity, BinaryOperatorop)

reducing(BinaryOperator op)

reducing(U identity, Function mapper, BinaryOperator op)

groupingBy(Function classifier)

groupingBy(Function classifier, Collector downstream)

groupingBy(Function classifier, Supplier mapFactory, Collector downstream)

groupingByConcurrent(Function classifier)

groupingByConcurrent(Function classifier, Collector downstream)

groupingByConcurrent(Functionclassifier, SuppliermapFactory, Collector downstream)

partitioningBy(Predicatepredicate)

partitioningBy(Predicatepredicate, Collector downstream)

collectingAndThen(Collector downstream, Function finisher)

toMap(Function keyMapper, Function valueMapper)

toMap(Function keyMapper, Function valueMapper, BinaryOperation mergeFunction)

toMap(FunctionkeyMapper, Function valueMapper, BinaryOperator mergeFunction, SuppliermapSupplier)

toConcurrentMap(Function keyMapper, Function valueMapper)

toConcurrentMap(Function keyMapper, Function valueMapper, BinaryOperationmergeFunction)

toConcurrentMap(FunctionkeyMapper, Function valueMapper, BinaryOperator mergeFunction, SuppliermapSupplier)

summarizingInt(ToIntFunctionmapper)

summarizingLong(ToLongFunctionmapper)

summarizingDouble(ToDoubleFunctionmapper)

 

 

你可能感兴趣的:(java)