public static void main(String[] args) {
Runnable runnable = new Runnable() {
@Override
public void run() {
System.out.println(“我来来”);
}
};
Runnable runnable1 = () -> {
System.out.println("我也来来");
};
runnable.run();
runnable1.run();
List<Student> students = Lists.newArrayList();
Student student = new Student();
student.setAge(11);
students.add(student);
Student student1 = new Student();
student1.setAge(22);
students.add(student1);
Predicate<Student> predicate = (e)->e.getAge()>21;
students.stream().filter(predicate).forEach(System.out::println);
System.out.println("------------------");
// 相当于诸葛亮给了赵云一道锦囊,在必要的时候打开
execConsumer(student,(e)->{
System.out.println(e.getAge());
});
System.out.println("------------------");
// 相当于建筑师把图纸画好,给施工队执行,并返回质检报告
Random r = new Random();
List<Integer> integers = execSupplier(() -> r.nextInt(30), 10);
integers.stream().forEach(System.out::println);
System.out.println("------------------");
// 相当于大白兔厂,Function.apply 是厂的流水线 牛奶+白沙糖是入参 产出大白兔是出参 注:Function第一个参数是入参,第二个参数是出参类型
String resultStr = execFunction(" 123 ",e->e.trim());
System.out.println(resultStr);
String s = execFunction("123", e -> e.substring(0, 1));
System.out.println(s);
System.out.println("------------------");
// 相当于海瑞拿着大明律法 对付官场之人,对就是对,错就是错,管你老几,皇上犯错也的骂
Boolean aBoolean = execPredicate("", e -> e.isEmpty());
System.out.println(aBoolean);
}
private static Boolean execPredicate(String s, Predicate<String> o) {
return o.test(s);
}
private static String execFunction(String s, Function<String,String> function) {
String apply = function.apply(s);
return apply;
}
public static void execConsumer(Student m, Consumer<Student> consumer){
consumer.accept(m);
}
public static List<Integer> execSupplier(Supplier<Integer> supplier,Integer n){
ArrayList<Integer> ns = Lists.newArrayList();
for (int i = 0; i < n; i++) {
ns.add(supplier.get());
}
return ns;
}
static class Student{
private Integer age;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"age=" + age +
'}';
}
}