zoukankan      html  css  js  c++  java
  • Java 8新特性二: Functional Interfaces

    关注:Java提升营,最新文章第一时间送达,10T 免费学习资料随时领取!!!

    Functional Interfaces概念

    一个functional interface是仅包含一个抽象方法的接口。他们只能做一个操作。从Java 8开始,lambda表达式可用来表示functional interface的实例。functional interface可以有多个默认方法静态方法RunnableActionListenerComparable都是functional interface的一些示例。

    在Java 8之前,我们必须创建匿名内部类对象或实现这些接口。

    // Java program to demonstrate functional interface 
      
    class Test 
    { 
        public static void main(String args[]) 
        { 
            // create anonymous inner class object 
            new Thread(new Runnable() 
            { 
                @Override
                public void run() 
                { 
                    System.out.println("New thread created"); 
                } 
            }).start(); 
        } 
    } 
    

    输出:

    New thread created
    

    从Java 8开始,我们可以使用lambda表达式来代表functional interface的实例,如下所示:

    // Java program to demonstrate Implementation of 
    // functional interface using lambda expressions 
      
    class Test 
    { 
      public static void main(String args[]) 
      { 
      
        // lambda expression to create the object 
        new Thread(()-> 
           {System.out.println("New thread created");}).start(); 
      } 
    } 
    

    输出:

    New thread created
    

    @FunctionalInterface注解

    标注了@FunctionalInterface注解的接口可以确保不能有多个抽象方法。如果存在多个抽象方法,则编译器会报“Unexpected @FunctionalInterface annotation”错误。然而,并不强制使用该注解。

    // Java program to demonstrate lamda expressions to implement 
    // a user defined functional interface. 
      
    @FunctionalInterface
    interface Square 
    { 
        int calculate(int x); 
    } 
      
    class Test 
    { 
        public static void main(String args[]) 
        { 
            int a = 5; 
      
            // lambda expression to define the calculate method 
            Square s = (int x)->x*x; 
      
            // parameter passed and return type must be 
            // same as defined in the prototype 
            int ans = s.calculate(a); 
            System.out.println(ans); 
        } 
    } 
    

    输出:

    25
    

    java.util.function包

    Java 8中的java.util.function包 包含许多内置的functional interface,例如-

    • Predicate: Predicate接口有一个抽象方法test,该方法返回一个Boolean值。
    public interface Predicate
    {
       public boolean test(T  t);
     }
    
    • BinaryOperator: BinaryOperator接口具有一个抽象方法apply,该方法传入两个参数并返回相同类型的结果。
    public interface BinaryOperator 
    {
         public T apply(T x, T y);
    }     
    
    • Function:Function接口具有一个抽象方法apply,该方法传入类型为T的参数并返回类型为R的结果。
    public interface Function 
    {
       public R apply(T t);
    }
    
    // A simple program to demonstrate the use 
    // of predicate interface 
    import java.util.*; 
    import java.util.function.Predicate; 
      
    class Test 
    { 
        public static void main(String args[]) 
        { 
      
            // create a list of strings 
            List<String> names = 
                Arrays.asList("Geek","GeeksQuiz","g1","QA","Geek2"); 
      
            // declare the predicate type as string and use 
            // lambda expression to create object 
            Predicate<String> p = (s)->s.startsWith("G"); 
      
            // Iterate through the list 
            for (String st:names) 
            { 
                // call the test method 
                if (p.test(st)) 
                    System.out.println(st); 
            } 
        } 
    }
    

    输出:

    Geek
    GeeksQuiz
    Geek2
    

    总结

    1. functional interface只有一个抽象方法,但可以有多个默认方法或静态方法。
    2. @FunctionalInterface注解用于确保接口不能具有多个抽象方法。此注释的使用是可选的。
    3. java.util.function包内包含Java 8中许多内置的functional interface
  • 相关阅读:
    编译 安装 infobright
    MySQL忘记密码恢复密码的实现方法
    Intel 服务器 架构 NUMA
    Centos 卸载 java
    vs2010 无法将文件“obj**”复制到“bin**”
    linux安装eclipse PyDev
    infobright 编译安装
    [转贴]==开手排车的八个绝招==
    [摘]广义企业级PDM系统下的PPM(工艺规划管理)
    中国皇帝定下佛教戒律:僧人不准吃肉
  • 原文地址:https://www.cnblogs.com/noodlesmars/p/12704769.html
Copyright © 2011-2022 走看看