package com.lovecoding.day24.function01;

import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class Test01 {


    //功能型
    @Test
    public void test06(){

        Convter<String,Character> convter1 = ( String s ) -> { return s.charAt(0);};
        //省略 （）类型
        Convter<String,Character> convter2 = ( s ) -> { return s.charAt(0);};
        //省略 （）类型  只有一个参数 小括号 也可以省略
        Convter<String,Character> convter3 =  s  -> { return s.charAt(0);};
        // {} 里面只有一行语句  {}省略 省略 return
        Convter<String,Character> convter4 =  s  ->  s.charAt(0);
    }

    //功能型
    @Test
    public void test05(){
        Convter<String,Character> convter = ( String s ) -> { return s.charAt(0);};

        Character a = convter.change("a");
        System.out.println(a);
    }

    @Test
    public void test04(){

        Function<Integer,String[]> function1 = (Integer e) -> {return new String[e];};

        String[] apply = function1.apply(10);

        System.out.println(apply);
    }

    @Test
    public void test03(){
        //判断
        Predicate<String> predicate1 =  (String e ) -> { return e.startsWith("a"); };

        boolean aaaa = predicate1.test("baaa");
        System.out.println(aaaa);

        Predicate<String> predicate = new Predicate<>() {
            @Override
            public boolean test(String s) {
                return s.startsWith("a");
            }
        };
    }

    @Test
    public void test02(){
        //返回值
        Supplier<Double> supplier = new Supplier<Double>() {
            @Override
            public Double get() {
                return Math.random();
            }
        };

        Supplier<Double> supplier1 = () -> { return Math.random(); };

        Double aDouble = supplier1.get();
        System.out.println(aDouble);
    }

    @Test
    public void test01(){
        //消费型
        Consumer<String> consumer1 = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };

        //lambda
        Consumer<String> consumer2 = new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };

        //lambda
        Consumer<String> consumer3 =  (String s)-> { System.out.println(s); };

        //输出
        consumer3.accept("hello");

        String[] arr = {"1","2"};

        List<String> strings = Arrays.asList(arr);
        //lambda
        strings.forEach( (e) -> System.out.println(e) );

        //之前
        strings.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });
    }
}
