2016-10-10 77 views
0

我的问题是,我如何“解开”一个哈希值对象?值HashMap对象

我能够创建,保存和加载哈希映射。我正在进行填充,然后选择员工来计算工资。

Map<Integer, Employee> hm = new HashMap<>(); 

hID++; 
    System.out.println("Enter employee first name: "); 
    String nameF = sc.next(); 

    System.out.println("Enter employee last name"); 
    String nameL = sc.next(); 

    hourly = new HourlyEmployee(); 
    Employee hEmp = new Employee(hourly, nameF, nameL); 

    System.out.println(hEmp.getName()); 

    hm.put(hID, hEmp); 

/* 
save() 
exit 
run 
load() 
select employee, enter ID 
loops to find match 
*/ 

Set<Map.Entry<Integer, Employee>> set = hm.entrySet(); 

    do 
    { 
    System.out.println("Enter the empoyee ID"); 
    int id = sc.nextInt(); 

     if (id >= 1000 && id < 1999) 
     { 
      System.out.println("***** HOURLY *****"); 

      for (Map.Entry<Integer, Employee> entry : set) 
     { 
     if (entry.getKey().equals(id)) 
     { 
      // this is where I run into the issues. all I get 
      // with the value is the hash location. I can't 
      // figure out how to unwrap into the hourly class 
      // as the object, then edit the values then wrap again. 
      System.out.println(entry.getValue()); 
      // I understand this is not how you get what I 
      // need. But this does give me: 
      // 
      // "[email protected]"  

     } 

} 

请参阅代码注释。

+0

无怪异的“包装”已经发生的值。 *任何* Employee都会打印这样的HashMap值或不是,因为您没有写入toString方法。只要像平时那样使用对象即可。 – user2357112

+0

我想说的是类HourlyEmployee是Employee的一部分,带有继承。我希望能够访问HourlyEmployee方法 – Chris

+0

如果不涉及HashMap,请执行您所做的任何操作。 (此外,你的班级构成没有任何意义。) – user2357112

回答

0

我找到了解决办法:*部分代码

public void selectEmployee() { 
    Set<Map.Entry<Integer, Employee>> set = hm.entrySet(); 

    do { 
     System.out.println("Enter the empoyee ID"); 

     int id = sc.nextInt(); 

     if (id >= 1000 && id < 1999) { 
      System.out.println("***** HOURLY *****"); 

      for (Map.Entry<Integer, Employee> entry : set) { 
       if (entry.getKey().equals(id)) { 

        int key = entry.getKey(); 
        Employee hEmp = entry.getValue(); 

        hEmp.getHourlyEmp().getGrossPay(); 

       } 

      } 
+0

我应该知道这一点。我在想什么。 – Chris