文章目录
1.函数对象表现形式 1.Lambda表达式(功能全面) 1.基本语法 2.只有一行逻辑,该逻辑结果是返回值 3.复杂逻辑 4.省略参数类型(可以通过上下文推导出类型时,比如实现了函数式接口) 5.只有一个参数时,参数的圆括号可以省略,但是也不能加参数类型
2.方法引用(写法简洁) 1.类型调用静态方法 2.类型调用成员方法(函数是以对象为基本单位的) 3.对象调用非静态方法 4.类型创建对象
3.练习 1.判断Lambda表达式正确性 2.方法引用转换为Lambda表达式(要素:逻辑,参数,返回值) 3.方法引用实例
4.函数式编程的技巧(重要) 1.定义函数式接口:定义参数 + 返回值 2.Lambda表达式:根据参数和返回值写出逻辑 3.方法引用:根据逻辑反推出参数和返回值
2.函数对象的类型 1.如何归类? 2.归类练习 3.自定义函数式接口 1.函数式接口加上@FunctionalInterface 在编译时检查是否函数式接口有且只有一个抽象方法 2.练习案例
3.JDK提供的函数式接口 1.IntPredicate 参数为int,返回值为boolean 2.IntBinaryOperator 参数为两个int,返回值为int 3.Supplier 参数为空,返回值为泛型 4.Function 参数为泛型,返回值也为泛型 5.实例 6.常见函数接口
7.练习 1.将判断条件使用函数式接口替换 Predicate
2.一个参数一个返回值 Function
3.将具体逻辑使用函数式接口替换 Consumer
4.没有参数,一个返回值 Supplier
4.方法引用 1.基本介绍 2.六种方法引用的格式 1.类名::静态方法 1.介绍 2.使用lambda表达式的方式,打印所有学生 forEach 3.使用方法引用,打印所有学生 forEach 4.使用lambda表达式的方式,打印所有男学生 filter 5.使用方法引用的方式,打印所有男学生 filter
2.类名:非静态方法 1.介绍 2.使用lambda表达式的方式,打印所有男学生 filter 3.使用方法引用的方式,打印所有男学生 filter
3.对象:非静态方法 1.介绍 2.输出 3.过滤性别是男的 4.将男生转化为女生
4.类名::new
5.this::非静态方法和super::非静态方法 1.使用this过滤男性 2.使用super过滤男性
6.六种使用方式对比
1.函数对象表现形式
1.Lambda表达式(功能全面)
1.基本语法
2.只有一行逻辑,该逻辑结果是返回值
3.复杂逻辑
4.省略参数类型(可以通过上下文推导出类型时,比如实现了函数式接口)
5.只有一个参数时,参数的圆括号可以省略,但是也不能加参数类型
2.方法引用(写法简洁)
1.类型调用静态方法
2.类型调用成员方法(函数是以对象为基本单位的)
3.对象调用非静态方法
4.类型创建对象
3.练习
1.判断Lambda表达式正确性
2.方法引用转换为Lambda表达式(要素:逻辑,参数,返回值)
3.方法引用实例
4.函数式编程的技巧(重要)
1.定义函数式接口:定义参数 + 返回值
2.Lambda表达式:根据参数和返回值写出逻辑
3.方法引用:根据逻辑反推出参数和返回值
2.函数对象的类型
1.如何归类?
2.归类练习
3.自定义函数式接口
1.函数式接口加上@FunctionalInterface 在编译时检查是否函数式接口有且只有一个抽象方法
package com. sun. first ;
public class Sample2 {
public static void main ( String [ ] args) {
Fun fun = ( a) -> {
return a + 1 ;
} ;
System . out. println ( fun. test ( 2 ) ) ;
}
}
@FunctionalInterface
interface Fun {
int test ( int a) ;
}
2.练习案例
package com. sun. first ;
import java. util. ArrayList ;
public class Sample2 {
public static void main ( String [ ] args) {
Fun fun = ( a) -> {
return a + 1 ;
} ;
System . out. println ( fun. test ( 2 ) ) ;
Fun1 fun1 = ( int a, int b, int c) -> a + b + c;
Fun2 fun2 = ( int a, int b) -> a - b;
Fun3 fun3 = ( ) -> new String ( ) ;
Fun4 fun4 = ( ) -> new ArrayList < String > ( ) ;
Fun5 < String , Integer > fun5 = ( str) -> {
return Integer . valueOf ( str) ;
} ;
System . out. println ( fun5. test ( "34" ) ) ;
}
}
@FunctionalInterface
interface Fun {
int test ( int a) ;
}
@FunctionalInterface
interface Fun1 {
int test ( int a, int b, int c) ;
}
@FunctionalInterface
interface Fun2 {
int test ( int a, int b) ;
}
@FunctionalInterface
interface Fun3 {
String test ( ) ;
}
@FunctionalInterface
interface Fun4 {
ArrayList < String > test ( ) ;
}
@FunctionalInterface
interface Fun5 < I , O > {
O test ( I input) ;
}
3.JDK提供的函数式接口
1.IntPredicate 参数为int,返回值为boolean
2.IntBinaryOperator 参数为两个int,返回值为int
3.Supplier 参数为空,返回值为泛型
4.Function 参数为泛型,返回值也为泛型
5.实例
6.常见函数接口
1.概览
2.命名规则
7.练习
1.将判断条件使用函数式接口替换 Predicate
1.题目
2.答案
package com. sun. first ;
import java. util. ArrayList ;
import java. util. Arrays ;
import java. util. List ;
import java. util. function. Predicate ;
public class Sample4 {
public static void main ( String [ ] args) {
List < Integer > filter = filter ( Arrays . asList ( 1 , 2 , 3 ) , i -> {
return ( i & 1 ) == 0 ;
} ) ;
System . out. println ( "filter = " + filter) ;
}
static List < Integer > filter ( List < Integer > list, Predicate < Integer > predicate) {
List < Integer > res = new ArrayList < > ( ) ;
for ( Integer i : list) {
if ( predicate. test ( i) ) {
res. add ( i) ;
}
}
return res;
}
}
2.一个参数一个返回值 Function
1.题目
2.答案
package com. sun. first ;
import java. util. ArrayList ;
import java. util. Arrays ;
import java. util. List ;
import java. util. function. Function ;
public class Sample5 {
public static void main ( String [ ] args) {
List < String > map = map ( Arrays . asList ( 1 , 2 , 3 ) , a -> {
return String . valueOf ( a) ;
} ) ;
System . out. println ( map) ;
}
static List < String > map ( List < Integer > list, Function < Integer , String > function) {
List < String > res = new ArrayList < > ( ) ;
for ( Integer i : list) {
res. add ( function. apply ( i) ) ;
}
return res;
}
}
3.将具体逻辑使用函数式接口替换 Consumer
1.题目
2.结果
package com. sun. first ;
import java. util. Arrays ;
import java. util. List ;
import java. util. function. Consumer ;
public class Sample6 {
public static void main ( String [ ] args) {
consume ( Arrays . asList ( 1 , 3 , 3 ) , num -> {
System . out. println ( "num = " + num) ;
} ) ;
}
static void consume ( List < Integer > list, Consumer < Integer > consumer) {
for ( Integer num : list) {
consumer. accept ( num) ;
}
}
}
4.没有参数,一个返回值 Supplier
1.题目
2.答案
package com. sun. first ;
import java. util. ArrayList ;
import java. util. List ;
import java. util. concurrent. ThreadLocalRandom ;
import java. util. function. Supplier ;
public class Sample7 {
public static void main ( String [ ] args) {
List < Integer > supply = supply ( 3 , ( ) -> {
int res = ThreadLocalRandom . current ( ) . nextInt ( ) ;
System . out. println ( res) ;
return res;
} ) ;
}
static List < Integer > supply ( int count, Supplier < Integer > supplier) {
List < Integer > result = new ArrayList < > ( ) ;
for ( int i = 0 ; i < count; i++ ) {
result. add ( supplier. get ( ) ) ;
}
return result;
}
}
4.方法引用
1.基本介绍
2.六种方法引用的格式
1.类名::静态方法
1.介绍
2.使用lambda表达式的方式,打印所有学生 forEach
package com. sun. methodref ;
import java. util. stream. Stream ;
public class MethodRef01 {
public static void main ( String [ ] args) {
Stream . of (
new Student ( "libai" , "男" ) ,
new Student ( "dufu" , "男" ) ,
new Student ( "lishimin" , "女" )
) . forEach ( student -> {
System . out. println ( student) ;
} ) ;
}
}
class Student {
private String name;
private String sex;
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public String getSex ( ) {
return sex;
}
public void setSex ( String sex) {
this . sex = sex;
}
public Student ( String name, String sex) {
this . name = name;
this . sex = sex;
}
@Override
public String toString ( ) {
return "Student{" +
"name='" + name + '\'' +
", sex='" + sex + '\'' +
'}' ;
}
}
3.使用方法引用,打印所有学生 forEach
public class MethodRef01 {
public static void main ( String [ ] args) {
Stream . of (
new Student ( "libai" , "男" ) ,
new Student ( "dufu" , "男" ) ,
new Student ( "lishimin" , "女" )
) . forEach ( MethodRef01 :: abc ) ;
}
public static void abc ( Student student) {
System . out. println ( student) ;
}
}
4.使用lambda表达式的方式,打印所有男学生 filter
public static void main ( String [ ] args) {
Stream . of (
new Student ( "libai" , "男" ) ,
new Student ( "dufu" , "男" ) ,
new Student ( "lishimin" , "女" )
) . filter ( student -> {
if ( student. getSex ( ) . equals ( "男" ) ) {
return true ;
}
return false ;
} )
. forEach ( MethodRef01 :: abc ) ;
}
public static void abc ( Student student) {
System . out. println ( student) ;
}
}
5.使用方法引用的方式,打印所有男学生 filter
public static void main ( String [ ] args) {
Stream . of (
new Student ( "libai" , "男" ) ,
new Student ( "dufu" , "男" ) ,
new Student ( "lishimin" , "女" )
) . filter ( MethodRef01 :: filter ) .
forEach ( MethodRef01 :: abc ) ;
}
public static boolean filter ( Student student) {
return student. getSex ( ) . equals ( "男" ) ;
}
public static void abc ( Student student) {
System . out. println ( student) ;
}
2.类名:非静态方法
1.介绍
2.使用lambda表达式的方式,打印所有男学生 filter
public static void main ( String [ ] args) {
Stream . of (
new Student ( "libai" , "男" ) ,
new Student ( "dufu" , "男" ) ,
new Student ( "lishimin" , "女" )
) . filter ( student -> {
return student. isMale ( student) ;
} )
. forEach ( MethodRef01 :: abc ) ;
}
3.使用方法引用的方式,打印所有男学生 filter
public void main ( String [ ] args) {
Stream . of (
new Student ( "libai" , "男" ) ,
new Student ( "dufu" , "男" ) ,
new Student ( "lishimin" , "女" )
) . filter ( Student :: isMale )
. forEach ( MethodRef01 :: abc ) ;
}
3.对象:非静态方法
1.介绍
2.输出
3.过滤性别是男的
4.将男生转化为女生
4.类名::new
1.介绍
2.调用不同参数个数的构造
package com. sun. first ;
import java. util. function. BiFunction ;
import java. util. function. Function ;
import java. util. function. Supplier ;
public class Sample9 {
public static void main ( String [ ] args) {
Supplier < Student > m1 = Student :: new ;
Student student = m1. get ( ) ;
System . out. println ( "student = " + student) ;
Function < String , Student > m2 = Student :: new ;
Student sun = m2. apply ( "sun" ) ;
System . out. println ( "sun = " + sun) ;
BiFunction < String , String , Student > m3 = Student :: new ;
Student apply = m3. apply ( "sun" , "男" ) ;
System . out. println ( "apply = " + apply) ;
}
}
5.this::非静态方法和super::非静态方法
1.使用this过滤男性
package com. sun. first ;
import java. util. stream. Stream ;
public class Sample10 {
Boolean isMan ( Student student) {
return student. getSex ( ) . equals ( "男" ) ;
}
public void main ( String [ ] args) {
Stream . of (
new Student ( "libai" , "男" ) ,
new Student ( "dufu" , "男" ) ,
new Student ( "lishimin" , "女" )
) . filter ( this :: isMan ) ;
}
}
2.使用super过滤男性
6.六种使用方式对比