我开发了一个名为 Employee.java 的 pojo。现在我打算把它作为用户定义的集合。我想制作一张地图并将所有员工类型的对象存储在其中。
下面是我的pojo
public class Employee {
String name,job;
int salary;
public Employee(String n , String j, int t ) //constructor
{
this.name= n;
this.job=j;
this.salary= t;
}
@Override
public int hashCode()
{
return name.hashCode()+job.hashCode()+salary;
}
@Override
public boolean equals(Object obj) {
Employee e = (Employee) obj;
return this.name.equals(e.name)&&this.job.equals(e.job)&&this.salary==e.salary;
}
}
现在我开发了另一个类,其中包含地图并将存储员工类型对象..
public static void main(String[] args)
{
Map employeeMap = new HashMap();
Employee e = new Employee("Saral", "Trainer", 34000);
Employee e1 = new Employee("Sarall", "saral", 34090);
employeeMap.put("S", e);
employeeMap.put("S1", e);
System.out.println(employeeMap.size());
Set s = employeeMap.entrySet();
Iterator it = s.iterator();
while(it.hasNext())
{
Map.Entry m =(Map.Entry)it.next();
System.out.println(m.getKey()+"\t"+m.getValue());
}
但是当我尝试运行它时,我想获取员工详细信息,但我在屏幕上显示了对象...我想查看员工值,请告诉我如何从员工对象中获取值。
2
S CollectionsPrac.Employee@285c2854
S1 CollectionsPrac.Employee@285c2854