Java学习路线-12:Lambda表达式与函数式编程

举报
彭世瑜 发表于 2021/08/13 23:23:23 2021/08/13
1.7k+ 0 0
【摘要】 第29 章 : 函数式编程 131 Lambda表达式 JDK >= 1.8 函数式编程语言: Scala https://www.scala-lang.org/ haskell https://www.haskell.org/ interface IMessage{ public void send(); } class Demo{ public sta...

第29 章 : 函数式编程

131 Lambda表达式

JDK >= 1.8
函数式编程语言:
Scala https://www.scala-lang.org/
haskell https://www.haskell.org/

interface IMessage{ public void send();
}

class Demo{ public static void main(String[] args) { IMessage message = new IMessage() { public void send(){ System.out.println("Hello Java"); } }; message.send(); // Hello Java }
}
  
 

使用Lambda

@FunctionalInterface
interface IMessage{ public void send();
}

class Demo{ public static void main(String[] args) { IMessage message = () -> { System.out.println("Hello Java"); }; message.send(); // Hello Java }
}
  
 

使用Lambda 表达式的要求:
SAM (Single Abstract Method)只有一个抽象方法
被称为函数式接口, 里边的方法只能有一个

Lambda表达式的格式:
方法没有参数: ()->{};
方法有参数:(参数, 参数)->{};
如果只有一行语句返回:(参数, 参数)->语句;


// 函数式接口
@FunctionalInterface
interface IMath{ public int add(int x, int y);
}

class Demo{ public static void main(String[] args) { IMath math = (x, y) -> { return x + y ; }; System.out.println(math.add(1, 1)); // 2 }
}
  
 

一行返回语句简化写法

IMath math = (x, y) -> x + y ;

  
 

优势:简化代码

132 方法引用

引用数据类型最大的特点是可以进行内存指向处理
JDK < 1.8 对象引用操作
JDK >= 1.8 方法引用操作:不同的方法名可以描述同一个方法

引用静态方法: 类名称::static 方法名称
引用实例对象方法: 实例化对象::普通方法
引用特定类型的方法:特定类::普通方法
引用构造方法:类名称::new

示例:引用静态方法

// static String valueOf(int i)

@FunctionalInterface
interface IFunction<P, R>{ public R change(P p);
}

class Demo{ public static void main(String[] args) { IFunction <Integer, String> function = String::valueOf ; String str = function.change(12); System.out.println(str.length()); // 2 }
}
  
 

方法引用可以为一个方法定义多个名字
要求是函数式接口

示例:引用实例对象方法

// String   toUpperCase()
@FunctionalInterface
interface IFunction<R>{ public R upper();
}

class Demo{ public static void main(String[] args) { IFunction <String> function = "Hello Java"::toUpperCase ; System.out.println(function.upper()); // HELLO JAVA }
}
  
 

引用普通方法一般都需要实例化对象,如果不给出实例化对象,还要引用普通方法
可以使用引用特定类型的方法

示例:引用特定类型的方法

// int  compareTo(String anotherString)
@FunctionalInterface
interface IFunction<P>{ public int compare(P p1, P p2);
}

class Demo{ public static void main(String[] args) { IFunction <String> function = String::compareTo ; System.out.println(function.compare("hello", "java")); // -2 }
}
  
 

示例:引用构造方法

class Person{ private String name ; private int age ; public Person(String name, int age){ this.name = name ; this.age = age ; } @Override public String toString(){ return "Person<" + this.name + " " + this.age + ">" ; }
}

@FunctionalInterface
interface IFunction<C>{ public C create(String name, int age);
}

class Demo{ public static void main(String[] args) { IFunction <Person> function = Person::new ; System.out.println(function.create("张三", 18)); // Person<张三 18> }
}
  
 

方法引用,只是弥补对引用支持

133 内建函数式接口

如果自定义Lambda表达式,需要使用@FunctionalInterface注解来声明

java.util.function 可以直接使用函数式接口

1、功能性函数式接口

@FunctionalInterface
public interface Function<T,R>{ R apply(T t);
}f
  
 

示例

import java.util.function.Function;

class Demo{ public static void main(String[] args) { Function<String, Boolean> fun = "**Hello"::startsWith; System.out.println(fun.apply("**")); // true }
}
  
 

2、消费型函数式接口
没有返回值

@FunctionalInterface
public interface Consumer<T>{ void accept(T t);
}

  
 

示例

import java.util.function.Consumer;

class Demo{ public static void main(String[] args) { Consumer<String> fun = System.out::println; fun.accept("Hello"); // Hello }
}
  
 

3、供给型函数式接口
没有接收参数,有返回值

@FunctionalInterface
public interface Supplier<T>{ T   get();
}

  
 

示例

import java.util.function.Supplier;

class Demo{ public static void main(String[] args) { Supplier<String> fun = "Hello Java"::toLowerCase; System.out.println(fun.get()); // hello java }
}
  
 

4、断言型函数式接口
进行判断处理

@FunctionalInterface
public interface Predicate<T>{ boolean test(T t);
}

  
 

示例

import java.util.function.Predicate;

class Demo{ public static void main(String[] args) { Predicate<String> fun = "Hello"::equalsIgnoreCase; System.out.println(fun.test("HELLO")); // true }
}
  
 

文章来源: pengshiyu.blog.csdn.net,作者:彭世瑜,版权归原作者所有,如需转载,请联系作者。

原文链接:pengshiyu.blog.csdn.net/article/details/102945761

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。