【TreeSet:请按照姓名的长度排序:比较器排序】

package com.yjf.esupplier.common.test;

import java.util.Comparator;
import java.util.TreeSet;

/**
 * @author shusheng
 * @description 比较器排序
 * @Email shusheng@yiji.com
 * @date 2018/12/17 10:36
 */
public class TreeSetDemo1 {

    public static void main(String[] args) {

        // 创建集合对象
        TreeSet<Student1> ts = new TreeSet<Student1>(new MyComparable());

        // 创建元素
        Student1 s1 = new Student1("linqingxia", 27);
        Student1 s2 = new Student1("zhangguorong", 29);
        Student1 s3 = new Student1("wanglihong", 23);
        Student1 s4 = new Student1("linqingxia", 27);
        Student1 s5 = new Student1("liushishi", 22);
        Student1 s6 = new Student1("wuqilong", 40);
        Student1 s7 = new Student1("fengqingy", 22);
        Student1 s8 = new Student1("linqingxia", 29);
        // 添加元素
        ts.add(s1);
        ts.add(s2);
        ts.add(s3);
        ts.add(s4);
        ts.add(s5);
        ts.add(s6);
        ts.add(s7);
        ts.add(s8);

        // 遍历
        for (Student1 s : ts) {
            System.out.println(s.getName() + "---" + s.getAge());
        }

    }

}

class MyComparable implements Comparator<Student1> {

    @Override
    public int compare(Student1 s1, Student1 s2) {

        int num1 = s1.getName().length() - s2.getName().length();
        int num2 = num1 == 0 ? s1.getName().compareTo(s2.getName()) : num1;
        int num3 = num2 == 0 ? s1.getAge() - s2.getAge() : num2;

        return num3;

    }
}

class Student1 {

    private String name;
    private int age;

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

 

posted @ 2019-01-31 10:44  书丶生  阅读(420)  评论(0编辑  收藏  举报