Мой вопрос: как мне «развернуть» объект значения хэш-карты?Объект 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]"
}
}
Просмотреть комментарии коментариев.
Нет странно «оборачивания» произошло со значением. * Любой * Employee будет печатать так, значение HashMap или нет, потому что вы не указали метод toString. Просто используйте объект, как обычно. – user2357112
То, что я пытаюсь сказать, является классом HourlyEmployee, является частью Employee, без наследования. Я хочу иметь доступ к методам HourlyEmployee. – Chris
Выполняйте все, что бы вы сделали, если HashMap не был задействован. (Кроме того, ваш класс не имеет никакого смысла.) – user2357112