1. 程式人生 > >【TreeSet:請按照姓名的長度排序:比較器排序】

【TreeSet:請按照姓名的長度排序:比較器排序】

ack return ret get 比較器 main [] ble class

package com.yjf.esupplier.common.test;

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

/**
 * @author shusheng
 * @description 比較器排序
 * @Email [email protected]
 * @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; } }

【TreeSet:請按照姓名的長度排序:比較器排序】