@Test public void test1(){ Runnable runnable1 = new Runnable() { @Override public void run() { System.out.println("I like to drink juice"); } }; runnable1.run(); //使用Lambda Runnable runnable2 = () -> System.out.println("I like to eat bread"); runnable2.run(); } //使用java内置的函数式接口举例 //Predicate : 过滤 @Test public void test2(){ List<String> list = Arrays.asList("北京", "南京", "西安", "东京", "普京"); /*List<String> stringList = filterString(list, new Predicate<String>() { @Override public boolean test(String s) { return s.contains("京"); } }); System.out.println(stringList);*/ List<String> stringList =filterString(list,s -> s.contains("京")); System.out.println(stringList); } public List<String> filterString(List<String> list, Predicate<String> predicate){ ArrayList<String> stringArrayList = new ArrayList<>(); for (String s : list) { if (predicate.test(s)){ stringArrayList.add(s); } } return stringArrayList; }