文章目录
引入 一、Lambda表达式
二、函数式(Functional)接口 2.1 函数式(Functional)接口介绍 2.2 Java内置函数式接口
三、方法引用与构造器引用
四、强大的Stream API 4.1 创建 Stream 的4种方式 4.2 Stream 的中间操作 4.2.1 筛选与切片 4.2.2 映射 4.2.3 排序
4.3 Stream 的终止操作 4.3.1 匹配与查找 4.3.2 归约 4.3.3 收集
五、Optional类
引入
一、Lambda表达式
1.1 快速入门
package com. gyh ;
import java. util. Comparator ;
public class LambdaTest {
public static void main ( String [ ] args) {
Runnable runnable = new Runnable ( ) {
@Override
public void run ( ) {
System . out. println ( "hsp" ) ;
}
} ;
runnable. run ( ) ;
Runnable runnable2 = ( ) -> System . out. println ( "hsp" ) ;
Comparator < Integer > comparator = new Comparator < Integer > ( ) {
@Override
public int compare ( Integer o1, Integer o2) {
return Integer . compare ( o1, o2) ;
}
} ;
int compare = comparator. compare ( 1 , 2 ) ;
System . out. println ( compare) ;
Comparator < Integer > comparator2 = ( o1, o2) -> Integer . compare ( o1, o2) ;
Comparator < Integer > comparator3 = Integer :: compareTo ;
}
}
1.2 Lambda语法
package com. gyh ;
import java. util. Comparator ;
import java. util. function. Consumer ;
public class LambadaTest1 {
public static void main ( String [ ] args) {
Runnable runnable = new Runnable ( ) {
@Override
public void run ( ) {
System . out. println ( "hsp" ) ;
}
} ;
Runnable runnable2 = ( ) -> {
System . out. println ( "hsp" ) ;
} ;
Consumer < String > consumer = new Consumer < String > ( ) {
@Override
public void accept ( String s) {
System . out. println ( s) ;
}
} ;
Consumer < String > consumer2 = ( String s) -> {
System . out. println ( s) ;
} ;
Consumer < String > consumer3 = ( s) -> {
System . out. println ( s) ;
} ;
Consumer < String > consumer4 = s -> {
System . out. println ( s) ;
} ;
Comparator < Integer > comparator = new Comparator < Integer > ( ) {
@Override
public int compare ( Integer o1, Integer o2) {
return Integer . compare ( o1, o2) ;
}
} ;
Comparator < Integer > comparator1 = ( x, y) -> {
System . out. println ( "实现函数式接口方法!" ) ;
return Integer . compare ( x, y) ;
} ;
Comparator < Integer > comparator2 = ( x, y) -> Integer . compare ( x, y) ;
}
}
二、函数式(Functional)接口
2.1 函数式(Functional)接口介绍
2.2 Java内置函数式接口
package com. gyh ;
import java. util. ArrayList ;
import java. util. Arrays ;
import java. util. List ;
import java. util. function. Consumer ;
import java. util. function. Predicate ;
public class LambdaTest2 {
public static void main ( String [ ] args) {
test ( ) ;
}
public static void test ( ) {
List < String > strings = Arrays . asList ( "北京" , "东京" , "西京" , "南京" , "天津" ) ;
List < String > str = filterList ( strings, s -> s. contains ( "京" ) ) ;
System . out. println ( str) ;
}
public static void happyTime ( double money, Consumer < Double > con) {
con. accept ( money) ;
}
private static List < String > filterList ( List < String > str, Predicate < String > p) {
ArrayList < String > newsStr = new ArrayList < > ( ) ;
for ( String s : str) {
if ( p. test ( s) ) {
newsStr. add ( s) ;
}
}
return newsStr;
}
}
三、方法引用与构造器引用
3.1 方法引用
package com. gyh ;
import java. util. Comparator ;
import java. util. function. Consumer ;
public class LambdaTest3 {
public static void main ( String [ ] args) {
}
public static void test1 ( ) {
Consumer < String > con1 = str -> System . out. println ( str) ;
con1. accept ( "北京" ) ;
Consumer < String > con2 = System . out:: println ;
}
public static void test2 ( ) {
Comparator < Integer > com1 = ( i1, i2) -> Integer . compare ( i1, i2) ;
Comparator < Integer > com2 = Integer :: compareTo ;
}
public static void test3 ( ) {
Comparator < String > com1 = ( i1, i2) -> i1. compareTo ( i2) ;
Comparator < String > com2 = String :: compareTo ;
}
}
3.2 构造器引用
package com. gyh ;
import java. util. function. Function ;
import java. util. function. Supplier ;
public class LambdaTest4 {
public void test1 ( ) {
Supplier < String > stringSupplier1 = new Supplier < String > ( ) {
@Override
public String get ( ) {
return new String ( ) ;
}
} ;
Supplier < String > stringSupplier2 = ( ) -> new String ( ) ;
Supplier < String > stringSupplier3 = String :: new ;
}
public void test2 ( ) {
Function < String , String > f1 = ( name) -> new String ( name) ;
Function < String , String > f2 = String :: new ;
}
public void test3 ( ) {
Function < Integer , String [ ] > f1 = ( length) -> new String [ length] ;
Function < Integer , String [ ] > f2 = String [ ] :: new ;
}
}
四、强大的Stream API
4.1 创建 Stream 的4种方式
package com. gyh ;
import java. util. ArrayList ;
import java. util. Arrays ;
import java. util. List ;
import java. util. Objects ;
import java. util. stream. IntStream ;
import java. util. stream. Stream ;
public class StreamAPITest {
public static void main ( String [ ] args) {
List < Employee > employees = EmployeeData . getEmployees ( ) ;
Stream < Employee > stream = employees. stream ( ) ;
Stream < Employee > employeeStream = employees. parallelStream ( ) ;
int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ;
IntStream stream1 = Arrays . stream ( arr) ;
Employee [ ] objects = ( Employee [ ] ) employees. toArray ( ) ;
Stream < Employee > stream2 = Arrays . stream ( objects) ;
Stream < Employee > objects1 = Stream . of ( objects) ;
Stream . iterate ( 0 , t -> t + 2 ) . limit ( 10 ) . forEach ( System . out:: println ) ;
Stream . generate ( Math :: random ) . limit ( 10 ) . forEach ( System . out:: println ) ;
}
}
class EmployeeData {
public static List < Employee > getEmployees ( ) {
ArrayList < Employee > employees = new ArrayList < > ( ) ;
employees. add ( new Employee ( "马化腾" , 34 , 1001 , 6000.38 ) ) ;
employees. add ( new Employee ( "马云" , 12 , 1002 , 9876.12 ) ) ;
employees. add ( new Employee ( "刘强东" , 33 , 1003 , 3000.82 ) ) ;
employees. add ( new Employee ( "雷军" , 26 , 1004 , 7657.37 ) ) ;
employees. add ( new Employee ( "李彦宏" , 65 , 1005 , 5555.32 ) ) ;
employees. add ( new Employee ( "比尔盖茨" , 42 , 1006 , 9500.43 ) ) ;
employees. add ( new Employee ( "任正非" , 26 , 1007 , 4333.32 ) ) ;
employees. add ( new Employee ( "扎克伯格" , 35 , 1008 , 2500.32 ) ) ;
return employees;
}
}
class Employee {
private String name;
private int age;
private int id;
private double salary;
public Employee ( String name, int age, int id, double salary) {
this . name = name;
this . age = age;
this . id = id;
this . salary = salary;
}
@Override
public boolean equals ( Object o) {
if ( this == o) return true ;
if ( o == null || getClass ( ) != o. getClass ( ) ) return false ;
Employee employee = ( Employee ) o;
return age == employee. age &&
id == employee. id &&
Double . compare ( employee. salary, salary) == 0 &&
Objects . equals ( name, employee. name) ;
}
@Override
public int hashCode ( ) {
return Objects . hash ( name, age, id, salary) ;
}
@Override
public String toString ( ) {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", id=" + id +
", salary=" + salary +
'}' ;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
public int getId ( ) {
return id;
}
public void setId ( int id) {
this . id = id;
}
public double getSalary ( ) {
return salary;
}
public void setSalary ( double salary) {
this . salary = salary;
}
}
4.2 Stream 的中间操作
4.2.1 筛选与切片
package com. gyh ;
import java. util. List ;
public class StreamAPITest1 {
public static void main ( String [ ] args) {
List < Employee > employees = EmployeeData . getEmployees ( ) ;
employees. stream ( )
. filter ( e -> e. getSalary ( ) > 7000 )
. forEach ( System . out:: println ) ;
employees. stream ( )
. limit ( 3 )
. forEach ( System . out:: println ) ;
employees. stream ( )
. skip ( 3 )
. forEach ( System . out:: println ) ;
employees. stream ( )
. distinct ( )
. forEach ( System . out:: println ) ;
}
}
4.2.2 映射
package com. gyh ;
import java. util. ArrayList ;
import java. util. Arrays ;
import java. util. List ;
import java. util. stream. Stream ;
public class StreamAPITest2 {
public static void main ( String [ ] args) {
List < String > strings = Arrays . asList ( "aa" , "bb" , "cc" , "dd" ) ;
strings. stream ( )
. map ( String :: toUpperCase )
. forEach ( System . out:: println ) ;
strings. stream ( )
. map ( StreamAPITest2 :: fromStringToStream )
. forEach ( s -> {
s. forEach ( System . out:: println ) ;
} ) ;
strings. stream ( )
. flatMap ( StreamAPITest2 :: fromStringToStream )
. forEach ( System . out:: println ) ;
}
public static Stream < Character > fromStringToStream ( String str) {
ArrayList < Character > characters = new ArrayList < > ( ) ;
for ( Character s : str. toCharArray ( ) ) {
characters. add ( s) ;
}
return characters. stream ( ) ;
}
}
4.2.3 排序
package com. gyh ;
import java. util. Arrays ;
import java. util. Comparator ;
import java. util. List ;
public class StreamAPITest3 {
public static void main ( String [ ] args) {
List < Integer > integers = Arrays . asList ( 12 , 43 , 65 , 34 , 87 , 0 , - 98 , 7 ) ;
integers. stream ( ) . sorted ( ) . forEach ( System . out:: println ) ;
EmployeeData . getEmployees ( )
. stream ( )
. sorted ( Comparator . comparingDouble ( Employee :: getSalary ) )
. forEach ( System . out:: println ) ;
}
}
4.3 Stream 的终止操作
4.3.1 匹配与查找
package com. gyh ;
import java. util. List ;
import java. util. Optional ;
public class StreamAPITest4 {
public static void main ( String [ ] args) {
List < Employee > employees = EmployeeData . getEmployees ( ) ;
boolean b = employees. stream ( )
. allMatch ( e -> e. getAge ( ) > 18 ) ;
boolean b1 = employees. stream ( )
. anyMatch ( e -> e. getSalary ( ) > 10000 ) ;
boolean b2 = employees. stream ( )
. noneMatch ( e -> e. getName ( ) . contains ( "雷" ) ) ;
Optional < Employee > first = employees. stream ( )
. findFirst ( ) ;
Optional < Employee > any = employees. stream ( )
. findAny ( ) ;
long count = employees. stream ( ) . filter ( e -> e. getAge ( ) > 12 ) . count ( ) ;
Optional < Employee > max = employees. stream ( )
. max ( ( e1, e2) -> Double . compare ( e1. getSalary ( ) , e2. getSalary ( ) ) ) ;
Optional < Employee > min = employees. stream ( )
. min ( ( e1, e2) -> Double . compare ( e1. getSalary ( ) , e2. getSalary ( ) ) ) ;
employees. stream ( ) . forEach ( System . out:: println ) ;
employees. forEach ( System . out:: println ) ;
}
}
4.3.2 归约
package com. gyh ;
import java. util. Arrays ;
import java. util. List ;
import java. util. Optional ;
public class StringAPITest5 {
public static void main ( String [ ] args) {
List < Integer > list = Arrays . asList ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ) ;
Integer reduce = list. stream ( ) . reduce ( 0 , Integer :: sum ) ;
List < Employee > employees = EmployeeData . getEmployees ( ) ;
Optional < Double > reduce1 = employees. stream ( )
. map ( Employee :: getSalary )
. reduce ( Double :: sum ) ;
}
}
4.3.3 收集
package com. gyh ;
import java. util. List ;
import java. util. stream. Collectors ;
public class StreamAPITest6 {
public static void main ( String [ ] args) {
List < Employee > employees = EmployeeData . getEmployees ( ) ;
List < Employee > collect = employees. stream ( )
. filter ( e -> e. getSalary ( ) > 6000 )
. collect ( Collectors . toList ( ) ) ;
collect. forEach ( System . out:: println ) ;
}
}
五、Optional类
package com. gyh ;
import java. util. Optional ;
public class StreamAPITest7 {
public static void main ( String [ ] args) {
Employee employee = new Employee ( "aa" , 12 , 1001 , 9000 ) ;
Optional < Employee > employee1 = Optional . of ( employee) ;
Optional < Object > empty = Optional . empty ( ) ;
Optional < Employee > employee2 = Optional . ofNullable ( employee) ;
}
}