如何根据键名从Hash Map获取值,而我们将自定义类对象作为键和值对传递。在下面的代码中,当我使用get方法通过使用键检索值时,我得到编译时间错误。请帮我解决这个问题。
HashMap<Employee1, Department1> hm= new HashMap<Employee1, Department1>();
hm.put(new Employee1(0, "name1", 25, 46666), new Department1(0, "developer"));
hm.put(new Employee1(0, "name2", 50, 40000), new Department1(0, "tester"));
hm.put(new Employee1(0, "name3", 34, 3000), new Department1(0, "hr"));
hm.put(new Employee1(0, "name4",30, 26666), new Department1(0, "manager"));
hm.put(new Employee1(0, "name5",28, 15000), new Department1(0, "accountant"));
Department1 value = (Department1) hm.get(0, "name5",28, 15000);
System.out.println(value);
Department.java -
package org.task;
public class Department1 {
private int deptid;
private String deptname;
public int getDeptid() {
return deptid;
}
public String getDeptname() {
return deptname;
}
public Department1(int deptid, String deptname) {
this.deptid = deptid;
this.deptname = deptname;
}
}
Employee.java -
package org.task;
public class Employee1 {
private int id;
private String name;
private int age;
private long salary;
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public long getSalary() {
return salary;
}
public Employee1(int id, String name, int age, int salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
}
我想通过传递部门ID或deptname
来检索员工的所有详细信息答案 0 :(得分:2)
您必须覆盖Employee类的equals方法 然后你可以使用
Employee1 testEmp = new Employee1(0, "name5",28, 15000);
Department1 value = (Department1) hm.get(testEmp);
System.out.println(value);
答案 1 :(得分:0)
如果要在HashMap或HashSet中使用某个类作为键,则需要向其添加equals()和hashCode()方法。 像:
public static class Employee1 {
private int id;
private String name;
private int age;
private long salary;
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public long getSalary() {
return salary;
}
public Employee1(int id, String name, int age, int salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee1 employee1 = (Employee1) o;
if (age != employee1.age) return false;
if (id != employee1.id) return false;
if (salary != employee1.salary) return false;
if (!name.equals(employee1.name)) return false;
return true;
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + name.hashCode();
result = 31 * result + age;
result = 31 * result + (int) (salary ^ (salary >>> 32));
return result;
}
}
之后可以通过这里的新密钥找到价值:
public static void main(String[] args) throws Exception {
HashMap<Employee1, Department1> hm = new HashMap<Employee1, Department1>();
hm.put(new Employee1(0, "name1", 25, 46666), new Department1(0, "developer"));
hm.put(new Employee1(0, "name2", 50, 40000), new Department1(0, "tester"));
hm.put(new Employee1(0, "name3", 34, 3000), new Department1(0, "hr"));
hm.put(new Employee1(0, "name4", 30, 26666), new Department1(0, "manager"));
hm.put(new Employee1(0, "name5", 28, 15000), new Department1(0, "accountant"));
Department1 value = hm.get(new Employee1(0, "name5", 28, 15000));
System.out.println(value);
}
如果你需要执行System.out.println(value); 您需要将方法toString()添加到Department1。像:
@Override
public String toString() {
return "Department1{" +
"deptid=" + deptid +
", deptname='" + deptname + '\'' +
'}';
}
答案 2 :(得分:0)
检查波纹管类以获得结果。
MainTest.java
import java.util.HashMap;
/**
* @author Rajesh
*
*/
public class MainTest {
public static void main(String []args)
{
HashMap<Employee, Department> hm= new HashMap<Employee, Department>();
hm.put(new Employee(0, "name0", 20, 46666), new Department(0, "developer"));
hm.put(new Employee(0, "name0", 25, 46666), new Department(0, "tester"));
hm.put(new Employee(0, "name3", 34, 3000), new Department(0, "hr"));
hm.put(new Employee(0, "name4",40, 26666), new Department(0, "manager"));
hm.put(new Employee(0, "name5",28, 15000), new Department(0, "accountant"));
Department value = (Department) hm.get(new Employee(0, "name3", 34, 3000));
System.out.println(value);
}
}
//获取对象值的公共类
MainData.java
/**
* @author Rajesh
*
*/
import java.lang.reflect.Field;
public class MainData<E> {
Class c;
public String toStringData(E e1) {
// TODO Auto-generated method stub
StringBuffer getData= new StringBuffer();
try {
c=e1.getClass();;
Field fields[] = c.getDeclaredFields();
for (Field field : fields) {
Object fieldValue = field.get(e1);
getData.append(field.getName()+":"+fieldValue+",");
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return getData.toString();
}
}
Department.java
/**
* @author Rajesh
*
*/
public class Department extends MainData<Department> {
public int deptid;
public String deptname;
public int getDeptid() {
return deptid;
}
public void setDeptid(int deptid) {
this.deptid = deptid;
}
public String getDeptname() {
return deptname;
}
public void setDeptname(String deptname) {
this.deptname = deptname;
}
public Department(int deptid, String deptname) {
this.deptid = deptid;
this.deptname = deptname;
}
@Override
public String toString() {
// TODO Auto-generated method stub
//for getting object value
return super.toStringData(this);
}
}
Employee.java
/**
* @author Rajesh
*
*/
public class Employee {
private int id;
private String name;
private int age;
private long salary;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public long getSalary() {
return salary;
}
public void setSalary(long salary) {
this.salary = salary;
}
public Employee(int id, String name, int age, long salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
@Override
public boolean equals(Object obj) {
Employee employee = (Employee) obj;
//compare all the property
if (this.age != employee.age) return false;
if (this.id != employee.id) return false;
if (this.salary != employee.salary) return false;
if (!this.name.equals(employee.name)) return false;
return true;
}
@Override
public int hashCode() {
//return any unique code
return this.id+this.name.hashCode()+this.age+(int)this.salary;
}
}