【问题标题】:HashMapStore. Print Method not showing correct output哈希映射存储。打印方法未显示正确的输出
【发布时间】:2012-06-23 19:15:47
【问题描述】:

我在网上查看过,但无法解决我遇到的问题。我正在使用 hashMap 创建员工商店,但我不确定是否应该像当前版本一样打印 toString() 或 theEntry.getKey()。当使用 getKey() 方法时,我得到了错误的输出:

*公司员工。*** 员工姓名:詹姆斯·奥卡罗尔 员工编号:James O' Carroll 电子邮件:James O' Carroll

正如您在 MainApp 中看到的,我想要 Store.add(new Employee ("James O' Carroll", 18,"hotmail.com"));作为输出。

我将粘贴我的代码:

//MainApp.
public class MainApp
{

    public static void main(String[] args)
    {
        new MainApp().start();

    }
    public void start()
    {
        EmployeeStore Store = new EmployeeStore();
        Store.add(new Employee ("James O' Carroll", 18,"hotmail.com"));
        Store.print();

    }

}

//Employee
//Imports:

//********************************************************************
//Employee Class.
public class Employee
{
//Variables.
    private String employeeName;
    private int employeeId;
    private String employeeEmail;
//********************************************************************  
//Constructor.
    public Employee(String employeeName, int employeeId, String employeeEmail) 
    {
        this.employeeName = employeeName;
        this.employeeId = employeeId;
        this.employeeEmail = employeeEmail;
    }
//********************************************************************
//Getters.
    public String getEmployeeEmail() {
        return employeeEmail;
    }
    public void setEmployeeEmail(String employeeEmail) {
        this.employeeEmail = employeeEmail;
    }
    public String getEmployeeName() {
        return employeeName;
    }
    public int getEmployeeId() {
        return employeeId;
    }
//********************************************************************
//toString method.
    public String toString() {
        return "Employee [employeeName=" + employeeName + ", employeeId="
                + employeeId + ", employeeEmail=" + employeeEmail + "]";
    }
//********************************************************************





}

//EmployeeStore.
//Imports.
import java.util.HashMap;
//********************************************************************
import java.util.Map;

public class EmployeeStore 
{
    HashMap<String, Employee> map;

//Constructor.  
    public EmployeeStore()
    {
        map = new HashMap<String,Employee>();
    }
//********************************************************************
//Hashmap Methods.
//Add to the Hashmap : Employee.
    public void add(Employee obj)
    {

        map.put(obj.getEmployeeName(), obj);
    }
//********************************************************************
//Remove from the Hashmap : Employee.
    public void remove(String key)
    {
      //Remove the Employee by name.
        map.remove(key);
    }
//********************************************************************
//Print the Hashmap : Employee. 
    public void print()
    {
        System.out.println("\n********Employee's in the Company.********");
        for(Map.Entry<String, Employee> theEntry : map.entrySet())
        {
            System.out.println("Employee Name:\t" + theEntry.getKey());
            System.out.println("Employee Id:\t" + theEntry.getKey());
            System.out.println("E-mail:\t "+ theEntry.getKey());

        }
    }


//********************************************************************  
//********************************************************************


}

【问题讨论】:

    标签: java hashmap


    【解决方案1】:

    这显然是错误的:

    for(Map.Entry<String, Employee> theEntry : map.entrySet())
    {
        System.out.println("Employee Name:\t" + theEntry.getKey());
        System.out.println("Employee Id:\t" + theEntry.getKey());
        System.out.println("E-mail:\t "+ theEntry.getKey());
    }
    

    您打印了 3 次相同的值 - 怎么可能是姓名、ID 电子邮件地址?您正在从条目中打印 key,它始终是名称。您想要条目的

    你想要这个:

    // This assumes you've renamed all the properties in Employee to be more
    // sensible - there's no point in including the "employee" part - we already
    // know we're calling a method on Employee
    for (Employee employee : map.values())
    {
        System.out.println("Employee Name:\t" + employee.getName());
        System.out.println("Employee Id:\t" + employee.getId());
        System.out.println("E-mail:\t "+ employee.getEmail());
    }
    

    (当然也可以只打印employee。这取决于您希望输出的内容。)

    【讨论】:

    • 是的,谢谢。我从一些笔记中得到了那个代码,它通过使用这个来工作:System.out.println("\n----- FLIGHTS IN THE AIR ----"); for(Map.EntrytheEntry : map.entrySet()) { System.out.println("飞行时间: \t" + theEntry.getKey()); System.out.println("详细信息:\t" + theEntry.getValue()); } } 锄头可以做到这一点。
    • @SoboLAN:哦,我很少有没有得到 任何 反对票的日子。有时是应得的,有时甚至没有解释......
    • @ConorPendlebury:嗯,这是打印键,然后是每个条目的值……你不明白什么?
    • 这是我第一次使用它,所以我不太了解它:S
    • @ConorPendlebury:你第一次使用什么?一点一点地进行 - 确保您了解每一行,并且在了解之前不要继续前进。这样你就不会不知所措,运气好的话。
    【解决方案2】:

    您需要遍历地图的值。通过这样做,您将获得 Employee 实例作为回报,并且可以简单地定义您的 print() 方法,如下所示,

    public void print(){
        for(Employee e : map.values()){
            System.out.println(e);
        }
    }
    

    请注意,您无需显式调用toString(),因为println 会自动为您执行此操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-23
      • 1970-01-01
      • 2015-09-22
      • 2017-08-20
      • 2018-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多