关于Map作为用户定义的基础

时间:2012-04-15 17:06:07

标签: java

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


}

现在我开发了另一个包含map的类,并将存储员工类型对象..

   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 个答案:

答案 0 :(得分:3)

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

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());

除非你真的希望输出与标签分开。

答案 1 :(得分:1)

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

@Override pulic String toString() {
    return name + " " + job;
}

答案 2 :(得分: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;
}

至于你的主要方法..你应该尝试学习一些关于泛型的基础知识(<>中的东西)。你最初不需要nity grity细节。只需学习如何将它与列表和地图一起使用..它将使您的生活更轻松。特别是因为你使用和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()));
    }

答案 3 :(得分: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类方法和成员。