一.函数编程(lambda表达式)
4大内置函数接口Consumer,Function,Supplier, Predicate
1.读取流
public static void test01 ( ) {
long t0 = System . nanoTime ( ) ;
int a[ ] = IntStream . range ( 0 , 1_000_000 ) . filter ( p -> p % 2 == 0 ) . toArray ( ) ;
long t1 = System . nanoTime ( ) ;
int b[ ] = IntStream . range ( 0 , 1_000_000 ) . parallel ( ) . filter ( p -> p % 2 == 0 ) . toArray ( ) ;
long t2 = System . nanoTime ( ) ;
System . out. printf ( "serial: %.2fs, parallel %.2fs%n" , ( t1 - t0) * 1e-9 , ( t2 - t1) * 1e-9 ) ;
}
2.组合多个Predicate过滤条件
List < String > list1 = Arrays . asList ( new String [ ] { "5" , "10" , "10" , "11" , "30" } ) ;
Predicate < String > predicate1 = ( f) -> Integer . valueOf ( f) > 5 ;
Predicate < String > predicate = ( f) -> Integer . valueOf ( f) < 30 ;
List < String > collect = list1. stream ( ) . filter ( predicate. and ( predicate1) ) . collect ( Collectors . toList ( ) ) ;
collect. forEach ( n -> System . out. println ( n) ) ;
3.map可以改变元素的值
List < Integer > list = Arrays . asList ( 100 , 200 , 300 , 400 , 500 ) ;
List < Integer > collect = list. stream ( ) . map ( f -> ++ f) . collect ( Collectors . toList ( ) ) ;
collect. forEach ( System . out:: println ) ;
4.reduce用来计算数据
List < Integer > list = Arrays . asList ( 100 , 200 , 300 , 400 , 500 ) ;
Integer integer = list. stream ( ) . reduce ( ( sum, cost) -> sum + cost) . get ( ) ;
System . out. println ( integer) ;
5.collect用生成不同的数据类型
List < String > list= Arrays . asList ( "我" , "叫张三" , "你好" ) ;
String collect = list. stream ( ) . collect ( Collectors . joining ( "-" ) ) ;
System . out. println ( collect) ;
6.flatMap 将多个Stream连接成一个Stream
List < Integer > result= Stream . of ( Arrays . asList ( 1 , 3 ) , Arrays . asList ( 5 , 6 ) ) . flatMap ( a-> a. stream ( ) ) . collect ( Collectors . toList ( ) ) ;
result. forEach ( System . out:: println ) ;
7.distinct去重 ,如果去重的是引用类型需要重写equals和hashcode
List < Integer > list= Arrays . asList ( new Integer [ ] { 1 , 2 , 3 , 3 , 4 , 4 } ) ;
List < Integer > collect = list. stream ( ) . distinct ( ) . collect ( Collectors . toList ( ) ) ;
collect. forEach ( System . out:: println ) ;
8.count 统计个数
List < Integer > list= Arrays . asList ( new Integer [ ] { 1 , 2 , 3 , 3 , 4 , 4 } ) ;
long count = list. stream ( ) . count ( ) ;
System . out. println ( count) ;
9.min,max,summaryStatistics
List < Integer > list= Arrays . asList ( new Integer [ ] { 1 , 2 , 3 , 3 , 4 , 4 , 5 } ) ;
Integer max = list. stream ( ) . max ( Comparator . comparingInt ( t -> t) ) . get ( ) ;
Integer min = list. stream ( ) . min ( Comparator . comparingInt ( t -> t) ) . get ( ) ;
System . out. println ( max+ "-" + min) ;
IntSummaryStatistics statistics = list. stream ( ) . mapToInt ( t -> t) . summaryStatistics ( ) ;
System . out. println ( "最小:" + statistics. getMin ( ) ) ;
System . out. println ( "最大:" + statistics. getMax ( ) ) ;
System . out. println ( "总和:" + statistics. getSum ( ) ) ;
System . out. println ( "元素个数:" + statistics. getCount ( ) ) ;
System . out. println ( "计算平均数:" + statistics. getAverage ( ) ) ;
10.peek方法,可以包含一个方法体
List < Integer > list= Arrays . asList ( new Integer [ ] { 1 , 2 , 3 , 3 , 4 , 4 , 5 } ) ;
list. stream ( ) . peek ( t-> {
System . out. println ( ++ t) ;
} ) . collect ( Collectors . toList ( ) ) ;
二.默认方法
什么是默认方法:在接口中定义有方法体的方法,使用default修饰,可以让实现者选择性实现,解决了实现接口者需要重写里面所有的方法的问题
interface A {
default void aa ( ) {
System . out. println ( "zhizhi" ) ;
}
}
class B implements A {
}