1. 程式人生 > >向TreeSet集合中加入5個員工的物件,根據員工的年齡(升序)進行排序,若年齡相同,再根據 工齡(降序)來排序,若工齡相同,根據薪水(降序)排序

向TreeSet集合中加入5個員工的物件,根據員工的年齡(升序)進行排序,若年齡相同,再根據 工齡(降序)來排序,若工齡相同,根據薪水(降序)排序

題目要求:

        向TreeSet集合中加入5個員工的物件,根據員工的年齡(升序)進行排序,若年齡相同,再根據
 工齡(降序)來排序,若工齡相同,根據薪水(降序)排序

分析一下? 我想還是不用了,題目已經給的很清晰了。就是一個員工類Employee   一個測試類  一個自己定義的MyComparator類實現了Comparator介面(如果你在Employee類中重寫了compareTo方法,那就省略這個類,雖然我不推薦,畢竟後期要求變化的時候不好改)

第一種:(推薦寫法)自定義的MyComparator類實現Comparator介面

01   MyComparator類

package cn.ketang.zuoye03;

import java.util.Comparator;

public class MyComparator implements Comparator<Employee> {

	@Override
	public int compare(Employee s1, Employee s2) {
	    
		int num = s1.getAge() - s2.getAge();
        //如果年齡相同,那我們比較工齡
		int num2 = num == 0 ? (s2.getServiceDays() - s1.getServiceDays()) : num;
        //如果工齡又相同,那我們比較薪水
		int num3 = num2 == 0 ? (int) (s2.getSalary() - s1.getSalary()) : num2;
        //如果薪水也相同,哎,你兩不會是同一個人吧,我們在比較下名字。
		int num4 = num3 == 0 ? s1.getName().compareTo(s2.getName()) : num3;

		return num4;
	}
}

02   Employee類

package cn.ketang.zuoye03;

public class Employee {// implements Comparable<Employee>
	private String name;
	private int age;
	private int ServiceDays;
	private int salary;

	public Employee() {

	}

	public Employee(String name, int age, int serviceDays, int salary) {
		super();
		this.name = name;
		this.age = age;
		ServiceDays = serviceDays;
		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 getServiceDays() {
		return ServiceDays;
	}

	public void setServiceDays(int serviceDays) {
		ServiceDays = serviceDays;
	}

	public double getSalary() {
		return salary;
	}

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

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "姓名是:" + name + "年齡是:" + age + "工齡是:" + ServiceDays + "薪水是:" + salary;
	}
}

03   測試類

package cn.ketang.zuoye03;

import java.util.TreeSet;

public class MyTreeSetTest {
	public static void main(String[] args) {
		TreeSet<Employee> ts = new TreeSet<Employee>(new MyComparator());
		Employee e = new Employee("wujulan", 18, 2, 5555);
		Employee e1 = new Employee("wujulan", 18, 3, 5521);
		Employee e2 = new Employee("wufa", 18, 3, 5521);// 和e1的區別是僅僅名字不一樣
		ts.add(new Employee("戰三", 18, 2, 5623));//不要問我為什麼用匿名物件寫
		ts.add(new Employee("lisi", 49, 1, 5000));
		ts.add(new Employee("wamg2", 19, 3, 6000));
		ts.add(e);
		ts.add(e);// 不能新增重複的,也填不進去,Mycomparator比較時返回值為0,就不新增,畢竟底層是一個紅黑樹,每新增一個元素都要從根節點往下比較
		ts.add(e1);
		ts.add(e2);

		for (Employee employee : ts) {
			System.out.println(employee);
		}
	}
}

第二個就是我不推薦的寫法,耦合性相較一而言較高

測試類還是測試類,然後在Employee類中實現一個Comparator介面,並重寫compareTo方法(反正你不寫,編譯器也會提示你寫的)

直接貼上去Employee類

package cn.ketang.zuoye03;

public class Employee implements Comparable<Employee> {
	private String name;
	private int age;
	private int ServiceDays;
	private int salary;

	public Employee() {

	}

	public Employee(String name, int age, int serviceDays, int salary) {
		super();
		this.name = name;
		this.age = age;
		ServiceDays = serviceDays;
		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 getServiceDays() {
		return ServiceDays;
	}

	public void setServiceDays(int serviceDays) {
		ServiceDays = serviceDays;
	}

	public double getSalary() {
		return salary;
	}

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

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "姓名是:" + name + "年齡是:" + age + "工齡是:" + ServiceDays + "薪水是:" + salary;
	}

	@Override
	public int compareTo(Employee s2) {

		int num = this.getAge() - s2.getAge();

		int num2 = num == 0 ? (s2.getServiceDays() - this.getServiceDays()) : num;

		int num3 = num2 == 0 ? (int) (s2.getSalary() - this.getSalary()) : num2;

		int num4 = num3 == 0 ? this.getName().compareTo(s2.getName()) : num3;

		return num4;
	}
}

結果來一波,排版沒調,請多擔待。

以上,多謝觀看。