JAVA-8 learn 03-04

0、lambda 定义

(parameters)-> expression 

  (parameters) -> { statements; }

1、 predicate boolean test(T t)

2、Consumer accept(T t)

3、Functioin  R apply(T, t)

4、Supplier  T get()

5、code

package com.tianmeng.java8.day04;

import com.tianmeng.java8.day03.Apple;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.*;

/**
 * @program: java8
 * @author: TIANMENG
 * @date: 2019-11-15 21:42
 **/
public class LambdaUsage {

    private static List filter(List source, Predicate prediccate){
        List result = new ArrayList<>();
        for (Apple apple :source){
            if(prediccate.test(apple)){
                result.add(apple);
            }
        }
        return result;
    }

    private static List filterByWeight(List source, LongPredicate prediccate){
        List result = new ArrayList<>();
        for (Apple apple :source){
            if(prediccate.test(apple.getWeight())){
                result.add(apple);
            }
        }
        return result;
    }

    private static List filterByTwo(List source, BiPredicate prediccate){
        List result = new ArrayList<>();
        for (Apple apple :source){
            if(prediccate.test(apple.getColor(), apple.getWeight())){
                result.add(apple);
            }
        }
        return result;
    }

    public static void simpleTestConsumer(List source, Consumer consumer){
        for (Apple apple : source){
            consumer.accept(apple);
        }
    }

    public static void simpleTestBiConsumer(String c, List source, BiConsumer consumer){
        for (Apple apple : source){
            consumer.accept(apple, c);
        }
    }

    private static String testFunction(Apple apple, Function function){
        return function.apply(apple);
    }

    private static Apple testBiFunction(String color, Long weight, BiFunction function){
        return function.apply(color, weight);
    }

    private static Apple createApple(Supplier supplier){
        return supplier.get();
    }

    public static void main(String[] args) {

        List list = Arrays.asList(new Apple("green", 120), new Apple("red",150));
        List greenList = filter(list, apple -> apple.getColor().equals("green"));
        System.out.println(greenList);
        List weightApple = filterByWeight(list, (w) -> w > 100);
        System.out.println(weightApple);
        List result = filterByTwo(list, (s, w) -> s.equals("green") && w > 100);
        System.out.println(result);
        System.out.println("==================== ");
        simpleTestConsumer(list, apple -> System.out.println(apple));
        simpleTestBiConsumer("hi  ", list, ((apple, s) -> System.out.println(apple + " " + s)));
        System.out.println("==================== ");
        String red = testFunction(new Apple("red", 100), apple -> apple.toString());
        System.out.println(red);

        IntFunction f = i -> i * 1000.0d;
        double result4 = f.apply(10);
        System.out.println("==================== ");
        System.out.println(result4);

        Apple result5 = testBiFunction("blue", 120L, (s, w) -> new Apple(s, w));
        System.out.println("==================== ");
        System.out.println(result5);

        Supplier s = String::new;   // method inference
        System.out.println(s.get().getClass());

        Apple result6 = createApple(() -> new Apple("black", 120));
        System.out.println(result6);


    }

}

 

你可能感兴趣的:(java-8)