Java8中Stream的排序,查找和匹配的用法

发布时间:2020-04-05 02:15:12 作者:孤魂1996
来源:网络 阅读:4919

Java8中Stream的排序,查找和匹配的用法

排序
  1. sorted( )---自然排序
  2. sorted(Comparator com)---定制排序

    public class Employee {
    
    private String name;
    private Integer age;
    private Double salary;
    private Status status;
    
    public Employee() {
        super();
    }
    
    public  Employee(Integer age){
        this.age = age;
    }
    
    public Employee(String name, Integer age, Double salary) {
        super();
        this.name = name;
        this.age = age;
        this.salary = salary;
    }
    
    public Employee(String name, Integer age, Double salary, Status status) {
        this.name = name;
        this.age = age;
        this.salary = salary;
        this.status = status;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public int getAge() {
        return age;
    }
    
    public void setAge(Integer age) {
        this.age = age;
    }
    
    public double getSalary() {
        return salary;
    }
    
    public void setSalary(Double salary) {
        this.salary = salary;
    }
    
    public Status getStatus() {
        return status;
    }
    
    public void setStatus(Status status) {
        this.status = status;
    }
    
    @Override
    public String toString() {
        return "Employee{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", salary=" + salary +
                ", status=" + status +
                '}';
    }
    
    @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 &&
                Objects.equals(name, employee.name);
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(name, age, salary);
    }
    
    public enum Status{
        FREE,
        BUSY,
        VOCATION;
    }
    }
    @Test
    public void test1(){
        List<String> list = Arrays.asList("ccc", "aaa", "bbb", "ddd", "eee");
    
        //自然排序
        list.stream()
            .sorted()
            .forEach(System.out::println);
    
        System.out.println("------------------------------");
    
        //定制排序
        employees.stream()
                 .sorted((e1, e2) -> {
                     if (e1.getAge() == e2.getAge()){
                         return e1.getName().compareTo(e2.getName());
                     }else{
                         return Integer.compare(e1.getAge(), e2.getAge());
                     }
                 }).forEach(System.out::println);
    }
    查找与匹配
  3. allMatch---检查是否匹配所有元素
  4. anyMatch---检查是否至少匹配一个元素
  5. noneMatch---检查是否没有匹配所有元素
  6. findFirst---返回第一个元素
  7. findAny---返回当前流中的任意元素
  8. count---返回流中元素的总个数
  9. max---返回流中最大值
  10. min---返回流中最小值

    List<Employee> employees = Arrays.asList(
            new Employee("张三", 18 ,9999.99, Employee.Status.FREE),
            new Employee("李四", 38, 5555.99, Employee.Status.BUSY),
            new Employee("王五", 50, 6666.66, Employee.Status.VOCATION),
            new Employee("赵六", 16, 3333.33, Employee.Status.FREE),
            new Employee("田七", 8, 7777.77, Employee.Status.BUSY)
    );
    @Test
    public void test2(){
        //allMatch---检查是否匹配所有元素
        boolean b1 = employees.stream()
                .allMatch((e) -> e.getStatus().equals(Employee.Status.BUSY));
        System.out.println(b1);
    
        //anyMatch---检查是否至少匹配一个元素
        boolean b2 = employees.stream()
                 .anyMatch((e) -> e.getStatus().equals(Employee.Status.BUSY));
        System.out.println(b2);
    
        //noneMatch---检查是否没有匹配所有元素
        boolean b3 = employees.stream()
                 .noneMatch((e) -> e.getStatus().equals(Employee.Status.BUSY));
        System.out.println(b3);
    
        //findFirst---返回第一个元素
        Optional<Employee> op = employees.stream()
                 .sorted((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()))
                 .findFirst();
        System.out.println(op.get());
    
        //findAny---返回当前流中的任意元素
        Optional<Employee> op2 = employees.stream()
                 .filter((e) -> e.getStatus().equals(Employee.Status.FREE))
                 .findAny();
        System.out.println(op2.get());
    }
    
    @Test
    public void test3(){
        //count---返回流中元素的总个数
        Long count = employees.stream()
                 .count();
        System.out.println(count);
    
        //max---返回流中最大值
        Optional<Employee> op1 = employees.stream()
                .max((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
        System.out.println(op1.get());
    
        //min---返回流中最小值
        Optional<Double> op2 = employees.stream()
                .map(Employee::getSalary)
                .min(Double::compare);
        System.out.println(op2.get());
    }
推荐阅读:
  1. 学习Java8的Stream
  2. Java8中Stream API的应用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

jdk8 java8 排序

上一篇:关于pkcs8 与 rsa 互转的一个坑

下一篇:Python遗传和进化算法框架之 Geatpy快速入门

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》