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{
public R change(P p);
}
class Demo{
public static void main(String[] args) {
IFunction function = String::valueOf ;
String str = function.change(12);
System.out.println(str.length());
// 2
}
}
方法引用可以为一个方法定义多个名字 要求是函数式接口
示例:引用实例对象方法
// String toUpperCase()
@FunctionalInterface
interface IFunction{
public R upper();
}
class Demo{
public static void main(String[] args) {
IFunction function = "Hello Java"::toUpperCase ;
System.out.println(function.upper());
// HELLO JAVA
}
}
引用普通方法一般都需要实例化对象,如果不给出实例化对象,还要引用普通方法 可以使用引用特定类型的方法
示例:引用特定类型的方法
// int compareTo(String anotherString)
@FunctionalInterface
interface IFunction{
public int compare(P p1, P p2);
}
class Demo{
public static void main(String[] args) {
IFunction 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" ;
}
}
@FunctionalInterface
interface IFunction{
public C create(String name, int age);
}
class Demo{
public static void main(String[] args) {
IFunction function = Person::new ;
System.out.println(function.create("张三", 18));
// Person
}
}
方法引用,只是弥补对引用支持
133 内建函数式接口如果自定义Lambda表达式,需要使用@FunctionalInterface注解来声明
java.util.function 可以直接使用函数式接口
1、功能性函数式接口
@FunctionalInterface
public interface Function{
R apply(T t);
}f
示例
import java.util.function.Function;
class Demo{
public static void main(String[] args) {
Function fun = "**Hello"::startsWith;
System.out.println(fun.apply("**"));
// true
}
}
2、消费型函数式接口 没有返回值
@FunctionalInterface
public interface Consumer{
void accept(T t);
}
示例
import java.util.function.Consumer;
class Demo{
public static void main(String[] args) {
Consumer fun = System.out::println;
fun.accept("Hello");
// Hello
}
}
3、供给型函数式接口 没有接收参数,有返回值
@FunctionalInterface
public interface Supplier{
T get();
}
示例
import java.util.function.Supplier;
class Demo{
public static void main(String[] args) {
Supplier fun = "Hello Java"::toLowerCase;
System.out.println(fun.get());
// hello java
}
}
4、断言型函数式接口 进行判断处理
@FunctionalInterface
public interface Predicate{
boolean test(T t);
}
示例
import java.util.function.Predicate;
class Demo{
public static void main(String[] args) {
Predicate fun = "Hello"::equalsIgnoreCase;
System.out.println(fun.test("HELLO"));
// true
}
}