Lambda_08 函数表达式 Stream filter筛选|8月更文挑战

103 阅读2分钟

这是我参与8月更文挑战的第23天,活动详情查看:8月更文挑战

@[TOC](Lambda_08 函数表达式 Stream filter筛选)

特点:

  1. 过滤集合中元素
  2. 需要条件表达式
  3. 全集 --到-->子集

创建内部类:Student 使用匿名类实现筛选,使用匿名类主要是便于理解使用功能lambda表达时,容易想想其内容是怎么实现的。

public class Lambda09 {
    public static void main(String[] args) {
        List<Student> students = Arrays.asList(new Student("张三", 18),
                new Student("李四", 19),
                new Student("王五", 21),
                new Student("赵六", 22));

        System.out.println("使用匿名类实现stream filter 过滤方法");
        //使用stream filter筛选20岁以上的学生
        students.stream().filter(new Predicate<Student>() {
            @Override
            public boolean test(Student stu) {
                return stu.getAge()>20;
            }
        }).forEach(stu-> System.out.println(stu.toString()));

        System.out.println("使用Lambda表达式实现stream filter 过滤方法");
        students.stream().filter(t->{
            return t.getAge()>20;
        }).forEach(stu-> System.out.println(stu.toString()));

        System.out.println("使用Lambda表达式(简化版)实现stream filter 过滤方法");
        students.stream().filter(t->t.getAge()>20).forEach(stu-> System.out.println(stu.toString()));

    }

}

class Student {
    private String name;
    private Integer age;

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

lambda表达式在实际开发中其实很好使用,建议大家多实用lambda表达式,但是就是语法比较抽象,不容易懂。需要多使用才能掌握。

public class Lambda03 {
    public static void main(String[] args) {
        /**
         * 一个String入参
         */
        LambdaInterface31 lambdaInterface31 = (String str) -> System.out.println("welcome:" + str);
        lambdaInterface31.fun("Lambda.");

        /**
         * 两个String入参
         */
        LambdaInterface32 lambdaInterface32 = (String name, int age) -> System.out.println(name + "今年" + age + "岁。");
        lambdaInterface32.fun("小明", 3);

        /**
         * 两个int入参 带返回值
         */
        LambdaInterface33 lambdaInterface33 = (int x, int y) -> {
            int sum = x + y;
            return sum;
        };
        int sum = lambdaInterface33.sum(2, 3);
        System.out.println("2+3="+sum);

        /**
         * 无参数有返回值
         */
        LambdaInterface34 lambdaInterface34 = () ->{
            System.out.println("无参数有返回值");
            return "无参数有返回值";
        };
        lambdaInterface34.fun();
    }
}

interface LambdaInterface31 {
    public void fun(String str);
}

interface LambdaInterface32 {
    public void fun(String name, int age);
}

interface LambdaInterface33 {
    public int sum(int x, int y);
}

interface LambdaInterface34 {
    public String fun();
}