函数式接口
抽象方法只定义一个的接口即为函数式接口,如下例:
- @FunctionalInterface
- interface IMammal {
-
- String NAME = "哺乳动物";
-
- void move();
-
- public default void eat() {
- System.out.println("哺乳动物正在吃......");
- }
- }
注意:
1、可以使用@FunctionalInterface注解来验证一个接口是不是函数式接口;
2、函数式接口中可以定义多个常量,也可以定义多个默认方法,还可以定义多个静态方法,但只能定义一个抽象方法;
3、java.lang.Runnable、java.util.Comparator<T>都是函数式接口;
4、函数式接口中只允许定义一个抽象方法,但是还可以定义java.lang.Object中的public方法,如下例:
- @FunctionalInterface
- interface IMammal {
-
- void move();
-
- boolean equals(Object obj);
- }
Lambda 表达式
内部类分为有名内部类和匿名内部类,Lambda 表达式的出现简化了函数式接口匿名内部类的语法,其表达式语法如下:([参数1], [参数2], [参数3],.... [参数n])->{代码块},如下例:
示例1
匿名内部类:
- @FunctionalInterface
- interface IArrayUtil{
- void iterate(Object [] array);
- }
-
- public class Test {
-
- public static void main(String args[]){
- IArrayUtil arrayUtil = new IArrayUtil() {
- @Override
- public void iterate(Object[] array) {
- for (Object obj : array) {
- System.out.println(obj);
- }
- }
- };
-
- arrayUtil.iterate(new String[] {"小张","小王"});
- }
- }
Lambda 表达式:
- @FunctionalInterface
- interface IArrayUtil {
- void iterate(Object[] array);
- }
-
- public class Test {
-
- public static void main(String args[]) {
- IArrayUtil arrayUtil = (Object[] array) -> {
- for (Object obj : array) {
- System.out.println(obj);
- }
- };
-
- arrayUtil.iterate(new String[] { "小张", "小王"});
- }
- }
示例2
匿名内部类:
- import java.util.Date;
-
- public class Test {
-
- public static void main(String[] args) {
- Runnable runnable = new Runnable() {
- @Override
- public void run() {
- System.out.println(new Date());
- }
- };
- new Thread(runnable).start();
- }
- }
Lambda 表达式:
- import java.util.Date;
-
- public class Test {
-
- public static void main(String[] args) {
- new Thread(() -> {
- System.out.println(new Date());
- }).start();
- }
- }
注意:
1、如果方法没有返回值且只有一行代码,则Lambda表达式语法可以是这种形式:
([
参数1], [
参数2], [参数3],.... [参数n])
->
单行语句
,如下例:
- @FunctionalInterface
- interface IMammal {
- void move(String name);
- }
-
- public class Test {
-
- public static void main(String[] args) {
- IMammal whale = (name) -> {
- System.out.println(name+"正在移动......");
- };
- whale.move("鲸鱼");
- }
- }
上面代码等效于如下代码:
- @FunctionalInterface
- interface IMammal {
- void move(String name);
- }
-
- public class Test {
-
- public static void main(String[] args) {
- IMammal whale = (name) -> System.out.println(name+"正在移动......");
- whale.move("鲸鱼");
- }
- }
2、如果方法有返回值且只有一行代码,则Lambda表达式语法可以是这种形式:
([
参数1], [
参数2], [参数3],.... [参数n])
->
表达式
,如下例:
- @FunctionalInterface
- interface IComputer {
- int add(int a, int b);
- }
-
- public class Test {
-
- public static void main(String[] args) {
- IComputer computer = (a, b) -> {
- return a+b;
- };
- int result = computer.add(1,2);
- System.out.println(result);
- }
- }
上面代码等效于如下代码:
- @FunctionalInterface
- interface IComputer {
- int add(int a, int b);
- }
-
- public class Test {
-
- public static void main(String[] args) {
- IComputer computer = (a, b) -> a+b;
- int result = computer.add(1,2);
- System.out.println(result);
- }
- }