美文网首页
lambda表达式(1)

lambda表达式(1)

作者: YNZXGWZM | 来源:发表于2018-08-02 17:34 被阅读0次
package com.mc.day1.lambda;

import com.mc.Employee;
import org.junit.Test;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

public class Lambda1 {
    @Test
    public void test1() {
        happy(1000.0, (x) -> {
            System.out.println(x);
            System.out.println("消费");
        });
        List<Integer> numberList = getNumberList(10, () -> (int) (Math.random() * 100));
        for (Integer integer : numberList) {
            System.out.println(integer);
        }

        PrintStream out = System.out;
        Consumer<String> strCon = out::println;
        strCon.accept("哈哈哈");
    }

    /*消费型接口*/
    public void happy(double money, Consumer<Double> con) {
        con.accept(money);
    }

    public List<Integer> getNumberList(int num, Supplier<Integer> supplier) {
        List resultList = new ArrayList();
        for (int i = 0; i < num; i++) {
            Integer result = supplier.get();
            resultList.add(result);
        }
        return resultList;

    }

    List<String> empList = Arrays.asList("aa", "bb", "cc", "dd");
    List<Employee> emps = Arrays.asList(
            new Employee(101, "张三", 18, 9999.99),
            new Employee(102, "李四", 59, 6666.66),
            new Employee(103, "王五", 28, 3333.33),
            new Employee(104, "赵六", 28, 7777.77),
            new Employee(105, "田七", 38, 5555.55)
    );

    @Test
    public void Test2() {
//        empList.stream().map(x -> x.toUpperCase()).forEach(System.out::println);
//        empList.stream().map(x -> x.toUpperCase()).forEach(System.out::println);

        emps.stream().map(new Function<Employee, Integer>() {
            @Override
            public Integer apply(Employee s) {
                return s.getAge();
            }
        }).forEach(System.out::println);

    }

    //需求:对于两个 Long 型数据进行处理
    public void op(Long l1, Long l2, MyFunction2<Long, Long> mf){
        System.out.println(mf.getValue(l1, l2));
    }
}

相关文章

网友评论

      本文标题:lambda表达式(1)

      本文链接:https://www.haomeiwen.com/subject/siysvftx.html