1、default关键字
直接在接口定义一个默认方法,也就是说这个接口的实现类实现了这个接口之后,不用管这个default修饰的方法,也可以直接调用,如下。
public interface NewCharacter { public void test1(); public default void test2(){ System.out.println("我是新特性1"); } }
public class NewCharacterImpl implements NewCharacter{ @Override public void test1() { } public static void main(String[] args) { NewCharacter nca = new NewCharacterImpl(); nca.test2(); } }
优点:所以说这个default方法是所有的实现类都不需要去实现的就可以直接调用,那么比如说jdk的集合List里面增加了一个sort方法,那么如果定义为一个抽象方法,其所有的实现类如arrayList,LinkedList等都需要对其添加实现,那么现在用default定义一个默认的方法之后,其实现类可以直接使用这个方法了,这样不管是开发还是维护项目,都会大大简化代码量。
一些使用了新特性的例子:
WebMvcConfigurer:https://www.jianshu.com/p/77482f0b59e9
2、Lambda
表达式的基础语法 : Java8 中引入了一个新的操作符 "->" 该操作符称为箭头操作符或 Lambda 操作符,箭头操作符将 Lambda 表达式拆分成两部分 :
左侧 : Lambda 表达式的参数列表
右侧 : Lambda 表达式中所需执行的功能, 即 Lambda 体
法格式一 : 无参数,无返回值
() -> System.out.println("Hello Lambda!");
语法格式二 : 有一个参数,并且无返回值
(x) -> System.out.println(x)
语法格式三 : 若只有一个参数,小括号可以省略不写
x -> System.out.println(x)
Consumer<String> con = (x) -> System.out.println(x);
con.accept("啦啦啦,我是卖报的小行家");
语法格式四 : 有两个以上的参数,有返回值,并且 Lambda 体中有多条语句
Comparator<Integer> com = (x, y) -> {
System.out.println("函数式接口");
return Integer.compare(x, y);
};
语法格式五 : 若 Lambda 体中只有一条语句, return 和 大括号都可以省略不写
Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
语法格式六 : Lambda 表达式的参数列表的数据类型可以省略不写,因为JVM编译器通过上下文推断出,数据类型,即“类型推断”
(Integer x, Integer y) -> Integer.compare(x, y);
注 : Lambda 表达式中的参数类型都是由编译器推断得出的。 Lambda 表达式中无需指定类型,程序依然可以编译,这是因为 javac 根据程序的上下文,在后台推断出了参数的类型。 Lambda 表达式的类型依赖于上下文环境,是由编译器推断出来的。这就是所谓的 “类型推断”。
Lambda 表达式需要 “函数式接口” 的支持
函数式接口 : 接口中只有一个抽象方法的接口,称为函数式接口,可以通过 Lambda 表达式来创建该接口的对象 (若 Lambda表达式抛出一个受检异常,那么该异常需要在目标接口的抽象方法上进行声明) 可以使用注解 @FunctionalInterface 修饰可以检查是否是函数式接口,同时 javadoc 也会包含一条声明,说明这个接口是一个函数式接口
@FunctionalInterface
public interface MyFun {
Integer getValue(Integer num);
}
函数式接口中使用泛型 :
@FunctionalInterface
public interface MyFun<T> {
T getValue(T t);
}
从匿名类到 Lambda 的转换
// 原来的匿名内部类作为参数传递
Comparator<Integer> comparator = new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return Integer.compare(o1, o2);
}
};
TreeSet<Integer> treeSet = new TreeSet<>(comparator);
// Lambda 表达式作为参数传递
Comparator<Integer> comparator = (o1, o2) -> Integer.compare(o1, o2);
TreeSet<Integer> treeSet = new TreeSet<>(comparator);
参考:start------------------------------------------------------------------------------------------------
作为参数传递 Lambda 表达式 : 为了将 Lambda 表达式作为参数传递,接收Lambda 表达式的参数类型必须是与该 Lambda 表达式兼容的函数式接口的类型
需求 : 雇员对象如下,有一个包含许多员工信息的对象 employees,要求获取当前公司中员工年龄大于 35 的员工信息
public class Employee {
private String name;
private int age;
public Employee() {
}
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
// ...
@Override
public String toString() {
return "Employee{" + "name='" + name + '\'' + ", age=" + age + '}';
}
}
最简单的方式是采用 foreach 循环遍历,以下是各种优化方式
优化方式一 : 策略设计模式
@FunctionalInterface
public interface MyPredicate<T> {
boolean test(T t);
}
public class FilterEmployeeByAge implements MyPredicate<Employee> {
@Override
public boolean test(Employee employee) {
return employee.getAge() >= 35;
}
}
public List<Employee> filterEmployees3(List<Employee> employees, MyPredicate<Employee> myPredicate) {
List<Employee> emps = new ArrayList<>();
for (Employee employee : employees) {
if (myPredicate.test(employee)) {
emps.add(employee);
}
}
return emps;
}
调用
List<Employee> emps = filterEmployees3(employees, new FilterEmployeeByAge());
for (Employee employee : emps) {
System.out.println(employee.toString());
}
优化方式二 : 匿名内部类
List<Employee> emps = filterEmployees3(employees, new MyPredicate<Employee>() {
@Override
public boolean test(Employee employee) {
return employee.getAge() >= 35;
}
});
for (Employee employee : emps) {
System.out.println(employee.toString());
}
优化方式三 : Lambda
filterEmployees3(employees, employee -> employee.getAge() >= 35).forEach(System.out::println);
优化方式四 : Stream API
employees.stream().filter((employee) -> employee.getAge() >= 35).limit(5).forEach(System.out::println);
参考:end------------------------------------------------------------------------------------------------
其他案例地址:https://blog.csdn.net/chenshun123/article/details/78122467
3、java8 内置的四大核心函数式接口
java8 内置的四大核心函数式接口
第一种:Consumer<T>:消费型接口
void accept(T t);
第二种:Supplier<T>:供给型接口
T get();
第三种:Function<T,R>:函数型接口
R apply(T t);
函数型接口,forExample:
方法:
//需求:用于处理字符串
public static String strHandler(String str, Function<String,String> function){
return function.apply(str);
}
//调用
String subStr = strHandler("我大北大荒威武", (str) -> str.substring(2, 5));
注意:该接口可以传方法如下:
//调用
String subStr = strHandler("我大北大荒威武", (str) -> testFunct(str));
System.out.println("testFunct"+subStr);
方法:这是一个自己写的截取字符串方法
public static String testFunct(String str){
return str.substring(2, 5);
}
//需求:用于处理字符串
public static String strHandler(String str, Function<String,String> function){
return function.apply(str);
}
第四种:Predicate<T>:断言型接口
boolean test(T t)
4、其他接口:
方法引用
若 Lambda 体中的功能,已经有方法提供实现,可以使用方法引用 (可以将方法引用理解为 Lambda 表达式的另外一种表现形式)
1> 对象的引用 :: 实例方法名
2> 类名 :: 静态方法名
3> 类名 :: 实例方法名
注 : 1> 方法引用所引用的方法的参数列表与返回值类型,需要与函数式接口中抽象方法的参数列表和返回值类型保持一致(就是函数签名和返回值一致)
2> 若Lambda 的参数列表的第一个参数,是实例方法的调用者,第二个参数(或无参)是实例方法的参数时,格式 : ClassName::MethodName
PrintStream ps = System.out;
Consumer<String> con1 = (str) -> ps.println(str);
Consumer<String> con2 = ps::println;
Consumer<String> con3 = System.out::println;
对象的引用 :: 实例方法名
Employee emp = new Employee(101, "张三", 18, 9999.99);
Supplier<String> sup = () -> emp.getName();
System.out.println(sup.get());
Supplier<String> sup2 = emp::getName;
System.out.println(sup2.get());
类名 :: 静态方法名
Comparator<Integer> comparator1 = (x, y) -> Integer.compare(x, y);
Comparator<Integer> comparator2 = Integer::compare;
BiFunction<Double, Double, Double> fun = (x, y) -> Math.max(x, y);
System.out.println(fun.apply(1.5, 22.2));
BiFunction<Double, Double, Double> fun2 = Math::max;
System.out.println(fun2.apply(1.2, 1.5));
类名 :: 实例方法名
BiPredicate<String, String> bp = (x, y) -> x.equals(y);
System.out.println(bp.test("abcde", "abcde"));
BiPredicate<String, String> bp2 = String::equals;
System.out.println(bp2.test("abc", "abc"));
Function<Employee, String> fun = (e) -> e.show();
System.out.println(fun.apply(new Employee()));
Function<Employee, String> fun2 = Employee::show;
System.out.println(fun2.apply(new Employee()));
注 : 当需要引用方法的第一个参数是调用对象,并且第二个参数是需要引用方法的第二个参数 (或无参数) 时 : ClassName::methodName
构造器引用
构造器的参数列表,需要与函数式接口中参数列表保持一致 (就是函数签名一致)
1> 类名 :: new
Supplier<Employee> sup = () -> new Employee();
System.out.println(sup.get());
Supplier<Employee> sup2 = Employee::new;
System.out.println(sup2.get());
Function<String, Employee> fun = Employee::new;
BiFunction<String, Integer, Employee> fun2 = Employee::new;
数组引用
1> 类型[] :: new
Function<Integer, String[]> fun = (args) -> new String[args];
String[] strs = fun.apply(10);
System.out.println(strs.length);
Function<Integer, Employee[]> fun2 = Employee[]::new;
Employee[] emps = fun2.apply(20);
System.out.println(emps.length);
四大核心函数式接口具体参考https://blog.csdn.net/kaishizige/article/details/79300506
lambda表达式详解地址:https://blog.csdn.net/chenshun123/article/details/78122467
感谢,引用地址文章的原作者