【名师课堂】Java高级开发

【名师课堂】Java高级开发

Java核心第三部
156课时 |
31736人已学 |
(17 评论)

加入学习
加入学习

Lamdba语法实际上简化为了方法引用,但其核心在于函数式编程。函数式接口有四种:

功能型函数式接口(输入一个数据,在数据处理后再输出):

如果现在确定操作的数据是int,则可以使用IntFunction;

public interface Function<T,R> {public R apply(T t);};

For example:

public class Test {

    public static void main(String args[]) {
        Function<Integer,String> iu=String::valueOf;
        System.out.println(iu.apply(100));
        
    }
}

供给型函数接口:

Interface Supplier<T> {public T get();};

For example:

public class Test {

    public static void main(String args[]) {
        Supplier<String> cons="hello"::toUpperCase;
        System.out.println(cons.get());
        
    }
}

 

消费型函数

pulic interface Consumer<T>{public void accept(T t);};

For example:

public class Test {

    public static void main(String args[]) {
        Consumer<String> cons=System.out::println;
        cons.accept("Hello World!");
        
    }
}

 

断言型函数接口

public interface Predicate<T>{boolean test(T t);};

For example:

public class Test {

    public static void main(String args[]) {
        Predicate<String> pre = "##hello"::startsWith;
        System.out.println(pre.test("##"));
        
    }
}

 

 

[展开全文]
sky_fengbiubiu · 2017-06-27 · 内建函数式接口 0

内建函数式接口

Lamdba简化了方法引用,Lamdb核心在于函数接口,接口的核心是只有一个方法。函数式编程里面只有四种接口。(java.until.function)

功能性函数式接口:public interface Function<T,R>{public R apply(T t);};

供给型函数接口:Interface Supplier<T> {public T get ();};

消费型函数式接口:pulic interface Consumer<T> {public void accept(T t);}

段言型函数式接口:public interface Predicate<T>{boolean test(T t);}

使用功能性:你输入一个数据,然后进行输出,实际上所有的函数与式函数都有扩展

Consumer<String> cons=System.out::println;

cons.accept("Hello");

Supplier<String>sup="hello"::toUpperCase;

System.out.println(sup.get());

Predicate<String> pre="##hello"::startswith;

System.out.println(pre.test("##"));

  

[展开全文]
yang2bing1105 · 2017-06-12 · 内建函数式接口 0

授课教师

阿里云开发者社区全面升级
一站式体验,助力云上开发!
进入新社区

相关课程

查看更多 >

本课程相关云产品