栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2.java8流的使用 stream的筛选与切片

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2.java8流的使用 stream的筛选与切片

1.代码
  1. 准备的bean类

public class Employee {
    private String name;
    private int age;
    private double salary;

    public Employee(String name, int age, double salary) {
        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 double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + ''' +
                ", age=" + age +
                ", salary=" + salary +
                '}';
    }
//    去重

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        return age == employee.age &&
                Double.compare(employee.salary, salary) == 0 &&
                name.equals(employee.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age, salary);
    }
}

其中equals与hashCode是为了去重
添加值

List employees=Arrays.asList(
            new Employee("张三",18,9999.99),
            new Employee("李四",58,5555.55),
            new Employee("王五",26,3333.33),
            new Employee("赵六",36,6666.66),
            new Employee("田七",12,8888.88),
            new Employee("田七",12,8888.88)
    );

stream要终止操作财执行,不然就不执行
过滤:这里是过滤,返回大于35的age值

//中间操作,不会执行
        Stream stream = employees.stream().filter((e) -> {
            System.out.println("Stream API 的中间操作");
            return e.getAge()>35;//返回age大于35的
        });
        //终止操作:一次性全部内容,即惰性求值
        stream.forEach(System.out::println);

limit(2)表示限制,限制流的数量为2,取前面两个,这个会执行两次

@Test
    public void test2(){
        employees.stream()
                .filter((e)->{
                    System.out.println("短路!");
                    return e.getSalary()>5000;
                })
                .limit(2)//执行两次
                .forEach(System.out::println);
    }

skip:表示跳过,前面的值,取后面的,这个会执行全部

//跳skip
    @Test
    public void test3(){
        employees.stream()
                .filter((e)->{
                    System.out.println("短路!");
                    return e.getSalary()>5000;
                })
                .skip(2)//执行了5次
                .forEach(System.out::println);
    }

distinct不同的,不能够一样

 @Test
    public void test4(){
        employees.stream()
                .filter((e)->{
                    System.out.println("去重");
                    return e.getSalary()>5000;
                })
                .distinct()
                .forEach(System.out::println);
    }
2.反思总结

limit 与skip是互补关系

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/356512.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号