Java对象按照某个字段值进行排序

1 实现Comparable接口

对应的代码:

package com.wei.test;

import java.util.Arrays;

/**
 * @author www
 * @date 2021/11/2 20:52
 * @description: TODO
 */
public class Cat1 implements Comparable {
    private String name;
    private int age;

    public Cat1() {

    }

    public Cat1(String name, int age) {
        this.name = name;
        this.age = age;
    }

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

    @Override
    public int compareTo(Cat1 another) {
        return this.age - another.age;
    }

    public static void main(String[] args) {
        Cat1[] cats = new Cat1[]{new Cat1("A", 18), new Cat1("B", 15), new Cat1("C", 33)};
        Arrays.sort(cats);
        System.out.println(Arrays.toString(cats));
    }
}

2 实现Comparator接口

对应的代码:

package com.wei.test;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author www
 * @date 2021/11/3 11:16
 * @description: TODO
 */
public class Cat4 {
    private String name;
    private Integer age;

    public Cat4() {
    }

    public Cat4(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;
    }

    public static class Cat4Compartor implements Comparator {

        @Override
        public int compare(Cat4 o1, Cat4 o2) {
            return o1.getAge() - o2.getAge();
        }
    }

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

    public static void main(String[] args) {
        Cat4[] cats = new Cat4[]{new Cat4("A", 18), new Cat4("B", 15), new Cat4("C", 33)};
        System.out.println(Arrays.toString(cats));
        // 排序后
        Arrays.sort(cats, new Cat4Compartor());
        System.out.println(Arrays.toString(cats));
    }
}



3 利用stream流

对应的代码

package com.wei.test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author www
 * @date 2021/11/2 20:52
 * @description: 按照类中的age进行排序
 */
public class Cat {
    private String name;
    private int age;

    public Cat() {

    }

    public Cat(String name, int age) {
        this.name = name;
        this.age = age;
    }

    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;
    }

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


    public static void main(String[] args) {
        Cat cat1 = new Cat("A", 18);
        Cat cat2 = new Cat("B", 15);
        Cat cat3 = new Cat("C", 33);
        List list = Arrays.asList(cat1, cat2, cat3);
        List sortedList = list.stream().sorted(Comparator.comparing(Cat::getAge)).collect(Collectors.toList());
        System.out.println(sortedList);
    }
}

4 直接利用sort方法

对应的代码

package com.wei.test;

import java.util.*;

/**
 * @author www
 * @date 2021/11/3 10:53
 * @description: TODO
 */
public class Cat3 {
    private String name;
    private Integer age;

    public Cat3() {
    }

    public Cat3(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 "Cat3{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public static void main(String[] args) {
        Cat3 cat1 = new Cat3("A", 18);
        Cat3 cat2 = new Cat3("B", 15);
        Cat3 cat3 = new Cat3("C", 33);
        List list = Arrays.asList(cat1, cat2, cat3);
        list.sort((p1, p2) -> p1.getAge().compareTo(p2.getAge()));
        System.out.println(list);
    }
}

你可能感兴趣的:(Java,杂记,java,后端)