1.消费性接口
@Test //Consummer 消费型接口 public void test1(){ a(1987.2,e-> System.out.println("赚了"+e+"元")); } public void a(Double money, Consumer d){ d.accept(money); }
2.供给型接口
@Test //Supllier 供给型接口,产生一个集合 public void test2(){ List<Integer> list=b(18,()->(int)(Math.random()*100)); for(Integer k:list){ System.out.println(k); } } public List<Integer> b(int num, Supplier<Integer> d){ List<Integer> list= new ArrayList<>(); for(int i=0;i<num;i++){ Integer n=d.get(); list.add(n); } return list; }
3.函数式接口
//Function 函数型接口 public void test(){ String str=c("wfdgbhh",x-> x.toUpperCase()); System.out.println(str); } public String c(String x, Function<String, String> d){ return d.apply(x); }
4.断言型接口
public void e(){ List<String> list= Arrays.asList("张三","李四","王二麻子"); List<String> list1 = Filter(list, e->e.equals("张三")); for(String k:list1){ System.out.println(k); } } public List<String> Filter(List<String> list, Predicate<String> p){ List<String> list1=new ArrayList<>(); for(String k:list){ if(p.test(k)){ list1.add(k); } } return list1; }