Use of Comparator and Comparable

Keywords: Java

According to section 6, Chapter 10 of Chen Liangyu, core technology of MOOC Java, University of China
Arrangement
https://www.icourse163.org/course/ECNU-1002842004?tid=1206091267
• object implements the compatible interface (the object class needs to be modified)
– implement compareTo method
• > Return 1, = = return 0, < return - 1
– Arrays and Collections call this method automatically when they sort the object

• create a new Comparator (when the object class cannot be changed)
– implement compare method
• > Return 1, = = return 0, < return - 1
– Comparator comparator will be submitted as a parameter to the sort method of the tool class

The object classes written by oneself can be modified directly by using Comparable (such as the Person below); the jar files sent by others can not be modified by themselves by using Comparator (such as Person2Comparator).

import java.util.Arrays;

public class Person implements Comparable<Person> {
	String name;
	int age;

	public String getName() {
		return name;
	}

	public int getAge() {
		return age;
	}

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

	public int compareTo(Person another) {
		int i = 0;
		i = name.compareTo(another.name); // Use string comparison
		if (i == 0) {
			// If the name is the same, compare the age and return the result of comparing the age
			return age - another.age;
		} else {
			return i; // Different names, return the result of comparing names
		}
	}

	public static void main(String... a) {
		Person[] ps = new Person[3];
		ps[0] = new Person("Tom", 20);
		ps[1] = new Person("Mike", 18);
		ps[2] = new Person("Mike", 20);

		Arrays.sort(ps);//sort ascending order
		for (Person p : ps) {
			System.out.println(p.getName() + "," + p.getAge());
		}
	}
}

Person 2 assumes no visibility

public class Person2 {
	private String name;
    private int age;
	public String getName() {
		return name;
	}
	public int getAge() {
		return age;
	}

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

If you need to compare person 2, use Comparator

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

public class Person2Comparator  implements Comparator<Person2> {
	public int compare(Person2 one, Person2 another) {
		int i = 0;
		i = one.getName().compareTo(another.getName());
		if (i == 0) {
			// If the name is the same, compare the age and return the result of comparing the age
			return one.getAge() - another.getAge();
		} else {
			return i; // Different names, return the result of comparing names
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Person2[] ps = new Person2[3];
		ps[0] = new Person2("Tom", 20);
		ps[1] = new Person2("Mike", 18);
		ps[2] = new Person2("Mike", 20);

		Arrays.sort(ps, new Person2Comparator());
		for (Person2 p : ps) {
			System.out.println(p.getName() + "," + p.getAge());
		}
	}
}

Posted by bugcoder on Sat, 23 Nov 2019 08:24:42 -0800