Skip to content

Latest commit

 

History

History
executable file
·
194 lines (106 loc) · 4.69 KB

File metadata and controls

executable file
·
194 lines (106 loc) · 4.69 KB

JDK8.0(1.8)的新特性

1. Lambda表达式

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

1.1 概念

Lambda表达式在Java语言中引入了一个新的语法元素和操作符。这个操作符就是**->**,该操作符被称为Lambda操作符或者箭头符号。它将Lambda表达式分为两个部分:

  • 左侧:指定了Lambda表达式需要的所有参数(即方法中需要的参数)
  • 右侧:指定了Lambda体,即Lambda表达式要执行的功能

1.2 语法格式

Lambda表达式的参数列表里面的数据类型可以省略不写,因为JVM编译器可以通过上下文推断出数据类型(类型推断)。

  • 语法格式一

    // 无参数无返回值
    Runnable r2 = ()-> System.out.println("HelloLambda");
  • 语法格式二

    // 有一个参数,无返回值
    Consumer<String> consumer2 = (s)-> System.out.println(s);
  • 语法格式三

    // 若只有一个参数,并且没有返回值,()可以省略
    Consumer<String> consumer2 = s-> System.out.println(s);
  • 语法格式四

    // 有两个以上的参数,有返回值,并且Lambda体中有多条语句,Lambda体必须要写{}
    Comparator<Integer> com1 = (x, y)-> {
        System.out.println("函数式接口");
        return x-y;
    };
  • 语法格式五

    // 若Lambda体中只有一条语句,return和{}都可以省略不写
    Comparator<Integer> com2 = (x, y) -> x-y;

1.3 函数式接口

Lambda表达式需要函数式接口的支持。

函数式接口:接口中只有一个抽象方法的接口称为函数式接口。

可以使用注解@Functional Interface修饰,这个注解可以检查该接口是否为函数式接口

1.4 Java8内置的四大核心函数式接口

  • Consumer:消费型接口

    void accept(T t);

  • Supplier:供给型接口

    T get();

  • Function<T, R>:函数型接口

    R apply(T);

  • Predicate:断言型接口(判断用的)

    boolean test(T t);

2. 方法引用和构造器引用

2.1 方法引用

若Lambda体中的内容已经有方法实现了,我们可以使用方法引用(可以理解为Lambda表达式另一种体现形式)

  • 语法格式

    有三种格式,这三种格式都有限制条件,即引用的方法的参数列表和返回值都要和Lambda表达式中的参数列表和返回值一致。

    • 对象::实例方法名

      Consumer<String> con = (x)-> System.out.println(x);
      // 上面的代码其实可以改写为
      PrintStream ps = System.out;
      Consumer<String> con = (x)-> ps.println(x);
      con.accept("hello world");
      // Lambda体 System.out.println(x); 已经有方法可以实现该功能了,所以可以利用方法引用
      Consumer<String> con1 = ps::println;
      con1.accept("hello");
    • 类::静态方法名

      Comparator<Integer> com1 = (x, y) -> Integer.compare(x, y);
      System.out.println(com1.compare(2, 1));
      // 因为Comparator中的int compare(T o1, T o2)方法和Integer中的compare方法的返回值和参数列表相同,所以可以使用方法引用
      Comparator<Integer> com2 = Integer::compare;
      System.out.println(com2.compare(2, 5));
    • 类::实例方法名

      BiPredicate<String, String> bp1 = (str1, str2)-> str1.equals(str2);
      BiPredicate<String, String> bp2 = String::equals;
      // 使用这种格式需要有前提条件,上面的str1必须是equals()方法的调用者,str2必须作为参数传入这个方法中

2.2 构造器引用

// 无参构造器引用
Supplier<Employee> sup = ()-> new Employee();
Supplier<Employee> sup2 = Employee::new;
// 有参构造器引用
Function<Integer, Employee> fun = (age)->new Employee(age);
// 利用构造器引用
Function<Integer, Employee> fun = Employee::new;
System.out.println(fun.apply(23));

3. 接口中的默认方法和静态方法

例如

public interface MyFun {
	// 默认方法
	default String getName() {
		return "呵呵";
	}
	// 静态方法
	public static void show() {
		System.out.println("接口中的静态方法");
	}
}

如果一个类的父类和它实现的接口类中有同样的方法,那么该类会选择父类中的方法执行。