1. 程式人生 > >十三、實現Comparable接口和new Comparator<T>(){ }排序的實現過程

十三、實現Comparable接口和new Comparator<T>(){ }排序的實現過程

pan @override -s tps 排列 brush test ray abc

參考:https://www.cnblogs.com/igoodful/p/9517784.html


Collections有兩種比較規則方式,第一種是使用自身的比較規則:

該類必須實現Comparable接口並重寫comparTo方法。

this可以想象為1,傳入對象o想象為2,返回1-2即按升序排序。返回2-1即按降序排序。

1、首先編寫一個實現Comparable接口的實體類

 1 package com.abc;
 2 //Comparable接口後面一定要加上需要比較的數據類型
 3 public class Person implements Comparable<Person>
{ 4 5 private String name; 6 private int age; 7 private int salary; 8 9 public Person() { 10 } 11 12 public Person(String name, int age, int salary) { 13 this.name = name; 14 this.age = age; 15 this.salary = salary; 16 } 17 18
public String getName() { 19 return name; 20 } 21 22 public void setName(String name) { 23 this.name = name; 24 } 25 26 public int getAge() { 27 return age; 28 } 29 30 public void setAge(int age) { 31 this.age = age; 32 }
33 34 public int getSalary() { 35 return salary; 36 } 37 38 public void setSalary(int salary) { 39 this.salary = salary; 40 } 41 42 @Override 43 public String toString() { 44 return "Person{" + 45 "name=‘" + name + ‘\‘‘ + 46 ", age=" + age + 47 ", salary=" + salary + 48 ‘}‘; 49 } 50 51 //自身定義年齡升序 52 @Override 53 public int compareTo(Person o) { 54 return this.age-o.age; 55 } 56 }

2、編寫測試代碼

package com.abc;

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

public class PersonTest {

    public static void main(String[] args){
        List<Person> people = new ArrayList<>();
        people.add(new Person("AAA",20,100));
        people.add(new Person("BBB",18,109));
        people.add(new Person("CCC",30,58));

        System.out.println(people);
        Collections.sort(people);
        System.out.println(people);
    }
}

3、運行結果

[Person{name=‘AAA‘, age=20, salary=100}, Person{name=‘BBB‘, age=18, salary=109}, Person{name=‘CCC‘, age=30, salary=58}]
//完成了年齡的升序排列 [Person{name=‘BBB‘, age=18, salary=109}, Person{name=‘AAA‘, age=20, salary=100}, Person{name=‘CCC‘, age=30, salary=58}]


第二個參數為比較器,可以使用它來定義針對集合排序時的比較元素大小的規則。

使用這種方式時,sort方法不要求集合元素必須實現Comparable接口了,因為不會使用元素自身的比較規則

1、編寫一個普通的實體類,不需要實現任何接口

package com.abcd;

public class Person{

    private String name;
    private int age;
    private int salary;

    public Person() {
    }

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

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

    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name=‘" + name + ‘\‘‘ +
                ", age=" + age +
                ", salary=" + salary +
                ‘}‘;
    }

}

2、編寫測試代碼

package com.abcd;

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

public class PersonTest {
    public static void main(String[] args){
        List<Person> people = new ArrayList<>();
        people.add(new Person("AAA",20,100));
        people.add(new Person("BBB",18,109));
        people.add(new Person("CCC",30,58));

        System.out.println(people);
     //排序規則 salary降序 Collections.sort(people, new Comparator<Person>() { @Override public int compare(Person o1, Person o2) { return o2.getSalary()- o1.getSalary(); } }); System.out.println(people); } }

3、運行結果

[Person{name=‘AAA‘, age=20, salary=100}, Person{name=‘BBB‘, age=18, salary=109}, Person{name=‘CCC‘, age=30, salary=58}]
[Person{name=‘BBB‘, age=18, salary=109}, Person{name=‘AAA‘, age=20, salary=100}, Person{name=‘CCC‘, age=30, salary=58}]

  

十三、實現Comparable接口和new Comparator<T>(){ }排序的實現過程