欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Java8 函數(shù)式接口理解及測試

 更新時間:2017年08月09日 09:13:31   投稿:jingxian  
下面小編就為大家?guī)硪黄贘ava8 函數(shù)式接口理解及測試。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

1. 函數(shù)式接口的理解

根據(jù)重構的思想,需要把容易變化的模塊進行抽象并封裝起來,從這個點來看,Java8新引入的函數(shù)式接口就是基于這個思想進行設計的。

2. 函數(shù)式接口定義 

2.1 自定義如下

需要FunctionalInterface關鍵字顯示聲明:

@FunctionalInterface
 public interface AppleInterface {
public void test();
 }

2.2 系統(tǒng)預定義

java.util.function.Consumer;
java.util.function.Function;
java.util.function.Predicate;
java.util.function.Supplier;  

可以去查看源碼了解具體的細節(jié),這幾個接口包括了常用的一些場景,一般可滿足需要

3. 函數(shù)式接口的使用

函數(shù)式接口一般使用前需要先定義,也可以使用系統(tǒng)預定義的幾個函數(shù)式接口

函數(shù)式接口的使用和使用一個變量沒有區(qū)別,顯示聲明定義,格式如下:

FunctionInterface interface=null;

這里的interface雖然看起來是一個變量,可是實際卻是一段行為代碼,用于執(zhí)行具體的業(yè)務邏輯,可以自由在方法接口間傳遞,也可以直接執(zhí)行

interface.doSomeThing();

如定義函數(shù)式接口為參數(shù)的接口:

public void filter(FunctionInterface interface)
{
 interface.doSomeThing();
}

4. 函數(shù)式接口練習

4.1 自定義實體類Apple

public class Apple {
  private String color;
  private float weight;

  public Apple(String color, float weight) {
  this.color = color;
  this.weight = weight;
  }

  public String getColor() {
  return color;
  }

  public void setColor(String color) {
  this.color = color;
  }

  public float getWeight() {
  return weight;
  }

  public void setWeight(float weight) {
  this.weight = weight;
  }
}

4.2 自定義函數(shù)式接口

該接口有一個test方法,不接收任何參數(shù),也沒有任何返回

@FunctionalInterface
public interface AppleInterface {
  public void test();
}

4.3 測試自定義函數(shù)式接口

 @Test
  public void DefineFunctionInterface(){
  //自定義函數(shù)式接口
  AppleInterface at=()->System.out.println("define FunctionInterface AppleInterface.");
  at.test();
  }

至此,就完成一個很簡單的函數(shù)式接口的定義和調(diào)用

4.4 系統(tǒng)預定義函數(shù)式接口

Consumer<T>:該接口接收一個對象T,返回void,測試如下

 @Test
  public void ConsumerTest(){
  Consumer<Apple> consumer=(Apple app)->{System.out.println(app.getColor()+","+app.getWeight());};
  List<Apple> apps=Arrays.asList(new Apple("red", 120),new Apple("blue", 80),
 new Apple("green",100));
  ConsumerApple(apps,consumer);
  }

  public void ConsumerApple(List<Apple> apps,Consumer<Apple> c){
  for(Apple app:apps){
 c.accept(app);
  }
  }

Supplier<T>:該接口不接收任何參數(shù),返回一個對象T,測試如下:

 @Test
  public void SupplierTest(){
  Supplier<Apple> supplier=()->{return new Apple("hello supplier",999);};
  Apple app=supplier.get();
  System.out.println(app.getColor()+","+app.getWeight());
  }

Predicate<T>:該接口接收一個對象T,返回一個Boolean

 @Test
  public void PredicateTest(){
  //系統(tǒng)預定義函數(shù)式接口測試
  Predicate<Apple> p1=(Apple a)->{if(a.getWeight()>90) return true;return false;};
  Predicate<Apple> p2=(Apple a)->{if(a.getColor().equals("blue")) return true;return false;};

  List<Apple> apps=Arrays.asList(new Apple("red", 120),new Apple("blue", 80),
 new Apple("green",100));

  filterApple(apps,p1);//篩選重量大于90g的蘋果
  filterApple(apps,p2);//篩選藍色的蘋果
  }

  public void filterApple(List<Apple> apps,Predicate<Apple> p){
  for(Apple app:apps){
 if(p.test(app)){
 System.out.println(app.getColor()+","+app.getWeight());
 }
  }

  } 

Function<T,R>: 該接口接收一個對象T,經(jīng)過轉換判斷,返回一個對象R

 @Test
  public void FunctionTest(){
  Function<String,Apple> function=(String s)->{return new Apple(s,666);};
  Apple app=function.apply("red");
  System.out.println(app.getColor()+","+app.getWeight());
  app=function.apply("green");
  System.out.println(app.getColor()+","+app.getWeight());

  }

以上這篇基于Java8 函數(shù)式接口理解及測試就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • 使用idea搭建spring項目,利用xml文件的形式進行配置方式

    使用idea搭建spring項目,利用xml文件的形式進行配置方式

    本文介紹了如何使用SpringIOC和SpringDI的思想開發(fā)一個打印機模擬程序,實現(xiàn)了靈活配置彩色墨盒或灰色墨盒以及打印頁面大小的功能,通過創(chuàng)建接口和實現(xiàn)類,并在配置文件中進行依賴注入,實現(xiàn)了控制反轉
    2024-11-11
  • Spring中的Sentinel熔斷降級詳解

    Spring中的Sentinel熔斷降級詳解

    這篇文章主要介紹了Spring中的Sentinel熔斷降級詳解,熔斷降級是一種保護系統(tǒng)穩(wěn)定性和可用性的機制,旨在防止故障的擴散和蔓延,提高用戶體驗和信任度,需要的朋友可以參考下
    2023-09-09
  • java實現(xiàn)TCP socket和UDP socket的實例

    java實現(xiàn)TCP socket和UDP socket的實例

    這篇文章主要介紹了本文主要介紹了java實現(xiàn)TCP socket和UDP socket的實例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • MybatisX 快速開發(fā)插件過程詳解

    MybatisX 快速開發(fā)插件過程詳解

    MybatisX 是一款基于 IDEA 的快速開發(fā)插件,方便在使用mybatis以及mybatis-plus開始時簡化繁瑣的重復操作,提高開發(fā)速率。這篇文章主要介紹了MybatisX 快速開發(fā)插件,需要的朋友可以參考下
    2021-10-10
  • 關于SpringMVC對Restful風格的支持詳解

    關于SpringMVC對Restful風格的支持詳解

    Restful就是一個資源定位及資源操作的風格,不是標準也不是協(xié)議,只是一種風格,是對http協(xié)議的詮釋,下面這篇文章主要給大家介紹了關于SpringMVC對Restful風格支持的相關資料,需要的朋友可以參考下
    2022-01-01
  • Spring注解之@Conditional使用解析

    Spring注解之@Conditional使用解析

    這篇文章主要介紹了Spring注解之@Conditional使用解析,@Conditional注解可以說是SpringBoot的條件注解,表示組件只有在所有指定條件都匹配時才有資格注冊,條件是可以在 bean 定義注冊之前??以編程方式確定的任何狀態(tài),需要的朋友可以參考下
    2024-01-01
  • Java的HashTable源碼解讀

    Java的HashTable源碼解讀

    這篇文章主要介紹了Java的HashTable源碼解讀,HashTable繼承了Dictionary類,提供了一些字典相關的基本功能如添加、刪除、判空、獲取元素數(shù)量等,需要的朋友可以參考下
    2023-12-12
  • Java異常中toString()和getMessage()區(qū)別

    Java異常中toString()和getMessage()區(qū)別

    在java異常體系中,要打印異常信息,可以通過:e.getMessage() 、 e.toString() e.printStackTrace() 等方法打印,本文主要介紹了Java異常中toString()和getMessage()區(qū)別,具有一定的參考價值,感興趣的可以了解一下
    2024-01-01
  • Javac/javap 自帶工具簡單使用講解

    Javac/javap 自帶工具簡單使用講解

    這篇文章主要介紹了Javac/javap 自帶工具簡單使用講解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03
  • java?poi?讀取單元格null或者空字符串方式

    java?poi?讀取單元格null或者空字符串方式

    這篇文章主要介紹了java?poi?讀取單元格null或者空字符串方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12

最新評論