Java8四大核心接口示例
public static void main(String[] args) { TestLambda testLambda = new TestLambda(); //消费型,消费 一个参数,无返回值 testLambda.consumer(1000,(money)->{ System.out.printf("消费%f元",money); }); //供给型,产生随机数 无参数,有返回值 System.out.println(testLambda.getNumList(5,()->{ return Math.random()*100; })); //函数型,剔除空格 一个参数,又返回值 System.out.println(testLambda.strHandler("\t\t\thello wolrd", (s) -> { return s.trim(); })); //断言型,处理字符串 有参数,有返回值(boolean类型) Predicate predicate = (str)->{ String s = (String) str; if (s.contains("china")){ return true; } return false; }; List list = new ArrayList(); list.add("i love china");list.add("i love chin1a"); list.add("are you speaking chinese ");list.add("china chaina"); System.out.println(testLambda.filterStr(list,predicate)); } public void consumer(double money,Consumer<Double> consumer){ consumer.accept(money); } public List<Double> getNumList(int num,Supplier<Double> supplier){ List<Double> list = new ArrayList<>(); for (int i = 0; i < num; i++) { Double n = supplier.get(); list.add(n); } return list; } public String strHandler(String str,Function<String ,String > function){ return function.apply(str); } public List<String > filterStr(List<String > list, Predicate<String> predicate){ List list1 = new ArrayList(); for (String string : list) { if (predicate.test(string)){ list1.add(string); } } return list1; }