//看着很简洁,就是有点难理解
public class Iterator_test {
public static void main(String[] args) {
List<String> list=new ArrayList<>();
list.add("343");
list.add("343434");
//使用Consumer接口
//函数式编程,消费者
list.forEach((String s)-> {System.out.println(s);});
list.forEach(s->System.out.println(s));
list.forEach(System.out::println);
//使用Function接口
String s=Function_test("hello word",(s1)->s1+" wind");
String s2=Function_test("hello word",(s1)->{String a=s1+" hi";return a;});
System.out.println(s);
System.out.println(s2);
//测试supplier接口
List<Integer> mao=Supplier_test(5,()->(int)(Math.random()*10));
mao.forEach(System.out::println);
//测试下断言接口predicate
List<String> list1= Arrays.asList("hello","wored","english","china");
List<String> list2=predicte(list1,(ss)->ss.contains("o"));
list2.forEach(System.out::println);
}
public static String Function_test(String str, Function<String,String> f){
return f.apply(str);
}
public static List<Integer> Supplier_test(int n, Supplier<Integer> s){ List<Integer> list =new ArrayList<>(); for(int i=0;i<n;i++) list.add(s.get()); return list; }
public static List<String> predicte(List<String> list, Predicate<String> p){ List<String> re=new ArrayList<>(); for (String s:list) if(p.test(s)) re.add(s); return re; } }