创建一个TreeSet对象,并自其中添加一些员工对象(Employee),其姓名和工资分别为:张三 8000,
答案:1 悬赏:30 手机版
解决时间 2021-03-25 21:12
- 提问者网友:抽煙菂渘情少年
- 2021-03-25 08:56
创建一个TreeSet对象,并自其中添加一些员工对象(Employee),其姓名和工资分别为:张三 8000,
最佳答案
- 五星知识达人网友:不如潦草
- 2021-03-25 10:06
package com.test.demo;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class Employee implements Comparable {
private String name;
private int price;
public Employee(String name, int price){
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
return this.price - ((Employee)o).price;
}
public String toString(){
return "名字" + name + ",价格" + price;
}
public static void main(String[] args){
Set set = new TreeSet();
set.add(new Employee("张三",8000));
set.add(new Employee("李四",6000));
set.add(new Employee("王五",5600));
set.add(new Employee("马六",7500));
Iterator it = set.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}追问是“进行降序输出”
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
public class Employee implements Comparable {
private String name;
private int price;
public Employee(String name, int price){
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public int compareTo(Object o) {
// TODO Auto-generated method stub
return this.price - ((Employee)o).price;
}
public String toString(){
return "名字" + name + ",价格" + price;
}
public static void main(String[] args){
Set set = new TreeSet();
set.add(new Employee("张三",8000));
set.add(new Employee("李四",6000));
set.add(new Employee("王五",5600));
set.add(new Employee("马六",7500));
Iterator it = set.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}追问是“进行降序输出”
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯