Java8-新特性及Lambda表达式
创始人
2024-02-17 12:36:30
0

1、Java8新特性内容概述

1.1、简介

Java 8(又称为jdk1.8)是Java语言开发的一个主要版本

Java 8是oracle公司于2014年3月发布,可以看成是自Java 5以来最具革命性的版本。Java 8为Java语言、编译器、类库、开发工具与JVM带来了大量新特性

1.2、新特性思维导图总结

在这里插入图片描述

1.3、新特性好处

  • 速度更快
  • 代码更少==(增加了新的语法:lambda表达式)==
  • 强大的StreamAPI
  • 最大化减少空指针异常:Optional
  • Nashorn引擎,允许在JVM上运行JS应用

2、Lambda表达式概述

2.1、为什么使用

lambda是一个匿名函数,我们可以把lambda表达式理解为是一段可以传递的代码(将代码像数据一样进行传递)。使用它可以写出更简洁、更灵活的代码。作为一种更紧凑的代码风格,使Java的语言表达能力得到了提示。

2.2、语法

Lamdba表达式:在Java8语言中引入的一种新的语法元素和操作符。这个操作符为->,该操作符被称为Lambda操作符或箭头操作符。它将Lambda分为两个部分:

  • 左侧:指定了Lambda表达式需要的参数列表
  • 右侧:指定了Lambda体,是抽象方法的实现逻辑,也即Lambda表达式要执行的功能

举例:

(o1,o2) -> Integer.compare(o1,o2)

格式:

  • ->:lambda操作符 或 箭头操作符
  • 左边:lambda形参列表(其实就是接口中的抽象方法的形参列表)
  • 右边:lambda体(其实就是重写的抽象方法的方法体)

3、lambda表达式快速使用

大致分为6种情况讲解

  • 无参无返回值

    Runnable run = () -> {System.out.println(111);
    };
    
  • 需要一个参数,但没有返回值

    Consumer con = (String x) -> {System.out.println(x);
    };
    
  • 数据类型可以省略,因为可由编译器推断出来,称为类型推断

    Consumer con = (x) -> {System.out.println(x);
    };
    
  • 若只需要一个参数时,参数的小括号可以省略

    Consumer con = str -> {System.out.println(str);
    };
    
  • 若需要两个以上的参数,多条执行语句,并且可以有返回值

    Comparator con = (x,y) -> {System.out.println(x);System.out.println(y);return Integer.compare(x,y);
    };
    
  • 当Lambda体只有一条语句时,return与大括号若有,都可以省略

    Comparator con = (x,y) -> Integer.compare(x,y);
    

无参无返回值

因为Runnable接口里的唯一接口方法run()方法就是无参无返回值的方法,所以我们可以使用Lambda表示

public static void main(String[] args){Runnable runn = () -> {System.out.println("hello world!");};runn.run(); //hello world!
}

需要一个参数,但没有返回值

Consumer接口里的唯一接口方法accept就是接收一个参数,无返回值的方法,所以可以使用Lambda表示

public static void main(String[] args){Consumer con = (String s) ->{System.out.println(s);};con.accept("hello world!"); // hello world!
}

数据类型可以省略

//Java可以类型推断,前面定义泛型,后面参数就不可能是其他类型了,所以可以省略
Consumer con = (s) ->{System.out.println(s);
};
con.accept("hello world!"); // hello world!

若只需要一个参数时,参数的小括号可以省略

// 因为我们只有一个参数 s,所以小括号也可以省略,只有在参数个数等于1的时候,才能省略,小于或者大于都不能省略!!!
Consumer con = s ->{System.out.println(s);
};
con.accept("hello world!"); // hello world!

若需要两个以上的参数,多条执行语句,并且可以有返回值

public static void main(String[] args){Comparator com = (x,y) -> {System.out.println(x); // 1System.out.println(y); // 5return Integer.compare(x,y);};int i = com.compare(1, 5);System.out.println(i); // -1
}

当Lambda体只有一条语句时,return与大括号若有,都可以省略

Comparator com = (x,y) -> Integer.compare(x,y);
int i = com.compare(1, 5);
System.out.println(i); // -1

4、函数式接口

4.1、如何理解函数式接口

  • Java从诞生起就一直倡导一切皆对象,在Java里面面向对象(OOP)编程是一切。但是随着python、scala等语言的星期和新技术的挑战,Java不得不做出调整以便支持更加广泛的技术要求,也即Java不但可以支持OOP还可以支持OOF(面向函数编程)
  • 在函数式编程语言当中,函数被当做一等公民对待。在将函数作为一等公民的编程语言中,Lambda表达式的类型是函数。但是在Java8中,有所不同。在Java8中,Labda表达式是对象,而不是函数,它们必须依附于一类特别的对象类型——函数式接口
  • 简单的说,在java8中,Lambda表达式就是一个函数式接口的实例。这就是Lambda表达式和函数式接口的关系。也就是说,只要一个对象是函数式接口的实例,那么该对象就可以用Lambda表达式来表示
  • 所以以前用匿名实现类表示的现在都可以用Lambda表达式来写

4.2、什么是函数式(Function)接口

  • 只包含一个抽象方法的接口,成为函数式接口
  • 你可以通过Lambda表达式来创建该接口的对象。(若Lambda表达式抛出一个受检异常(即:非运行时异常),那么该异常需要在目标接口的抽象方法上进行声明)
  • 我们可以在一个接口上使用==@FunctionalInterface==注解,这样做可以检查它是否是一个函数式接口。同时javadoc也会包含一条声明,说明这个接口是一个函数式接口
  • 在java.util.function包下定义了Java8的丰富的函数式接口

4.3、Java内置四大核心函数式接口

Java提供了四大核心函数式接口

  • Consumer:消费性接口。对类型为T的对象应用操作,包含方法:void accept(T t)
  • Supplier:供给型接口。返回类型为T的对象,包含方法:T get()
  • Function:函数型接口。对类型为T的对象应用操作,并返回结果。结果是R类型的对象。包含方法:R apply(T t)
  • Predicate:断定型接口。确定类型为T的对象是否满足某约束,并返回boolean值。包含方法:boolean test(T t)

在这里插入图片描述

Consumer

public static void main(String[] args){Consumer con = s -> System.out.println(s);con.accept("hello world!"); // hello world!
}

Supplier

public static void main(String[] args){Supplier sup = () -> "hello world";String s = sup.get();System.out.println(s); // hello world
}

Function

public static void main(String[] args){// 输入类型为Integer类型,返回结果为String类型Function fun = (a) -> String.valueOf(a);String apply = fun.apply(5201314);System.out.println(apply); // 5201314
}

Predicate

public static void main(String[] args){Predicate pre = (s) -> s == null;boolean aaa = pre.test("aaa");System.out.println(aaa); // falseboolean test = pre.test(null);System.out.println(test); // true
}

4.4、其他接口

除了上面四大核心接口外,还提供了针对四大核心接口的增强接口,有需要的可以直接使用

在这里插入图片描述

5、方法引用

  • 当要传递给Lambda体的操作,已经有实现的方法了,可以使用方法引用

  • 方法引用可以看做是Lambda表达式深层次的表达。换句话说,方法引用就是Lambda表达式,也就是函数式接口的一个实例,通过方法的名字来指向一个方法,可以认为是Lambda表达式的一个语法糖

  • 要求:实现接口的抽象方法的参数列表和返回值类型,必须与方法引用的方法的参数列表和返回值类型保持一致

  • 如下三种主要使用情况

    • 对象::实例方法名

    • 类::静态方法名

    • 类::实例方法名

5.1、对象::实例方法名

// 情况一:对象::方法
// Consumer中的void accept(T t)
// PrintSteam中的void println(T t)
public static void main(String[] args){Consumer con1 = a -> System.out.println(a);con1.accept("hello"); // helloSystem.out.println("*****************************");PrintStream out = System.out;// 对象::方法,结果和上面一样Consumer con2  = out::println;con2.accept("world!"); // world
}

5.2、类::静态方法名

// 情况二:类::静态方法
// Comparator中的int compare(T o1, T o2)
// Integer中的static int compare(int x, int y)
public static void main(String[] args){Comparator com = (x,y) -> x.compareTo(y);int i = com.compare(10, 20);System.out.println(i); // -1System.out.println("***************************");Comparator com2 = Integer::compare;int i2 = com2.compare(10, 20);System.out.println(i2); // -1
}

5.3、类::实例方法名

// 情况二:类::实例方法
// Comparator中的int compare(T o1, T o2)
// Integer中的int compareTo(Integer anotherInteger)
// 虽然参数数量不一致,但是调用方法为这样结构的 o1.xxx(o2),可以写 o1的类名::o1要调用的方法
public static void main(String[] args){Comparator com = (x,y) -> x.compareTo(y);int i = com.compare(10, 20);System.out.println(i); // -1System.out.println("***************************");Comparator com2 = Integer::compareTo;int i2 = com2.compare(10, 20);System.out.println(i2); // -1
}

6、构造器引用

6.1、无参构造

public class Test{public static void main(String[] args){// Supplier中的 T get()// Student中的 public Student() 无参构造Supplier sup = () -> new Student();System.out.println(sup.get()); // Student{name='null', age=null, birthday=null}// 构造引用写法Supplier sup2 = Student::new;System.out.println(sup2.get()); // Student{name='null', age=null, birthday=null}}
}class Student{private String name;private Integer age;private LocalDateTime birthday;@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", age=" + age +", birthday=" + birthday +'}';}public Student() {}public Student(String name, Integer age, LocalDateTime birthday) {this.name = name;this.age = age;this.birthday = birthday;}public Student(String name, Integer age) {this.name = name;this.age = age;}public Student(String name) {this.name = name;}
}

6.2、单参数构造

public class Test{public static void main(String[] args){// Function中的 R apply(T t)// Student中的 public Student(String name) 单参构造Function fun = a -> new Student(a);Student apply = fun.apply("张三");System.out.println(apply); // Student{name='张三', age=null, birthday=null}// 构造引用写法Function fun2 = Student::new;Student apply1 = fun2.apply("李四");System.out.println(apply1); // Student{name='李四', age=null, birthday=null}}
}//... 下面实体类省略

6.2、双参数构造

public class Test{public static void main(String[] args){// BiFunction中的 R apply(T t, U u)// Student中的 public Student(String name, Integer age) 双参构造BiFunction biFun1 = (a,b) -> new Student(a,b);Student apply = biFun1.apply("张三", 20);System.out.println(apply); // Student{name='张三', age=20, birthday=null}// 构造引用写法BiFunction biFun2 = Student::new;Student apply2 = biFun2.apply("李四", 25);System.out.println(apply2); // Student{name='李四', age=25, birthday=null}}
}
//... 下面实体类省略

6.3、三参数构造

三参构造我们上面讲解Java的函数式接口并没有提到,我也不知道,所以需要自己写一个函数式接口,满足业务需求

1、定义函数式接口

@FunctionalInterface
interface MyFunction{R apply(T t, U u, O o);
}

2、使用自定义函数式接口

public static void main(String[] args){// MyFunction中的 R apply(T t, U u, O o)// Student中的 public Student(String name, Integer age, LocalDateTime birthday) 三参构造MyFunction mf = (a,b,c) -> new Student(a,b,c);Student apply = mf.apply("张三", 20, LocalDateTime.now());System.out.println(apply); // Student{name='张三', age=20, birthday=2022-11-25T16:01:10.610}// 构造引用写法MyFunction mf2 = Student::new;Student apply2 = mf.apply("李四", 25, LocalDateTime.now());System.out.println(apply2); // Student{name='李四', age=25, birthday=2022-11-25T16:01:10.611}
}

7、数组引用

就把数组当成特殊的类,使用new关键字创建

public static void main(String[] args){// MyFunction中的 R apply(T t, U u, O o)// Student中的 public Student(String name, Integer age, LocalDateTime birthday) 三参构造Function fun = length -> new String[length];String[] strings = fun.apply(3);for (String string : strings) {/*nullnullnull*/System.out.println(string);}System.out.println("**********************************************");// 数组引用Function fun2 = String[]::new;String[] strings2 = fun2.apply(4);for (String s : strings2) {/*nullnullnullnull*/System.out.println(s);}
}

相关内容

热门资讯

【深圳特区报】深港融通新格局 ... 前海港资企业突破万家、累计105项制度创新成果在全国复制推广、现代服务业增加值达1460亿元……12...
犯罪对象和受贿数额认定问题分析 实践中,有的行贿人为了送给国家工作人员好处,不直接送给国家工作人员财物,而是先委托国家工作人员代为出...
用好制度创新“加速器” 制度创新是破解发展难题、激发区域活力的核心密钥。上海浦东开发开放30余载的实践证明,唯有以制度创新破...
紫牛热点︱家庭纠纷导致情绪失控... 扬子晚报网12月21日讯(记者 郭一鹏) 12月20日下午,一段男子在街头拦停一辆越野车,追砸车辆前...
多城出台政策对老房子“强制体检... 越来越多城市的老房子,要“强制体检”了。 最近,郑州市房管局发布了一则实施方案,将对房龄30年以上的...
创新思路解难题 布吉街道成功解... 深圳市龙岗区布吉街道是全市人口最密集的街道之一,小区物业管理纠纷较多,长期面临利益冲突尖锐复杂、制度...
涉嫌受贿 尹锡悦夫妇将遭共同起... 韩国前总统尹锡悦妻子金建希(中)。 新华社发 当地时间12月21日,韩国“金建希特检组”称已完成对...
政策再“上新”,免税经济添活力 □ 本报记者 宋晓华 财政部等五部门前不久联合印发《关于完善免税店政策支持提振消费的通知》,明确自1...