Java8新特性:Lambda表達式

Lambda表達式,案例一:new Thread(() -> System.out.println("thread"));javascript

Lambda表達式,案例二:由參數/箭頭和主體組成:java

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

以前的代碼形式:app

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

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

  }函數

}測試

針對這種情形,咱們怎麼理解呢?其實很簡單,上看一下上述lambda表達式的語法:() -> {}(): 括號就是接口方法的括號,接口方法若是有參數,也須要寫參數。只有一個參數時,括號能夠省略。-> : 分割左右部分的,沒啥好講的。{} : 要實現的方法體。只有一行代碼時,能夠不加括號,能夠不寫return。在控制檯打印,不返回任何值(看起來像是返回void)  ui

函數式接口就是隻顯式聲明一個抽象方法的接口。爲保證方法數量很少很多,java8提供了一個專用註解@FunctionalInterface,這樣,當接口中聲明的抽象方法多於或少於一個時就會報錯。以下圖所示:
this

Lambda表達式和函數式接口結合

步驟:spa

  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(); }

運行結果

其餘形式的函數式接口及實現

上述內容實現了無參無返回值的函數接口與實現,固然還有其餘形式:

有參無返回值

接口

@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");

運行

無參有返回值

接口

@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);

運行

有參有返回值

接口

@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表達式的定義所覆蓋。
接口 參數 返回值 類別 示例
Consumer T void 消費型接口 輸出一個值
Supplier None T 供給型接口 工廠方法
Function T R 函數型接口 得到 Artist 對象的名字
Predicate T boolean 斷言型接口 這張唱片已經發行了嗎


 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個字的水果)
相關文章
相關標籤/搜索