Java 当我们将自定义类对象作为键和值传递时,如何根据键名从HashMap中获取值
当我们将自定义类对象作为键和值对传递时,如何根据键名从哈希映射中获取值。在下面的代码中,当我使用get方法通过键检索值时,我遇到编译时错误。请帮我解决这个问题Java 当我们将自定义类对象作为键和值传递时,如何根据键名从HashMap中获取值,java,collections,Java,Collections,当我们将自定义类对象作为键和值对传递时,如何根据键名从哈希映射中获取值。在下面的代码中,当我使用get方法通过键检索值时,我遇到编译时错误。请帮我解决这个问题 HashMap<Employee1, Department1> hm= new HashMap<Employee1, Department1>(); hm.put(new Employee1(0, "name1", 25, 46666), new Department1(0, "developer"));
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);
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或部门名称来检索员工的所有详细信息您必须重写employee类的equals方法 然后你可以用
Employee1 testEmp = new Employee1(0, "name5",28, 15000);
Department1 value = (Department1) hm.get(testEmp);
System.out.println(value);
如果要使用某个类作为HashMap或HashSet中的键,则需要向其中添加方法equals()和hashCode()。 比如: 之后,可以通过如下所示的新键查找值:
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);
}
检查下面的类以获得结果 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);
}
}
你的问题不太清楚。你所说的“密钥名”是什么意思。如果这是
Employee1
的一个属性,那么也可以将该类添加到您的帖子中。什么是“cte”?重写了equal和hashode()并遵循上面的代码,但仍然没有得到所需的输出。输出为-org.task。Department1@87816dtrySystem.out.println(value.getDeptid());它的打印java对象是DeparteMet。覆盖部门的字符串方法以正确打印值如果我的答案对您有帮助,您可以投票并接受。祝你好运
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);
}
@Override
public String toString() {
return "Department1{" +
"deptid=" + deptid +
", deptname='" + deptname + '\'' +
'}';
}
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);
}
}
/**
* @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();
}
}
/**
* @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);
}
}
/**
* @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;
}
}