java容器(数组和集合)内元素的排序问题

 

package com.janson.day20180827;

import java.util.*;

/**
 * java中容器内对象的排序可以通过Collections.sort()和Arrays.sort()两种方法实现
 * 但用以上两种方法有一个前提就是被排序的对象应该是支持排序的或可以比较的
 * 所以被排序的对象必须实现comparable接口或Comparator接口
 * comparable接口是排序接口,若一个类实现了该接口,就意味着该类支持排序
 * comparator接口是比较接口,我们如果要控制某个类的次序,而该类本身不支持排序(即没有实现comparable接口),那么就可以建立一个“该类的比较器”来进行排序
 */
public class TestCompare {
    public static void main(String[] args) {
        /*
        *测试Person类排序
        */
        Person p1 = new Person("张三","男",30);
        Person p2 = new Person("李四","女",25);
        Person p3 = new Person("王五","女",28);
        Person[] p = new Person[3];
        p[0] = p1;
        p[1] = p2;
        p[2] = p3;
        System.out.println("Person排序前:");
        for (Person person : p) {
            System.out.println(person.getName() + "," + person.getSex() + "," + person.getAge());
        }
        //Arrays.sort(p); //当被比较的对象没有实现排序接口或比较接口时,调用该方法就会失败

        /*
         * 测试Panda类排序
         */
        Panda[] panda = new Panda[]{new Panda("P1","man",10), new Panda("P2","female",15),new Panda("P3","man",6)};
        //Panda[] panda = {new Panda("P1","man",10), new Panda("P2","female",15),new Panda("P3","man",6)};
        System.out.println("Panda排序前:");
        for (Panda pa : panda) {
            System.out.println(pa.getName() + "," + pa.getSex() + "," + pa.getAge());
        }
        Arrays.sort(panda);
        System.out.println("Panda排序后:");
        for (Panda pa : panda) {
            System.out.println(pa.getName() + "," + pa.getSex() + "," + pa.getAge());
        }

        /*
         * 测试Dog类排序
         */
        List<Dog> dogList = new ArrayList<>();
        dogList.add(new Dog("D1","M",20));
        dogList.add(new Dog("D2","F",18));
        dogList.add(new Dog("D3","M",16));
        System.out.println("Dog排序前:");
        for (Dog d : dogList) {
            System.out.println(d.getName() + "," + d.getSex() + "," + d.getAge());
        }
        Collections.sort(dogList,new DogComparator()); //用我们写好的DogComparator对Dog进行排序
        System.out.println("Dog排序后:");
        for (Dog d : dogList) {
            System.out.println(d.getName() + "," + d.getSex() + "," + d.getAge());
        }
    }
}

/**
 * Person类没有实现排序接口或比较接口
 */

class Person {
    private String name;
    private String sex;
    private int age;

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

    String getName() {
        return name;
    }

    String getSex() {
        return sex;
    }

   int getAge() {
        return age;
    }
}

/**
 * Cat类实现了comparable排序接口
 */

class Panda implements Comparable<Panda>{
    private String name;
    private String sex;
    private int age;

    Panda(String name,String sex,int age){
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    String getName() {
        return name;
    }

    String getSex() {
        return sex;
    }

    int getAge() {
        return age;
    }

    @Override
    public int compareTo(Panda p) {
        return this.getAge() - p.getAge();
    }
}

/**
 * Dog类
 */

class Dog {
    private String name;
    private String sex;
    private int age;

    Dog(String name,String sex,int age){
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    String getName() {
        return name;
    }

    String getSex() {
        return sex;
    }

    int getAge() {
        return age;
    }

}

/**
 * 定义一个DogComparator类实现了comparator比较接口
 */
class DogComparator implements Comparator<Dog> {
    @Override
    public int compare(Dog o1, Dog o2) {
        return o1.getAge() - o2.getAge();
    }

 

posted @ 2018-09-10 15:07  那心之所向  阅读(1427)  评论(0编辑  收藏  举报