函数式(Functional)接口
Posted lemonzhang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了函数式(Functional)接口相关的知识,希望对你有一定的参考价值。
public class LambdaTest2 { @Test public void test1(){ happyTime(500, new Consumer<Double>() { @Override public void accept(Double aDouble) { System.out.println("水,价格为:" + aDouble); } }); System.out.println("********************"); happyTime(400,money -> System.out.println("水,价格为:" + money)); } public void happyTime(double money, Consumer<Double> con){ con.accept(money); } @Test public void test2(){ List<String> list = Arrays.asList("北京","南京","天津","东京","西京","普京"); List<String> filterStrs = filterString(list, new Predicate<String>() { @Override public boolean test(String s) { return s.contains("京"); } }); System.out.println(filterStrs); List<String> filterStrs1 = filterString(list,s -> s.contains("京")); System.out.println(filterStrs1); } //根据给定的规则,过滤集合中的字符串。此规则由Predicate的方法决定 public List<String> filterString(List<String> list, Predicate<String> pre){ ArrayList<String> filterList = new ArrayList<>(); for(String s : list){ if(pre.test(s)){ filterList.add(s); } } return filterList; } }
以上是关于函数式(Functional)接口的主要内容,如果未能解决你的问题,请参考以下文章