HashMap对象的值

时间:2016-10-10 16:47:48

标签: hashmap key-value

我的问题是,我如何解开"一个hashmap值对象?

我可以创建,保存和加载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:
           //
           // "edu.umsl.composition.++++++++.Employee@682a0b20"       

       }

}

请参阅代码注释。

1 个答案:

答案 0 :(得分: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();

                }

            }