2012-06-23 52 views
1

我看了网上,我还没有能够解决我有问题。我正在使用哈希映射制作一个员工商店,但我不知道我是否应该打印toString()或theEntry.getKey(),就像我在当前版本中一样。当使用信息getKey()方法,我得到错误的输出是:HashMapStore。打印方法没有显示正确的输出

*员工在公司。 *** 员工姓名:詹姆斯O“卡罗尔 员工标识:詹姆斯O”卡罗尔 电子邮件:詹姆斯O”卡罗尔

正如你可以在MainApp看到我想Store.add(新员工(“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()); 

     } 
    } 


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


} 

回答

3

这显然是错误的:

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

要打印的相同值的三倍 - 这怎么可能是姓名,身份证电子邮件地址?您正在输入密钥,该条目始终是名称。您希望的值为的条目。

你想这样的:

// 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这取决于你所要的输出是什么。)

+0

是的工作,谢谢你。我从一些笔记中得到了这段代码,它通过使用它来工作:System.out.println(“\ n ----- FLIGHTS IN THE AIR ----”); \t \t为(Map.Entry的<字符串,平面> theEntry:map.entrySet()) \t \t { \t \t \t的System.out.println( “飞行时间:\ t” 的+ theEntry.getKey()); System.out.println(“Details:\ t”+ theEntry.getValue());}; \t \t} \t}锄头这样做的工作。 – Pendo826

+0

+1。你最后一次得到赞誉的时间是什么时候? :) –

+1

@SoboLAN:哦,这是一个罕见的一天,我没有*任何* downvotes。有时候是应得的,有时候甚至没有解释... –

2

你要迭代的值地图。通过这样做,你会得到Employee实例的回报,并且可以简单地定义你的print()方法如下,

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

请注意,您并不需要显式调用toString()因为println会自动为你做的。