hashmap用法

急救、、、、、、、、、、
有一个员工类,属性有姓名,工资,工号,最后输出工资最高的员工信息,要求用HashMap做。

第1个回答  推荐于2016-02-18
package com.baiduzhidao;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Map.Entry;

public class EmpMap {
public static void main(String[] args) {
HashMap<String,Emp> hm=new HashMap<String, Emp>();
hm.put("001", new Emp("001", "张三", 2000));
hm.put("002", new Emp("002", "李四", 2030));
hm.put("003", new Emp("003", "王五", 8000));
hm.put("004", new Emp("004", "赵六", 2500));
Set<Entry<String, Emp>> set=hm.entrySet();
float max=0;
Emp maxEmp=null;
Iterator<Entry<String, Emp>> it=set.iterator();
while(it.hasNext()){
Entry<String, Emp> hmEmp=it.next();
if(hmEmp.getValue().getSalary()>max){
max=hmEmp.getValue().getSalary();
maxEmp=hmEmp.getValue();
}
}
if(maxEmp!=null){
System.out.println("工资最高的员工信息为:"+maxEmp);

}
}
}
class Emp{
private String empNo;
private String userName;
private float salary;

public Emp(String empNo, String userName, float salary) {
super();
this.empNo = empNo;
this.userName = userName;
this.salary = salary;
}
@Override
public String toString() {
return "Emp [empNo=" + empNo + ", salary=" + salary + ", userName=" + userName + "]";
}
public String getEmpNo() {
return empNo;
}
public void setEmpNo(String empNo) {
this.empNo = empNo;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}

}本回答被提问者采纳
相似回答