一、什么是函数式接口

1、只包含一个抽象方法的接口,称为函数式接口。

2、你可以通过Lambda表达式来创建该接口的对象。(若Lambda表达式抛出一个受检异常,那么该异常需要在目标接口的抽象方法上进行声明)。

3、我们可以在任意函数式接口上使用@FunctionalInterface注解,这样做可以检查它是否是一个函数式接口,同时javadoc也会包含一条声明,说明这个接口是一个函数式接口。

例如:

@FunctionalInterface
public interface MyFucntion2<T,R> { public R getValue(T t1,T t2);
}

以下对这个函数式接口进行测试:

 @Test
public void test2(){
Long op = op(200L, 300L, (x, y) -> x + y);
System.out.println(op);
} public Long op(Long l1,Long l2,MyFucntion2<Long,Long> myFucntion2){
return myFucntion2.getValue(l1,l2);
}

二、Java内置四大核心函数式接口

在学习lambda表达式的时候,我们知道,要使用lambda表达式,我们就要创建一个函数式接口,那每次用lambda表达式的时候岂不是很麻烦,这时候,java给我们内置了四大核心函数式接口:

1、 Consumer<T> : 消费型接口,void accept(T t);

代码示例:

@Test
public void test3(){
consumenMoney(2312,(m)-> {
if (m>1000){
System.out.println("买衣服花了 1000,还剩"+(m-1000));
m-=1000;
}
if (m>1000){
System.out.println("买鞋花了 1000,还剩"+(m-1000));
m-=1000;
}
System.out.println("最后还剩"+m);
});
}
public void consumenMoney(double money, Consumer<Double> comsumer){
//可以实现 Consumer接口怎么花钱
comsumer.accept(money);
}

2、 Supplier<T> : 供给型接口,T get();

   代码示例:

    @Test
public void test4(){
int id=0;
List<Apple> list = getAppleList(5,()->{
Apple apple1 = new Apple(id,"苹果1",new BigDecimal("3"),10);
return apple1;
});
list.forEach(System.out::println);
} //产生指定数量的苹果
public List<Apple> getAppleList(int num ,Supplier<Apple> supplier){
List<Apple> list = new ArrayList<>();
for (int i = 0; i <num ; i++) {
Apple apple = supplier.get();
list.add(apple);
}
return list;
}

3、Function<T, R> : 函数型接口,R apply(T t);

代码示例:

@Test
public void test5() {
String trimStr=strHandler("\t\t 你好,world! ",(String::trim);
System.out.println(trimStr); String sumString=strHandler("Helloworld!",(str)->str.substring(2, 4));
System.out.println(sumString);
}
//需求:用于处理字符串
public String strHandler(String str,Function<String,String> fun) {
return fun.apply(str);
}

4、 Predicate<T> : 断言型接口,boolean test(T t);

代码示例:

@Test
public void test5() {
List<String> list=Arrays.asList("Hello","world","hi","o","123");
List<String> filterStr = filterStr(list, (str)->str.length()>1);
filterStr.forEach(System.out::println);
} //需求:将满足条件的字符串,放入集合中
public List<String> filterStr(List<String> list, Predicate<String> pre){
List<String> list2=new ArrayList<>();
list.forEach((str)->{
if(pre.test(str)){
list2.add(str);
} }); return list2;
}

三、其他接口

我们熟悉的:

public interface Runnable { void run(); }
public interface Callable<V> { V call() throws Exception; }
public interface ActionListener { void actionPerformed(ActionEvent e); }
public interface Comparator<T> { int compare(T o1, T o2); boolean equals(Object obj); }

最新文章

  1. CCS5.2/CCS5.3/CCS5.4 仿真调试小技巧
  2. BestCoder#49
  3. nodemailer 发邮件
  4. Unity3D 动画回调方法
  5. lintcode:Find the Connected Component in the Undirected Graph 找出无向图汇总的相连要素
  6. win7(64位)+IE8+QC9.0
  7. 关于Lua 5.1中的debug.hook和coroutine
  8. 【原创】MapReduce计数器
  9. CAD2014以上版本不信任加载项解决方法
  10. 转载-SharePoint 2010 WebPart与Google地图系列 一:创建显示地图的WebPart
  11. iOS 弹出菜单UIMenuController的基本使用
  12. Linux 控制CPU使用率
  13. MVC模式浅谈
  14. Android为ViewPager添加切换动画——自己定义ViewPager
  15. 文件查找:locate、find
  16. es 中的 Iterator
  17. 如何解决 SQL Server 中的锁升级所致的阻塞问题
  18. PHP 对 memcache操作类
  19. 启动Oracle数据库时报错ORA-00119 &amp; ORA-00132
  20. CentOS 中安装 jdk

热门文章

  1. VS 2017 没有工具栏中没有Report Viewer的解决方案
  2. SQL常用短语小记-持续更新
  3. CF1217A Creating a Character
  4. MyBatis:配置解析
  5. python中的with用法
  6. 实用的VMware虚拟机使用技巧十一例
  7. Mycat简介及适用场景
  8. 吴裕雄--天生自然 JAVASCRIPT开发学习: DOM - 改变 HTML
  9. 批量导出数据库表(oracle)
  10. UML-设计对象时涉及的制品有哪些?