一、Lambda表达式
() -> System.out.println("Hello Lambda!")
new Thread(new Runnable() { @Override public void run() { System.out.println("Hello Lambda!"); } }).start();
二、方法引用
Listlist = Arrays.asList("Java", "C++", "Python", "JavaScript"); list.forEach(System.out::println);
上述代码就使用了方法引用,System.out::println等价于item -> System.out.println(item)。
三、函数式接口
@FunctionalInterface public interface MyInterface { void doSomething(); }
MyInterface myInterface = () -> System.out.println("Do something"); myInterface.doSomething(); // 输出:Do something
四、流
Listwords = Arrays.asList("Java", "C++", "Python", "JavaScript"); long count = words.stream().filter(w -> w.length() > 2).count(); System.out.println(count); // 输出:4