0

我开发了一个名为 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
4

4 回答 4

3

您需要覆盖toStringEmployee 类中的方法,例如:

public String toString() {
    return name + " [" + job + "] - salary: " + salary;
}

顺便说一句,您可以替换:

    Iterator it = s.iterator();
    while(it.hasNext())
    {           
        Map.Entry m =(Map.Entry)it.next();
        System.out.println(m.getKey()+"\t"+m.getValue());

    }

System.out.println(s.toString());

除非您真的希望输出是制表符分隔的。

于 2012-04-15T17:09:56.363 回答
1

您需要覆盖toString()Employee 的方法

@Override pulic String toString() {
    return name + " " + job;
}
于 2012-04-15T17:09:36.300 回答
1

首先。您的哈希码已损坏。尝试运行这个:

        System.out.println("Should be false: " + (new Employee("Sara", "Trainer", 1).hashCode() == new Employee("Trainer", "Sara", 1).hashCode()));

如果您正在使用 IDE(如 eclipse),则有一个函数可以自动生成 equals 和 hashcode 方法,您会得到如下内容:

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((job == null) ? 0 : job.hashCode());
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    result = prime * result + salary;
    return result;
}



@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Employee other = (Employee) obj;
    if (job == null) {
        if (other.job != null)
            return false;
    } else if (!job.equals(other.job))
        return false;
    if (name == null) {
        if (other.name != null)
            return false;
    } else if (!name.equals(other.name))
        return false;
    if (salary != other.salary)
        return false;
    return true;
}

至于你的主要方法..你应该尝试学习一些关于泛型的基础知识(<>里面的东西)。一开始你不需要详细的细节。只需学习如何将它与列表和地图一起使用。它会让您的生活更轻松。尤其是因为您的使用和 IDE...

这是您的主要方法的重构版本:

public static void main(String[] args)
    {           
        Map<String, Employee> employeeMap = new HashMap<String, Employee>();
        Employee e = new Employee("Saral", "Trainer", 34000);
        Employee e1 = new Employee("Sarall", "saral", 34090);
        employeeMap.put("S", e);
        employeeMap.put("S1", e1);
        System.out.println(employeeMap.size());
        Set<Entry<String, Employee>> entrySet = employeeMap.entrySet();
        for (Entry<String, Employee> entry: entrySet) {
            System.out.println(entry.getKey()+"\t"+entry.getValue().name);
        }

        System.out.println("Should be false: " + (new Employee("Sara", "Trainer", 1).hashCode() == new Employee("Trainer", "Sara", 1).hashCode()));
    }
于 2012-04-15T17:23:10.243 回答
0

改变这个

Iterator it = s.iterator();
while(it.hasNext())
{           
  Map.Entry m =(Map.Entry)it.next();
  Employee empl = (Employee) m.getValue();
  System.out.println(m.getKey()+"\t"+empl.name);
}

正如你可以看到的线

Employee empl = (Employee) m.getValue();

该值被“强制转换”到一个Employee对象,您可以开始使用empl变量并使用所有Employee类方法和成员。

于 2012-04-15T17:09:47.377 回答