Java8 常用FunctionInterface使用方法

package com.lgx.jdk8.part02;

import java.util.*;
import java.util.function.*;

/**
 * java.util.function包API自學
 */
public class Test06LearnBySelf {
    public static void main(String[] args) {
        /**Consumer:接收一個參數不返回值,functional method is accept(Object)**/
        List<String> list = Arrays.asList("hello", "world");
        list.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println("s = " + s.toUpperCase());
            }
        });
        list.forEach(item -> System.out.println(item.toUpperCase()));

        /**DoubleConsumer:接收一個Double參數不返回值,functional method is accept(double)**/
        DoubleConsumer dc = (x) -> System.out.println(x*x);
        dc.accept(3.1415);
        List<Double> list2 = Arrays.asList(1.11, 2.22);
        /*list2.forEach(new DoubleConsumer(){
            @Override
            public void accept(double value) {

            }
        });*/ //一直報錯???原因,forEach方法接受的參數就是Consumer,DoubleConsumer不是Consumer的子類
        list2.forEach(item -> System.out.println(item));

        /**BiConsumer:接收兩個參數不返回值,functional method is accept(Object, Object)**/
        Map<String, String> map = new HashMap<>();
        map.put("key1", "value1");
        map.put("key2", "value2");
        map.forEach(new BiConsumer<String, String>() {
            @Override
            public void accept(String s1, String s2) {
                System.out.println("key = " + s1 + ", value = " + s2);
            }
        });
        map.forEach((s1, s2) -> System.out.println("key = " + s1 + ", value = " + s2));

        System.out.println("================================================================");

        /**Function:接收一個參數返回一個值,functional method is apply(Object)**/
        Function<String, String> function = (s1) -> s1.toUpperCase();
        String result = function.apply("hello world");
        System.out.println("result = " + result);

        /**IntFunction:接收一個Int參數返回一個Int值,functional method is apply(int)**/
        IntFunction<Integer> intFunction = (int1) -> int1 * int1;
        int intResult = intFunction.apply(100);
        System.out.println("intResult = " + intResult);

        /**DoubleToIntFunction:接收一個double參數返回一個int值,functional method is applyAsInt(Object)**/
        DoubleToIntFunction doubleToIntFunction = (d1) -> (int)d1+2;
        int doubleToIntResult = doubleToIntFunction.applyAsInt(3.1415926);
        System.out.println("doubleToIntResult = " + doubleToIntResult);

        /**BiFunction:接收兩個參數返回一個值,functional method is apply(Object, Object)**/
        BiFunction<String, String, String> biFunction = (s1, s2) -> s1 + s2;
        String biResult = biFunction.apply("hello", "world");
        System.out.println("biResult = " + biResult);

        /**BinaryOperator(二元運算符):接收兩個參數返回一個值(參數和返回值類型是一致的),functional method is apply(Object, Object)**/
        BinaryOperator<Double> binaryOperator = (d1, d2) -> d1 + d2;
        double binaryResult = binaryOperator.apply(20.0, 30.0);
        System.out.println("binaryResult = " + binaryResult);
        double minResult = BinaryOperator.minBy(Double::compareTo).apply(20.0 , 30.0);
        System.out.println("minResult = " + minResult);
        double maxResult = BinaryOperator.maxBy(Double::compareTo).apply(20.0 , 30.0);
        System.out.println("maxResult = " + maxResult);

        System.out.println("================================================================");

        /**Predicate:接收一個參數返回一個boolean值,functional method is test(Object)**/
        Predicate<String> predicate = (a) -> a == null;
        boolean predicateResult = predicate.test(new String());
        System.out.println("predicateResult = " + predicateResult);

        /**DoublePredicate:接收一個參數返回一個boolean值,functional method is test(double)**/
        DoublePredicate doublePredicate = (d) -> d >= 10;
        boolean doublePredicateResult = doublePredicate.test(9.99999);
        System.out.println("doublePredicateResult = " + doublePredicateResult);

        /**BiPredicate:接收兩個參數返回一個boolean值,functional method is test(Object, Object)**/
        BiPredicate<Integer, Integer> biPredicate = (b1, b2) -> b1 > b2;
        boolean biPredicateResult = biPredicate.test(3, 2);
        System.out.println("biPredicateResult = " + biPredicateResult);

        System.out.println("================================================================");

        /**Supplier:不接受參數返回一個值,functional method is get(Object)**/
        Supplier<String> supplier = () -> "test";
        String supplierResult = supplier.get();
        System.out.println("supplierResult = " + supplierResult);

        /**DoubleSupplier:不接受參數返回一個Double值,functional method is getAsDouble()**/
        DoubleSupplier doubleSupplier = () -> 1;
        double doubleSupplierResult = doubleSupplier.getAsDouble();
        System.out.println("doubleSupplierResult = " + doubleSupplierResult);

        System.out.println("================================================================");



    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章