zoukankan      html  css  js  c++  java
  • java8:四大核心函数式接口(消费型、供给型、函数型、断言型)

    1、四大核心函数式接口

    (1)java8内置的四大核心函数式接口

    2、Consumer<T> : 消费型接口

    (1)源码

    @FunctionalInterface
    public interface Consumer<T> {
    
        /**
         * Performs this operation on the given argument.
         *
         * @param t the input argument
         */
        void accept(T t);
    
        /**
         * Returns a composed {@code Consumer} that performs, in sequence, this
         * operation followed by the {@code after} operation. If performing either
         * operation throws an exception, it is relayed to the caller of the
         * composed operation.  If performing this operation throws an exception,
         * the {@code after} operation will not be performed.
         *
         * @param after the operation to perform after this operation
         * @return a composed {@code Consumer} that performs in sequence this
         * operation followed by the {@code after} operation
         * @throws NullPointerException if {@code after} is null
         */
        default Consumer<T> andThen(Consumer<? super T> after) {
            Objects.requireNonNull(after);
            return (T t) -> { accept(t); after.accept(t); };
        }
    }

    有参数无返回值

    (2)接口的运用

        public void happy(double money, Consumer<Double> con){
            con.accept(money);
        }
        public void test(){
            happy(10000, (m) -> System.out.println(m));
        } 

    3、供给型接口

    (1)源码

    @FunctionalInterface
    public interface Supplier<T> {
    
        /**
         * Gets a result.
         *
         * @return a result
         */
        T get();
    }

    只有返回值,没有输入参数

    (2)接口的运用

           public void test(){
            List<Integer> numList = getNumList(10, () -> (int)(Math.random() * 100));
            
            for (Integer num : numList) {
                System.out.println(num);
            }
        }
        
        //需求:产生指定个数的整数,并放入集合中
        public List<Integer> getNumList(int num, Supplier<Integer> sup){
            List<Integer> list = new ArrayList<>();
            
            for (int i = 0; i < num; i++) {
                Integer n = sup.get();
                list.add(n);
            }
            
            return list;
        }

    4、函数型接口

    (1)源码

    @FunctionalInterface
    public interface Function<T, R> {
    
        /**
         * Applies this function to the given argument.
         *
         * @param t the function argument
         * @return the function result
         */
        R apply(T t);

    有输入有输出

    (2)运用

            public void test3(){
            String newStr = strHandler("------ ", (str) -> str.trim());
            System.out.println(newStr);
            
            String subStr = strHandler("-------", (str) -> str.substring(2, 5));
            System.out.println(subStr);
        }
        
        //需求:用于处理字符串
        public String strHandler(String str, Function<String, String> fun){
            return fun.apply(str);
        }    

    5、断言型接口

    (1)源码

    @FunctionalInterface
    public interface Predicate<T> {
    
        /**
         * Evaluates this predicate on the given argument.
         *
         * @param t the input argument
         * @return {@code true} if the input argument matches the predicate,
         * otherwise {@code false}
         */
        boolean test(T t);

    输入一个参数,得到一个Boolean类型的返回值

    (2)运用

        public void test(){
            List<String> list = Arrays.asList("Hello", "atguigu", "Lambda", "www", "ok");
            List<String> strList = filterStr(list, (s) -> s.length() > 3);
            
            for (String str : strList) {
                System.out.println(str);
            }
        }
        
        //需求:将满足条件的字符串,放入集合中
        public List<String> filterStr(List<String> list, Predicate<String> pre){
            List<String> strList = new ArrayList<>();
            
            for (String str : list) {
                if(pre.test(str)){
                    strList.add(str);
                }
            }
            
            return strList;
        }
    每个人都会有一段异常艰难的时光 。 生活的压力 , 工作的失意 , 学业的压力。 爱的惶惶不可终日。 挺过来的 ,人生就会豁然开朗。 挺不过来的 ,时间也会教你 ,怎么与它们握手言和 ,所以不必害怕的。 ——杨绛
  • 相关阅读:
    IntelliJ IDEA 常用快捷键汇总
    Git常用命令
    org.h2.jdbc.jdbcsqlexception: database is already closed (to disable automatic closing at vm shutdown, add ";db_close_on_exit=false" to the db url) [90121-197]
    AbstractErrorController
    JSR-303
    MultipartFile
    day4
    day3
    day 2
    day1
  • 原文地址:https://www.cnblogs.com/zhai1997/p/13679536.html
Copyright © 2011-2022 走看看