java.util.function 中的 Function、Predicate、Consumer

來源:互聯網
上載者:User

標籤:interface   傳回值   執行   參數   vat   out   就是   返回   java   

函數式介面:

函數式介面(Functional Interface)就是一個有且僅有一個抽象方法,但可以有多個非抽象方法的介面。

函數式介面可以被隱式轉換為 Lambda 運算式。

Function 函數Function 與 BiFunction

輸入一個或多個參數,也可以規定傳回值類型,並執行一段邏輯

Function<Integer, Integer> function = num -> num + 1;Function<Integer, Integer> function1 = num -> num * 2;System.out.println(function.apply(1));      // out:2System.out.println(function1.compose(function).apply(1));   // out:4System.out.println(function1.andThen(function).apply(1));   // out:3BiFunction<Integer, Integer, Long> bF = (i1, i2) -> Long.parseLong(i1+i2+"");System.out.println(bF.apply(1, 2));      // out:3DIYBiFunction<Integer, Integer, Integer, Integer> diyBiFunction = (n1,n2,n3) -> n1+n2+n3;System.out.println(diyBiFunction.apply(1,2,3)); //out:6public interface DIYBiFunction<T, U, E, R> {  R apply(T t, U u, E e);}public class DIYBiFunctionImpl implements DIYBiFunction {  @Override  public Object apply(Object o, Object o2, Object o3) {    if (o instanceof Integer        && o2 instanceof Integer        && o3 instanceof Integer) {      return (Integer)o + (Integer)o2 + (Integer)o3;    } else {      return null;    }  }}
Predicate 謂詞:

判斷輸入的對象是否符合某個條件

BiPredicate
public class BiPredicateTest {     public static void main(String[] args) {         // 表示一個謂詞         Predicate<String> p1 = p -> p.length() > 2;         System.out.println(p1.test("1"));         System.out.println(p1.test("123"));          BiPredicate<Integer, String> biPredicate = (i , s) -> s.length() > i;         System.out.println(biPredicate.test(1, "12"));     } } 
Consumer :

接收一個參數,並執行一段邏輯

BiConsumer
public class BiConsumerTest {     public static void main(String[] args) {         Map<Integer, String> map = Maps.newHashMap();         map.put(1, "a");         map.put(2, "b");         map.put(3, "c");         BiConsumer<Integer, String> biConsumer = new BiConsumer<Integer, String>() {             @Override             public void accept(Integer integer, String s) {                 System.out.println(String.format("out:%s-%s", integer, s));             }         };         map.forEach(biConsumer);         map.forEach(new BiC1());         map.forEach(new BiC2());     }     private static class BiC1 implements BiConsumer<Integer, String> {         @Override         public void accept(Integer integer, String s) {             System.out.println(String.format("BiC1, out: %s - %s", integer, s));         }     }     private static class BiC2 implements BiConsumer<Integer, String> {         @Override         public void accept(Integer integer, String s) {             System.out.println(String.format("BiC2, out: %s - %s", integer, s));         }     } } 

 

java.util.function 中的 Function、Predicate、Consumer

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.