JDK8中Lambda表达式的使用

本文通过实例演示了Java 8中Lambda表达式的多种使用方式,包括数据过滤、映射、排序等操作,展示了Lambda简化代码的强大能力。

JDK8中Lambda表达式的使用,demo如下

TestLambda.java类

package cn.corki.test;



import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;

import org.junit.Test;

public class TestLambda {

    // 可以这样处理数据
    List<Employee> employees  = Arrays.asList(
                new Employee("张三",18,2555),
                new Employee("李四",28,4444),
                new Employee("王五",38,3333),
                new Employee("孙二",22,5555),
                new Employee("大一",21,6666),
                new Employee("小三",25,6665),
                new Employee("大佬",15,2334)
            );

    @Test
    public void test1()
    {
//      employees.forEach(System.out::println);
        // 过滤工资大于5000的
        employees.stream()
            .filter((e) -> e.getSalary() > 5000)
            .forEach(System.out::println);

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

        // 年龄小于20 的,只取最前面的2条
        employees.stream()
            .filter((e) -> e.getAge() <= 21)
            .limit(2)
            .forEach(System.out::println);

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

        // 把名字全部输出
        employees.stream()
            .map(Employee::getName)
            .forEach(System.out::println);
    }

    @Test
    public void test2()
    {
        // 1.无参无返回值 ()-> System.out.println("Hello,Lambda");
        Runnable r = ()-> System.out.println("Hello,Lambda");
        r.run();

        // 2.有一个参数无返回值 x随便写 (x)->System.out.println(x);
        Consumer<String> con = (x)->System.out.println(x);

        // 3.若只有1个参数,小括号可以不写
        Consumer<String> con1 = x->System.out.println(x);
        con.accept("我大尚硅谷威武!");

        // 4.有两个以上参数,有返回值,并且Lambda有多条语句
        Comparator<Integer> com = (x,y)->{
            System.out.println("函数体式接口");
            return Integer.compare(x,y);
        }; 

        // 5. 若Lambda体中只有1条语句,return和大括号都可以不写
        Comparator<Integer> com2 = (x,y)->Integer.compare(x, y);

        // 6. Lambda参数列表的数据类型可以省略不写
        Comparator<Integer> com3 = (Integer x,Integer y)->Integer.compare(x, y);
        System.out.println(com.compare(30, 6));
    }


    @Test
    public void test3(){
        // 函数式接口:接口中只有一个抽象方法的接口,用注解@FunctionalInterface修饰判断是否支持
        Integer num = operation(100, (x)->x*x);
        System.out.println(num);

        System.out.println(operation(200, (y)->y+200));
    }

    @FunctionalInterface
    public interface MyFun{
        public Integer getValue(Integer num);
    }
    /*
     * 对一个整数进行运输,此时,并没有指定加减乘除
     */
    public Integer operation(Integer num,MyFun mf){
        return mf.getValue(num);
    }

    @Test
    public void test4(){
        // 先按年龄比,如果年龄相同,按姓名比
        Collections.sort(employees, (e1,e2) -> {
            if(e1.getAge() == e2.getAge()){
                return e1.getName().compareTo(e2.getName());
            }else{
                return -Integer.compare(e1.getAge(), e2.getAge()); // 从大到小
//              return Integer.compare(e1.getAge(), e2.getAge()); // 从小到大
            }
        });
        for (Employee employee : employees) {
            System.out.println(employee);
        }
    }

    @Test
    public void test5(){
        // 声明一个带2个参数类型的泛型接口,R为返回值,计算Long型的值
        op(100L, 200L, (x,y)->x+y);
        op(100L, 200L, (x,y)->x*y);
    }

    public interface MyFunc2<T,R>{
        public R getValue(T t1,T t2);
    }

    public void op(Long l1,Long l2,MyFunc2<Long, Long> mf){
        System.out.println(mf.getValue(l1, l2));
    }
}

Employee.java类

package cn.corki.test;

public class Employee {
    private String name;
    private int age;
    private int salary;
    public Employee(String name, int age, int salary) {
        super();
        this.name = name;
        this.age = age;
        this.salary = salary;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public int getSalary() {
        return salary;
    }
    public void setSalary(int salary) {
        this.salary = salary;
    }
    @Override
    public String toString() {
        return "Employee [name=" + name + ", age=" + age + ", salary=" + salary + "]";
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值