一. Java8新特性简介html
1,速度更快java
2,代码更少(增长了新的语法Lambda表达式)数组
3,强大的Stream APIapp
4,便于并行dom
5,最大化减小空指针异常Optionalide
二. Lambda表达式函数
1. 为何使用Lambda表达式ui
Lambda 是一个匿名函数,咱们能够把 Lambda表达式理解为是一段能够传递的代码(将代码像数据同样进行传递)。能够写出更简洁、更灵活的代码。做为一种更紧凑的代码风格,使Java的语言表达能力获得了提高。 spa
2. Lambda表达式的关键:从匿名类到 Lambda 的转换.net
示例:
Runnable runnable = new Runnable() { @Override public void run() { System.out.println("hello"); } }; runnable.run(); Runnable runnable2 = () -> System.out.println("hello2"); runnable2.run();
3. Lambda表达式语法
Lambda表达式在Java 语言中引入了一个新的语法元
素和操做符。这个操做符为 “->” , 该操做符被称
为 Lambda 操做符或剪头操做符。它将 Lambda 分为
两个部分:
左侧: 指定了 Lambda 表达式须要的全部参数
右侧: 指定了 Lambda 体,即 Lambda 表达式要执行
的功能。
(1)语法格式一:无参,无返回值,Lambda 体只需一条语句
示例:
Runnable r1 = () -> System.out.println("Hello Lambda!");
(2)语法格式二:Lambda 须要一个参数
示例:
Consumer<String> con = (x) -> System.out.println(x);
(3)语法格式三:Lambda 只须要一个参数时,参数的小括号能够省略
示例:
Consumer<String> con = x -> System.out.println(x);
(4)语法格式四:Lambda 须要两个参数,而且有返回值
示例:
Comparator<Integer> com = (x, y) -> { System.out.println("函数式接口"); return Integer.compare(x, y); };
(5)语法格式五:当 Lambda 体只有一条语句时,return 与大括号能够省略
示例:
Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
(6)Lambda 表达式的参数列表的数据类型能够省略不写,由于JVM编译器经过上下文推断出,数据类型,即“类型推断”
示例:
Comparator<Integer> com = (Integer x,Integer y) -> {//Integer 类型能够省略 System.out.println("函数式接口"); return Integer.compare(x, y); };
省略参数类型:
Comparator<Integer> com = (x,y) -> { System.out.println("函数式接口"); return Integer.compare(x, y); };
类型推断:Lambda 表达式中的参数类型都是由编译器推断 得出的。 Lambda 表达式中无需指定类型,程序依然可 以编译,这是由于 javac 根据程序的上下文,在后台 推断出了参数的类型。 Lambda 表达式的类型依赖于上 下文环境,是由编译器推断出来的。这就是所谓的 “类型推断”
三. 函数式接口
1. 什么是函数式接口
(1)只包含一个抽象方法的接口,称为函数式接口。
(2)你能够经过 Lambda 表达式来建立该接口的对象。(若 Lambda 表达式抛出一个受检异常,那么该异常须要在目标接口的抽象方 法上进行声明)。
(3)咱们能够在任意函数式接口上使用 @FunctionalInterface 注解, 这样作能够检查它是不是一个函数式接口,同时 javadoc 也会包 含一条声明,说明这个接口是一个函数式接口。
2. 自定义函数接口
@FunctionalInterface public interface TestMyFunction { public double getValue(); }
函数式接口中使用泛型:
@FunctionalInterface public interface TestMyFunction<T> { public T getValue(T t); }
3. 做为参数传递的Lambda表达式
public String toUpperString(TestMyFunction<String> mf,String str) { return mf.getValue(str); }
做为参数传递的lambda表达式:
String newStr = toUpperString((str) -> str.toUpperCase(),"abc"); System.out.println(newStr);
做为参数传递 Lambda 表达式:为了将 Lambda(TestMyFunction<String> mf) 表达式做为参数传递,接收Lambda 表达式的参数类型必须是与该 Lambda 表达式兼容的函数式接口
的类型。
4,Java 内置四大核心函数式接口
(1)Consumer<T> : 消费型接口
void accept(T t);
示例:
//Consumer<T> 消费型接口 : @Test public void test1() { happy(10000, (m) -> System.out.println("大家刚哥喜欢大宝剑,每次消费:" + m + "元")); } public void happy(double money, Consumer<Double> con) { con.accept(money); }
(2)Supplier<T> : 供给型接口 T get();
示例:
//Supplier<T> 供给型接口 : @Test public void test2() { List<Integer> numList = getNumList(10, () -> (int) (Math.random() * 100)); for (Integer num : numList) { System.out.println(num); } } //需求:产生指定个数的整数,并放入集合中 public List<Integer> getNumList(int num, Supplier<Integer> sup) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < num; i++) { Integer n = sup.get(); list.add(n); } return list; }
(3),Function<T, R> : 函数型接口 R apply(T t);
示例:
@Test public void test3() { String newStr = strHandler("\t\t\t 我大尚硅谷威武 ", (str) -> str.trim()); System.out.println(newStr); String subStr = strHandler("我大尚硅谷威武", (str) -> str.substring(2, 5)); System.out.println(subStr); } // 需求:用于处理字符串 public String strHandler(String str, Function<String, String> fun) { return fun.apply(str); }
(4),Predicate<T> : 断言型接口 boolean test(T t);
示例:
//Predicate<T> 断言型接口: @Test public void test4() { List<String> list = Arrays.asList("Hello", "atguigu", "Lambda", "www", "ok"); List<String> strList = filterStr(list, (s) -> s.length() > 3); for (String str : strList) { System.out.println(str); } } // 需求:将知足条件的字符串,放入集合中 public List<String> filterStr(List<String> list, Predicate<String> pre) { List<String> strList = new ArrayList<>(); for (String str : list) { if (pre.test(str)) { strList.add(str); } } return strList; }
5,其它接口
6,方法引用和构造器引用
方法引用:
当要传递给Lambda体的操做,已经有实现的方法了,可使用方法引用!
(实现抽象方法的参数列表,必须与方法引用方法的参数列表保持一致! )
方法引用:使用操做符 “::” 将方法名和对象或类的名字分隔开来。
以下三种主要使用状况:
(1)对象::实例方法
例如:
@Test public void test1() { PrintStream ps = System.out; Consumer<String> con = (str) -> ps.println(str); con.accept("Hello World!"); System.out.println("--------------------------------"); Consumer<String> con2 = ps::println; con2.accept("Hello Java8!"); Consumer<String> con3 = System.out::println; } @Test public void test2() { Employee emp = new Employee(101, "张三", 18, 9999.99); Supplier<String> sup = () -> emp.getName(); System.out.println(sup.get()); System.out.println("----------------------------------"); Supplier<String> sup2 = emp::getName; System.out.println(sup2.get()); }
(2)类::静态方法
例如:
@Test public void test() { BiFunction<Double, Double, Double> fun = (x, y) -> Math.max(x, y); System.out.println(fun.apply(1.5, 22.2)); System.out.println("--------------------------------------------------"); BiFunction<Double, Double, Double> fun2 = Math::max; System.out.println(fun2.apply(1.2, 1.5)); } @Test public void test4() { Comparator<Integer> com = (x, y) -> Integer.compare(x, y); System.out.println("-------------------------------------"); Comparator<Integer> com2 = Integer::compare; }
(3)类::实例方法
例如:
@Test public void test5() { BiPredicate<String, String> bp = (x, y) -> x.equals(y); System.out.println(bp.test("abcde", "abcde")); System.out.println("-----------------------------------------"); BiPredicate<String, String> bp2 = String::equals; System.out.println(bp2.test("abc", "abc")); System.out.println("-----------------------------------------"); Function<Employee, String> fun = (e) -> e.show(); System.out.println(fun.apply(new Employee())); System.out.println("-----------------------------------------"); Function<Employee, String> fun2 = Employee::show; System.out.println(fun2.apply(new Employee())); }
注意:
* ①方法引用所引用的方法的参数列表与返回值类型,须要与函数式接口中抽象方法的参数列表和返回值类型保持一致!
* ②若Lambda 的参数列表的第一个参数,是实例方法的调用者,第二个参数(或无参)是实例方法的参数时,格式: ClassName::MethodName
构造器引用:构造器的参数列表,须要与函数式接口中参数列表保持一致!
格式: ClassName::new
与函数式接口相结合,自动与函数式接口中方法兼容。
能够把构造器引用赋值给定义的方法,与构造器参数
列表要与接口中抽象方法的参数列表一致!
Function<Integer,MyClass> fun = (n) -> new MyClass(n); //等同于 Function<Integer,MyClass> fun2 = MyClass::new;
数组引用:
格式: type[] ::new
Function<Integer,Integer[]> fun = (n) -> new Integer[n]; //等同于 Function<Integer,Integer[]> fun2 = Integer[]::new;
Lambda其余博文介绍:
https://www.cnblogs.com/zhenghengbin/p/9418961.html