1. 程式人生 > >建立一個TreeSet物件,並自其中新增一些員工物件(Employee),其姓名和工資分別為:張三 8000,李四 6000,王五 5600,馬六 7500,最後按照工資的多少進行降序輸出.(提示:讓

建立一個TreeSet物件,並自其中新增一些員工物件(Employee),其姓名和工資分別為:張三 8000,李四 6000,王五 5600,馬六 7500,最後按照工資的多少進行降序輸出.(提示:讓

import java.util.Set;
import java.util.TreeSet;

import java.util.Iterator;

public class Employee implements Comparable {
	private String name;
	private double salary;

	public  Employee(String name,double salary) {
		this.name=name;
		this.salary=salary;
	}
	public String GetName() {
		return name;
	}
	public double getsalary() {
		return salary;
	}
	public String toString() {
		return "名字" + name + ",價格" +  salary;
	}
	
	@Override
	public int compareTo(Object o) {
		// TODO Auto-generated method stub
		return (int) (((Employee)o).salary-this.salary);
	}
	public static void main(String[] args) {
		Set set = new TreeSet();
		set.add(new Employee("zhangsan",8000));
		set.add(new Employee("lisi",9000));
		set.add(new Employee("wangwu",6000));
		set.add(new Employee("maliu",7000));
		Iterator it = set.iterator();
		while(it.hasNext())
		{
			System.out.println(it.next());
		}
	}

}