真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

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

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

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

    創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),張店企業(yè)網(wǎng)站建設(shè),張店品牌網(wǎng)站建設(shè),網(wǎng)站定制,張店網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,張店網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實用型網(wǎng)站。

    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 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---返回當(dāng)前流中的任意元素
  8. count---返回流中元素的總個數(shù)
  9. max---返回流中最大值
  10. min---返回流中最小值

    List 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 op = employees.stream()
                 .sorted((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()))
                 .findFirst();
        System.out.println(op.get());
    
        //findAny---返回當(dāng)前流中的任意元素
        Optional op2 = employees.stream()
                 .filter((e) -> e.getStatus().equals(Employee.Status.FREE))
                 .findAny();
        System.out.println(op2.get());
    }
    
    @Test
    public void test3(){
        //count---返回流中元素的總個數(shù)
        Long count = employees.stream()
                 .count();
        System.out.println(count);
    
        //max---返回流中最大值
        Optional op1 = employees.stream()
                .max((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
        System.out.println(op1.get());
    
        //min---返回流中最小值
        Optional op2 = employees.stream()
                .map(Employee::getSalary)
                .min(Double::compare);
        System.out.println(op2.get());
    }

文章標(biāo)題:Java8中Stream的排序,查找和匹配的用法
轉(zhuǎn)載來于:http://weahome.cn/article/ihsded.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部