# Java8之函数式接口 **Repository Path**: fpfgitmy_admin/java8-functional-interface ## Basic Information - **Project Name**: Java8之函数式接口 - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2021-04-28 - **Last Updated**: 2021-04-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### Java8值函数式接口 + 如果一个接口中,只声明了一个抽象方法,则此接口就称为函数式接口 + 在接口上声明`@FunctionalInterface`注解,校验该接口是否函数式接口 + 示例: ``` @FunctionalInterface public interface MyInterface { void info(); // void info(); 只能有一个抽象方法,才被称之为函数式接口 } ``` #### java内置的4大核心函数式接口 ##### 消费型接口` Consumer void accetp(T t)` ``` @Test public void test1() { happyTime(500, money -> { System.out.println("消费了" + money); }); } public void happyTime(double money, Consumer con) { con.accept(money); } ``` ##### 供给型接口`Supplier T get()` #####  函数型接口`Function R apply(T t)` ##### 断定型接口`Predicate boolean test(T t)` ``` @Test public void test2() { List list = Arrays.asList("北京", "南京", "天津", "东京", "普京"); List filterString = filterString(list, s -> s.contains("京")); System.out.println("过滤后的字符串集合=" + filterString); } // 根据给定的规则,过滤集合中的字符串,此规则由Predicate的方法决定 public List filterString(List list, Predicate pre) { ArrayList filterList = new ArrayList<>(); for (String s : list) { if (pre.test(s)) { filterList.add(s); } } return filterList; } ```