@Testpublicvoidtest2(){List<Integer> numberList =getNumberList(10,()->(int)(Math.random()*100));for(Integer num : numberList){System.out.println(num);}}//需求:产生一些整数,并放在集合中publicList<Integer>getNumberList(int num,Supplier<Integer> sup){List<Integer> list =newArrayList<>();for(int i =0; i < num ; i++){list.add(sup.get());}return list;}
3、函数型接口
@Testpublicvoidtest3(){String result =Strandler("\t\thello world,i like china!", str -> str.trim());System.out.println(result);}//需求:用于处理字符串publicStringStrandler(String str,Function<String,String> fun){return fun.apply(str);}
4、断言形接口
@Testpublicvoidtest4(){List<String> employees =Arrays.asList("hello","houchen","shuchenxi");List<String> list =filterStr(employees, str -> str.length()>3);for(String s : list){System.out.println(s);}}//需求:将满足条件的字符串放入集合中publicList<String>filterStr(List<String> list,Predicate<String> pre){List<String> stringList =newArrayList<>();for(String str : list){if(pre.test(str)){stringList.add(str);}}return stringList;}