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;
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM