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