您现在的位置是:首页 > 文章详情

函数式接口

日期:2019-10-24点击:403

函数式接口

定义

  1. 函数式接口就是一个有且仅有一个抽象方法,但是可以有多个非抽象方法的接口;
  2. 函数式接口可以被隐式转换为Lambda表达式;

自定义函数式接口

 @FunctionalInterface interface GreetingService { void sayMessage(String message); } 

函数式接口种类

1.8 之前已有的

 java.lang.Runnable java.util.concurrent.Callable java.security.PrivilegedAction java.util.Comparator java.io.FileFilter java.nio.file.PathMatcher java.lang.reflect.InvocationHandler java.beans.PropertyChangeListener java.awt.event.ActionListener javax.swing.event.ChangeListener

1.8 新增的

 java.util.function
  • function中包含很多方法。例子如Predicate的使用。
 public static void eval(List<Integer> list, Predicate<Integer> predicate) { // 方法一 for(Integer n: list) { if(predicate.test(n)) { System.out.println(n + " "); } } //方法二 结合Stream 的过滤方法 System.out.println("-----------------------------------------"); list.stream().filter((name) -> (predicate.test(name))).forEach((name) ->{ System.out.println(name); }); //方法三 结合Stream 的过滤方法 ,在结合 方法引用 System.out.println("-----------------------------------------"); list.stream().filter((name) -> (predicate.test(name))).forEach(System.out::println); } public static void main(String[] args) { List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9); // 传递参数 n eval(list, n->true); // 传递参数 n eval(list, n-> n>3); //Predicate接口接受多个条件同时进行比较 List<String> lists = Arrays.asList("Great works are accomplished not by strength but by perseverance.","Keep its original intention and remain unchanged"); Predicate<String> starts = (name) -> name.startsWith("1"); Predicate<String> lengths = (name) -> name.length() == 4; lists.stream().filter(starts.and(lengths)).forEach(System.out::println); }
  • 方法一使用的是常用的for循环遍历,方法二和方法三结合了Stream Api的过滤方法,及方法引用。

    参考地址

参考地址
## github博客列表地址
github
欢迎关注公众号,查看更多内容 :
XG54_9_WXMH_5X_HB_H_7V

原文链接:https://yq.aliyun.com/articles/722648
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章