java1.8新特性之list多条件排序和JAVA Comparator接口排序

一、java1.8新特性之list多条件排序和Comparator接口排序


## 一、java1.8新特性之list多条件排序

public static void main(String[] args) {


    String orderId1 = "2321837281372913";
    String userId1 = "20180701001";
    String orderId2 = "2321837281372914";
    String userId2 = "20180701002";
    String orderId3 = "2321837281372912";
    String userId3 = "20180701003";
    String orderId5 = "2321837281372918";
    String userId5 = "20180701004";
    String orderId6 = "2321837281372918";
    String userId6 = "20180701009";
    String orderId4 = "2321837281372918";
    String userId4 = "20180701005";
    
    Order order = new Order();
    order.setUserId(userId1);
    order.setOrderId(orderId1);
    Order order1 = new Order();
    order1.setOrderId(orderId2);
    order1.setUserId(userId2);

    Order order2 = new Order();
    order2.setOrderId(orderId3);
    order2.setUserId(userId3);
    Order order3 = new Order();
    order3.setOrderId(orderId4);
    order3.setUserId(userId4);
    Order order4 = new Order();
    order4.setOrderId(orderId5);
    order4.setUserId(userId5);
    Order order5 = new Order();
    order5.setUserId(userId6);
    order5.setOrderId(orderId6);

    List<Order> orderList = new ArrayList<Order>();
    orderList.add(order);
    orderList.add(order1);
    orderList.add(order2);
    orderList.add(order3);
    orderList.add(order4);
    orderList.add(order5);

    System.out.println(orderList.toString());
    System.out.println("=========================");
    System.out.println("=======这样子就是以orderid降序,如果ordeid相同再以userid降序==================");
    orderList.sort(Comparator.comparing(Order::getOrderId).thenComparing(Order::getUserId).reversed());
    System.out.println(orderList.toString());

    System.out.println("=======这样子就是以orderid降序,如果ordeid相同再以userid升序==================");
    orderList.sort(Comparator.comparing(Order::getOrderId).reversed().thenComparing(Order::getUserId));
    System.out.println(orderList.toString());

    System.out.println("=======这样子就是以orderid升序,如果ordeid相同再以userid升序==================");
    orderList.sort(Comparator.comparing(Order::getOrderId).thenComparing(Order::getUserId));
    System.out.println(orderList.toString());

    System.out.println("=======这样子就是以orderid升序,如果ordeid相同再以userid降序==================");
          orderList.sort(Comparator.comparing(Order::getOrderId).reversed().thenComparing(Order::getUserId).reversed());
    System.out.println(orderList.toString());


}

二、JAVA Comparator接口排序

package com.test2;

/**
 * @author luokun
 * @date 2019/7/3 17:25
 */
public class Student {

    private int age;
    private String name;
    private String nametest;
    public int getAge() {
        return age;
    }

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

    public String getName() {
        return name;
    }

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

    public String getNametest() {
        return nametest;
    }

    public void setNametest(String nametest) {
        this.nametest = nametest;
    }

    @Override
    public String toString() {
        return "Student [name=" + name + ", nametest=" + nametest +",age=" + age + "]";
    }

}
import com.test2.Student;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Test2 {
    public static void main(String[] args) {
        List<Student> list = new ArrayList<Student>();

        //创建3个学生对象,年龄分别是20、19、21,并将他们依次放入List中
        Student s1 = new Student();
        s1.setAge(20);
        Student s2 = new Student();
        s2.setAge(19);
        Student s3 = new Student();
        s3.setAge(21);
        list.add(s1);
        list.add(s2);
        list.add(s3);
        System.out.println("排序前:"+list);

        Collections.sort(list, new Comparator<Student>(){
            /*
             * int compare(Student o1, Student o2) 返回一个基本类型的整型,
             * 返回负数表示:o1 小于o2,
             * 返回0 表示:o1和o2相等,
             * 返回正数表示:o1大于o2。
             */
            public int compare(Student o1, Student o2) {

                //按照学生的年龄进行升序排列
                if(o1.getAge() > o2.getAge()){
                    return 1;
                }
                if(o1.getAge() < o2.getAge()){
                    return -1;
                }
                return 0;
            }
        });
        System.out.println("排序后:"+list);
    }

}
  • 0
    点赞
  • 0
    评论
  • 2
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值