哈希映射按键排序

时间:2017-11-15 09:50:22

标签: java sorting collections hashmap key

以下是我打印未分类键值对的代码。

我有2个POJO类和一个主要方法。我必须根据对我的hashmap进行排序。我需要在hashmap中执行此操作,而不能在树形图或其他任何内容中执行此操作。任何人都可以帮助?

MAIN CLASS

public class HashDemo { public static void main(String args []){

    EmployeeBean e1=new EmployeeBean(100,"selva",1000);
    EmployeeBean e2=new EmployeeBean(101,"siva",2000);
    EmployeeBean e3=new EmployeeBean(102,"silva",3000);

    DepartmentBean d1=new DepartmentBean(123);
    DepartmentBean d2=new DepartmentBean(456);
    DepartmentBean d3=new DepartmentBean(789);

    Map <DepartmentBean,EmployeeBean> map=new HashMap<DepartmentBean,EmployeeBean>();

    map.put(d1,e1);
    map.put(d2,e2);
    map.put(d3,e3);
    for(Map.Entry m:map.entrySet()){  
           System.out.println(m.getKey()+" "+m.getValue()); 
        }

}}

引用EmployeeBean

公共类EmployeeBean {

private int empid;
private String empName;

private int empSal;
public int getEmpid() {
    return empid;
}
public void setEmpid(int empid) {
    this.empid = empid;
}
public String getEmpName() {
    return empName;
}
public void setEmpName(String empName) {
    this.empName = empName;
}
public int getEmpSal() {
    return empSal;
}
public void setEmpSal(int empSal) {
    this.empSal = empSal;
}
public EmployeeBean(int empid, String empName, int empSal) {
    super();
    this.empid = empid;
    this.empName = empName;
    this.empSal = empSal;
}
@Override
public String toString() {
    return "empid=" + empid + ", empName=" + empName + ", empSal=" + empSal + "";
}       

}

DEPARTMENTBEAN

公共类DepartmentBean {     private int deptId;

public int getDeptId() {
    return deptId;
}
public void setDeptId(int deptId) {
    this.deptId = deptId;
}

public DepartmentBean(int deptId) {
    super();
    this.deptId = deptId;
}
@Override
public String toString() {
    return "deptId=" + deptId + "";
}
}

0 个答案:

没有答案