JDK 8.0 新特性——函数式接口和Lambda 表达式

函数式接口

       抽象方法只定义一个的接口即为函数式接口,如下例:

[java]  view plain  copy
  1. @FunctionalInterface  
  2. interface IMammal {  
  3.       
  4.     String NAME = "哺乳动物";  
  5.   
  6.     void move();  
  7.   
  8.     public default void eat() {  
  9.         System.out.println("哺乳动物正在吃......");  
  10.     }  
  11. }  

注意:

              1、可以使用@FunctionalInterface注解来验证一个接口是不是函数式接口;

              2、函数式接口中可以定义多个常量,也可以定义多个默认方法,还可以定义多个静态方法,但只能定义一个抽象方法;

              3、java.lang.Runnable、java.util.Comparator<T>都是函数式接口;

              4、函数式接口中只允许定义一个抽象方法,但是还可以定义java.lang.Object中的public方法,如下例:

[java]  view plain  copy
  1. @FunctionalInterface  
  2. interface IMammal {  
  3.   
  4.     void move();  
  5.       
  6.     boolean equals(Object obj);  
  7. }  

Lambda 表达式

       内部类分为有名内部类和匿名内部类,Lambda 表达式的出现简化函数式接口匿名内部类的语法,其表达式语法如下:([参数1], [参数2], [参数3],.... [参数n])->{代码块},如下例:

示例1

匿名内部类  

[java]  view plain  copy
  1. @FunctionalInterface  
  2. interface IArrayUtil{  
  3.     void iterate(Object [] array);  
  4. }  
  5.   
  6. public class Test {  
  7.       
  8.     public static void main(String args[]){  
  9.         IArrayUtil arrayUtil = new IArrayUtil() {  
  10.             @Override  
  11.             public void iterate(Object[] array) {  
  12.                 for (Object obj : array) {  
  13.                     System.out.println(obj);  
  14.                 }  
  15.             }  
  16.         };  
  17.           
  18.         arrayUtil.iterate(new String[] {"小张","小王"});  
  19.     }  
  20. }  

Lambda 表达式

[java]  view plain  copy
  1. @FunctionalInterface  
  2. interface IArrayUtil {  
  3.     void iterate(Object[] array);  
  4. }  
  5.   
  6. public class Test {  
  7.   
  8.     public static void main(String args[]) {  
  9.         IArrayUtil arrayUtil = (Object[] array) -> {//array参数前的数组数据类型可以去掉  
  10.             for (Object obj : array) {  
  11.                 System.out.println(obj);  
  12.             }  
  13.         };  
  14.   
  15.         arrayUtil.iterate(new String[] { "小张""小王"});  
  16.     }  
  17. }  

示例2

匿名内部类

[java]  view plain  copy
  1. import java.util.Date;  
  2.   
  3. public class Test {  
  4.   
  5.     public static void main(String[] args) {  
  6.         Runnable runnable = new Runnable() {  
  7.             @Override  
  8.             public void run() {  
  9.                 System.out.println(new Date());  
  10.             }  
  11.         };  
  12.         new Thread(runnable).start();  
  13.     }  
  14. }  
Lambda 表达式
[java]  view plain  copy
  1. import java.util.Date;  
  2.   
  3. public class Test {  
  4.   
  5.     public static void main(String[] args) {  
  6.         new Thread(() -> {  
  7.             System.out.println(new Date());  
  8.         }).start();  
  9.     }  
  10. }  

注意:

              1、如果方法没有返回值且只有一行代码,则Lambda表达式语法可以是这种形式: ([ 参数1], [ 参数2], [参数3],.... [参数n]) -> 单行语句 ,如下例:
[java]  view plain  copy
  1. @FunctionalInterface  
  2. interface IMammal {  
  3.     void move(String name);  
  4. }  
  5.   
  6. public class Test {  
  7.   
  8.     public static void main(String[] args) {  
  9.         IMammal whale = (name) -> {  
  10.             System.out.println(name+"正在移动......");  
  11.         };  
  12.         whale.move("鲸鱼");  
  13.     }  
  14. }  

上面代码等效于如下代码:

[java]  view plain  copy
  1. @FunctionalInterface  
  2. interface IMammal {  
  3.     void move(String name);  
  4. }  
  5.    
  6. public class Test {  
  7.    
  8.     public static void main(String[] args) {  
  9.         IMammal whale = (name) -> System.out.println(name+"正在移动......");  
  10.         whale.move("鲸鱼");  
  11.     }  
  12. }  
2、如果方法返回值且只有一行代码,则Lambda表达式语法可以是这种形式: ([ 参数1], [ 参数2], [参数3],.... [参数n]) -> 表达式 ,如下例:

[java]  view plain  copy
  1. @FunctionalInterface  
  2. interface IComputer {  
  3.     int add(int a, int b);  
  4. }  
  5.   
  6. public class Test {  
  7.   
  8.     public static void main(String[] args) {  
  9.         IComputer computer = (a, b) -> {  
  10.             return a+b;  
  11.         };  
  12.         int result = computer.add(1,2);  
  13.         System.out.println(result);  
  14.     }  
  15. }  
上面代码等效于如下代码:
[java]  view plain  copy
  1. @FunctionalInterface  
  2. interface IComputer {  
  3.     int add(int a, int b);  
  4. }  
  5.   
  6. public class Test {  
  7.   
  8.     public static void main(String[] args) {  
  9.         IComputer computer = (a, b) -> a+b;  
  10.         int result = computer.add(1,2);  
  11.         System.out.println(result);  
  12.     }  
  13. }  

猜你喜欢

转载自blog.csdn.net/guo147369/article/details/78389847