-1

I created a HashMap where the keys are Integers and the values are of the Employee class. Employee contains the employee's first name, last name and address. I am having issues with printing out the values. Here is what i tried.

employees.put(5, e1);

String test=employees.get(5).toString();
System.out.println(employees.toString());
System.out.println(test);

output:

{5=identitymanagement.Employee@6da264f1}
identitymanagement.Employee@6da264f1

What am I doing wrong?

ollo
  • 24,797
  • 14
  • 106
  • 155
  • Nothing. Your first output is the hash (key 5, value identitymanagement.Employee@6da264f1), the second is the employee class identitymanagement.Employee@6da264f. What do you expect to see? – m0skit0 Apr 13 '13 at 20:25
  • All is good, keep going! – Vitaly Apr 13 '13 at 20:27
  • Overwrite toString in employee if you want to be able to print it. – danieln Apr 13 '13 at 20:27
  • See [this](http://stackoverflow.com/questions/4712139/java-object-default-tostring) question. – m0skit0 Apr 13 '13 at 20:28
  • You're currently printing out the employee object, and the number that you're seeing is its reference in memory. You'll need to access that object's fields and print those. – kronion Apr 13 '13 at 20:28
  • You even did not give him a chance to phrase a question.. Not good, really. – Vitaly Apr 13 '13 at 20:29
  • What exactly do you want to print for the `Employee` class? `identitymanagement.Employee@6da264f1` is the default `toString()` method for any class. You need to either program a new `toString()`, or use a `java.util.Iterator` over the `keySet()` to print out the key and a custom representation of the `Employee` for each entry. – ameed Apr 13 '13 at 20:48

2 Answers2

2

A look at your code

String test=employees.get(5).toString();

This will grab the item with the key 5 in your hashmap, then call that object's toString method. The way your object is behaving at the moment implies you have no overridden that method, which is why it is printing out the objects address in memory.

System.out.println(employees.toString());

This will attempt to print out the HashMap object. In the same vain as your Employee class, HashMap does not override it's toString method, so it simply prints out the objects reference in memory.

A solution

The convention, when outputting the details of a class, is to override the toString() method. This would look something like this:

public String toString()
{
   return "name: " + name;
}

When you place this method in your class, you can call the toString method and it won't just print out the memory address of the object, which is what it is doing at the moment :)

When using this code, all you have to do is pass the object to the System.out.println method and it will do the rest:

Employee e = employees.get(5);
System.out.println(e);
christopher
  • 26,815
  • 5
  • 55
  • 89
0

The correct way is

Employee e = employees.get(5); // return's Employee object stored in map with key 5

String firstName = e.firstName;
String lastName = e.lastName;
String address = e.address;
Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111