JDK8新特性:Lambda表达式

Lambda表达式,案例一:new Thread(() -> System.out.println(“thread”));

Lambda表达式,案例二:由参数/箭头和主体组成:

(Apple a1, Apple a2)  -> a1.getWeight().compareTo(a2.getWeight());

之前的代码形式:

Comparator<Apple> byWeight = new Comparator<Apple>() {
  public int compare(Apple a1, Apple a2) {

    return a1.getWeight().compareTo(a2.getWeight());

  }

}

针对这种情形,我们怎么理解呢?其实很简单,上看一下上述lambda表达式的语法:() -> {}(): 括号就是接口方法的括号,接口方法如果有参数,也需要写参数。只有一个参数时,括号可以省略。-> : 分割左右部分的,没啥好讲的。{} : 要实现的方法体。只有一行代码时,可以不加括号,可以不写return。在控制台打印,不返回任何值(看起来像是返回void)  

函数式接口就是只显式声明一个抽象方法的接口。为保证方法数量不多不少,java8提供了一个专用注解@FunctionalInterface,这样,当接口中声明的抽象方法多于或少于一个时就会报错。如下图所示:
《JDK8新特性:Lambda表达式》

Lambda表达式和函数式接口结合

步骤:

  1. 新建无参函数式接口(先演示无参);
  2. 新建包含属性为函数式接口的类;
  3. 实现函数式接口;
  4. 测试函数式接口的方法;

新建无参函数式接口

@FunctionalInterface public interface InterfaceWithNoParam { void run(); }

新建包含属性为函数式接口的类

public class TestJava8{

 //匿名内部类 InterfaceWithNoParam param1 = new InterfaceWithNoParam() { @Override public void run() { System.out.println("通过匿名内部类实现run()"); } }; //Lambda表达式 //空括号表示无参 InterfaceWithNoParam param = () -> System.out.println("通过Lambda表达式实现run()") ; }

测试函数式接口的方法

@Test public void testIntfaceWithNoparam() { this.param.run(); this.param1.run(); }

运行结果

《JDK8新特性:Lambda表达式》

其他形式的函数式接口及实现

上述内容实现了无参无返回值的函数接口与实现,当然还有其他形式:

有参无返回值

接口

@FunctionalInterface public interface InterfaceWithParams { void run(String s); }

实现

InterfaceWithParams params = new InterfaceWithParams() { @Override public void run(String s) { System.out.println("通过" + s + "实现run(String)"); } }; InterfaceWithParams params1 = (String s) -> System.out.println("通过" + s + "实现run(String)");

测试

    this.params.run("匿名类"); this.params1.run("Lambda");

运行

《JDK8新特性:Lambda表达式》

无参有返回值

接口

@FunctionalInterface public interface InterfaceUnVoidWithNoParam { String run(); }

实现

InterfaceUnVoidWithNoParam interfaceUnVoidWithNoParam = new InterfaceUnVoidWithNoParam() { @Override public String run() { return "Hello World!"; } }; InterfaceUnVoidWithNoParam interfaceUnVoidWithNoParam1 = () -> "Hello Lambda!";

测试

    String s = this.interfaceUnVoidWithNoParam.run(); System.out.println("返回结果是:"+s); String s0 = this.interfaceUnVoidWithNoParam1.run(); System.out.println("返回结果是:"+s0);

运行

《JDK8新特性:Lambda表达式》

有参有返回值

接口

@FunctionalInterface public interface InterfaceUnVoidWithParams { String run(Integer integer); }

实现

InterfaceUnVoidWithParams interfaceWithParams = new InterfaceUnVoidWithParams() { @Override public String run(Integer integer) { return String.valueOf(integer); } }; InterfaceUnVoidWithParams interfaceWithParams1 = (Integer integer) -> String.valueOf(integer);

测试

    String s1 = this.interfaceWithParams.run(1); System.out.println("您输入的是:"+s1); String s2 = this.interfaceWithParams1.run(2); System.out.println("您输入的是:"+s2);

进一步深入案例:
java.util.function中 Function, Supplier, Consumer, Predicate和其他函数式接口广泛用在支持lambda表达式的API中。这些接口有一个抽象方法,会被lambda表达式的定义所覆盖。
接口参数返回值类别示例
ConsumerTvoid消费型接口输出一个值
SupplierNoneT供给型接口工厂方法
FunctionTR函数型接口获得 Artist 对象的名字
PredicateTboolean断言型接口这张唱片已经发行了吗


 Predicate接口:(断言型接口)

@FunctionalInterface
interface Predicate<T> {

boolean test(T t);

}
/**
* 执行Predicate判断
* @param age 年龄
* @param predicate Predicate函数式接口
* @return 返回布尔类型结果
*/
public static boolean doPredicate(int age, Predicate<Integer> predicate) {
return predicate.test(age);
}

//Predicate<Integer> predicate = (age) -> age >= 18?true:false;

public static void main(String[] args) {
boolean isAdult = doPredicate(20, x -> x >= 18);
System.out.println(isAdult);
}
}

 

supply接口类:(供给类)

public static List<Integer> supply(Integer num, Supplier<Integer> supplier){
List<Integer> resultList = new ArrayList<Integer>() ;
for(int x=0;x<num;x++)
resultList.add(supplier.get());
return resultList ;
}

public static void main(String[] args) {
List<Integer> list = supply(6,() -> (int)(Math.random()*100));
list.forEach(System.out::println);
}

消费型接口示例:

public
static
void
donation(Integer money, Consumer<Integer> consumer){
 
consumer.accept(money);
}
public
static
void
main(String[] args) {
 
donation(
1000
, money -> System.out.println(
"好心的麦乐迪为Blade捐赠了"
+money+
"元"
)) ;
}  
函数型接口示例: 转换字符串为Integer  
public
static
Integer convert(String str, Function<String, Integer> function) {
 
return
function.apply(str);
}
public
static
void
main(String[] args) {
 
Integer value = convert(
"28"
, x -> Integer.parseInt(x));
}  
断言型接口示例:(筛选出只有2个字的水果)        

 

 

点赞