Comparable 与 Comparator

Java Custom objects can not be compared directly, the need to achieve Comparable interface or Comparator interface.

1, Comparable Interface

This interface imposes achieve its objects of each class from a total ordering. This ordering is called natural ordering class, compareTo method of the class referred to as its natural comparison method.

package com.latiny.basic;

import java.util.Arrays;

public class ComparableTest {

    public static void main(String[] args) {

        int[] nums = {4, 3, 2, 1, 5, 6};
        Arrays.sort(nums);
        System.out.println(Arrays.toString(nums));
        
        String[] strs = {"Jack", "Rose", "Mia", "Latiny", "贝贝", "欢欢"};
        Arrays.sort(strs);
        System.out.println(Arrays.toString(strs));
        
        Bird[] cats = {new Bird("Jmy", 4), new Bird("Feign", 1), new Bird("Selina", 9)};
        Arrays.sort(cats);
        System.out.println(Arrays.toString(cats));
    }

}

class Bird implements Comparable<Bird> {
    String name;
    int age;
    
    public Bird() {
        super();
    }

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

    @Override
    public String toString() {
        return "[name:" + name + ", " + "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 int compareTo(Bird bird) {
        return this.age - bird.age;
    }
}

 

2、Comparator

Comparator interface is required to achieve a custom class, in accordance with the principles of OO, closed for modification, the extended open. Suitable class has been defined, the time can not modify it or do not want to go, it needs when it is compared by a combination of methods compare the new class that implements Comparator interface.

public class ComparableTest {

    public static void main(String[] args) {

        Bike[] bikes = {new Bike("Latiny", 20), new Bike("Luise", 30), new Bike("Jordan", 28)};
        Arrays.sort(bikes, new BikeComparator());
        System.out.println(Arrays.toString(bikes));
    }
}

class BikeComparator implements Comparator<Bike> {

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

class Bike {
    private String name;
    private int speed;
    
    public Bike(String name, int speed) {
        super();
        this.name = name;
        this.speed = speed;
    }
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getSpeed() {
        return speed;
    }
    public void setSpeed(int speed) {
        this.speed = speed;
    }
    @Override
    public String toString() {
        return "Bike [name=" + name + ", speed=" + speed + "]";
    }
}

 

Guess you like

Origin www.cnblogs.com/Latiny/p/10645461.html