自定义一个雇员类,包含雇员ID、雇员姓名、雇员年龄和雇员地址四个字段,现要求完成如下程序:

自定义一个雇员类,包含雇员ID、雇员姓名、雇员年龄和雇员地址四个字段,现要求完成如下程序:
A、定义10个雇员,并且添加到HashSet集合类中,然后将所有雇员的资料打印出来。
B、按雇员年龄降序排列。
c、将上面的雇员的身份证号作为键对象,雇员类对象作为值对象,添加5个雇员到HashMap集合类对象中,并打印出每个雇员的身份证号、ID、姓名、年龄和地址。

求代码

1、实体类

package com.test.bean;

public class Employee {
public Employee() {
super();
}
private String employee_ID;//雇员ID
private String employee_name;//雇员姓名
private String employee_age;//雇员年龄
private String employee_address;//雇员地址
public String getEmployee_ID() {
return employee_ID;
}
public void setEmployee_ID(String employee_ID) {
this.employee_ID = employee_ID;
}
public String getEmployee_name() {
return employee_name;
}
public void setEmployee_name(String employee_name) {
this.employee_name = employee_name;
}
public String getEmployee_age() {
return employee_age;
}
public void setEmployee_age(String employee_age) {
this.employee_age = employee_age;
}
public String getEmployee_address() {
return employee_address;
}
public void setEmployee_address(String employee_address) {
this.employee_address = employee_address;
}
public Employee(String employee_ID, String employee_name,
String employee_age, String employee_address) {
super();
this.employee_ID = employee_ID;
this.employee_name = employee_name;
this.employee_age = employee_age;
this.employee_address = employee_address;
}



}

2、自定义排序器

package com.test.test;

import java.util.Comparator;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import com.test.bean.Employee;

public class ComparatorTask implements Comparator{
    public ComparatorTask() {
        super();
    }
    
    //自定义比较器
    public int compare(Object arg0, Object arg1) {
    Employee employeeList0=(Employee)arg0;
    Employee employeeList1=(Employee)arg1;

    //比较申请时间
    int flag=employeeList1.getEmployee_age().compareTo(employeeList0.getEmployee_age());
    return flag;
     }
}


3、测试

package com.test.test;

import com.test.bean.Employee;
import java.util.*;
import java.util.Comparator;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

public class EmployeeTest {
/**
*  A、定义10个雇员,并且添加到HashSet集合类中,然后将所有雇员的资料打印出来。  
B、按雇员年龄降序排列。    
c、将上面的雇员的身份证号作为键对象,雇员类对象作为值对象,添加5个雇员到HashMap集合类对象中,并打印出每个雇员的身份证号、ID、姓名、年龄和地址。*/

public Set<Employee> set = new HashSet<Employee>();

public void test(){
//A、定义10个雇员,并且添加到HashSet集合类中,然后将所有雇员的资料打印出来。  
Employee employee0 = new Employee("1","1","1","1");
Employee employee1 = new Employee("2","2","2","2");
Employee employee2 = new Employee("3","3","3","3");
Employee employee3 = new Employee("4","4","4","4");
Employee employee4 = new Employee("5","5","5","5");
Employee employee5 = new Employee("6","6","6","6");
Employee employee6 = new Employee("7","7","7","7");
Employee employee7 = new Employee("8","8","8","8");
Employee employee8 = new Employee("9","9","9","9");
Employee employee9 = new Employee("0","0","0","0");

set.add(employee0);
set.add(employee1);
set.add(employee2);
set.add(employee3);
set.add(employee4);
set.add(employee5);
set.add(employee6);
set.add(employee7);
set.add(employee8);
set.add(employee9);

System.out.println("---------排序之前-----------");
for(Employee e:set){
System.out.println("雇员ID:"+e.getEmployee_ID()+"雇员姓名:"+e.getEmployee_name()+"雇员年龄:"+e.getEmployee_age()+"雇员地址:"+e.getEmployee_age());
System.out.println("*******************************************");
}

//B、按雇员年龄降序排列。    
System.out.println("---------排序之后-----------");
//Set转List,方法一 : ArrayList(Collection<?> c)   
//Set转List,方法二:List实现类(ArrayList/LinkedList)的方法  -- addAll(Collection<?> c)   
List<Employee> list = new ArrayList<Employee>();  
list.addAll(set);
ComparatorTask comparator=new ComparatorTask();
Collections.sort(list,comparator);
for(Employee l:list){
System.out.println("雇员ID:"+l.getEmployee_ID()+"雇员姓名:"+l.getEmployee_name()+"雇员年龄:"+l.getEmployee_age()+"雇员地址:"+l.getEmployee_age());
System.out.println("*******************************************");
}

//c、将上面的雇员的身份证号作为键对象,雇员类对象作为值对象,添加5个雇员到HashMap集合类对象中,并打印出每个雇员的身份证号、ID、姓名、年龄和地址。*/
//构造Map数据  
Map<String, Employee> map = new HashMap<String,Employee>();
map.put(employee0.getEmployee_ID(), employee0);
map.put(employee1.getEmployee_ID(), employee1);
map.put(employee2.getEmployee_ID(), employee2);
map.put(employee3.getEmployee_ID(), employee3);
map.put(employee4.getEmployee_ID(), employee4);

System.out.println("---------使用HashMap-----------");
for (String key : map.keySet()) {
Employee value = map.get(key);
System.out.println("雇员ID:"+value.getEmployee_ID()+"雇员姓名:"+value.getEmployee_name()+"雇员年龄:"+value.getEmployee_age()+"雇员地址:"+value.getEmployee_age());
System.out.println("*******************************************");

}



}



//测试
public static void main(String[] arg){
EmployeeTest employeeTest = new EmployeeTest();
employeeTest.test();
}
}





注:排序有点bug,但不影响该测试,如果有更好的排序再补充一下。


温馨提示:答案为网友推荐,仅供参考
相似回答