有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将地图视为用户定义的基础

我开发了一个名为Employee的pojo。JAVA现在我打算把它做成用户定义的收藏。我想制作一个地图,并在其中存储所有employee类型的对象

下面是我的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并将存储employee类型的对象

   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

共 (3) 个答案

  1. # 1 楼答案

    首先。你的哈希代码坏了。 尝试运行以下命令:

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

    如果您使用的是and 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()));
        }
    
  2. # 2 楼答案

    您需要重写Employee的toString()方法

    @Override pulic String toString() {
        return name + " " + job;
    }
    
  3. # 3 楼答案

    把这个换成

    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类方法和成员