Reputation: 27
I have created a Map as follows:
private Map<String, List<Client>> clientCatalogue;
this.clientCatalogue = new TreeMap<String, List<Client>>();
The keys are client names, and the values are a list of client details.
I am iterating over it using keySet(). I want to access each list of values associated with the keys one at a time, and then do further processing on each before moving on to the next list of values. I have copied the values into a List type variable (clientDetails) and thought I'd print them out to check them. But I keep getting hashcodes back. Why is this, and what can I do to unpack these hashcodes and access the values I want? Any suggestions appreciated.
List<Client> clientDetails;
clientDetails = new ArrayList<Client>();
for (String eachClient : this.clientCatalogue.keySet())
{
clientDetails = clientCatalogue.get(eachClient);
System.out.println("Details of client are: " + clientDetails);
}
Upvotes: 2
Views: 104
Reputation: 13393
Try this:
for (String eachClient : this.clientCatalogue.keySet())
{
List<Client> clientDetails; = clientCatalogue.get(eachClient);
for(Client cl : clientDetails)
{
System.out.println("Details of client are: " + cl.toString());
}
}
The problem was that you were printing the list as a whole instead of the individual clients in the list.
BTW, you need a proper implementation for the toString()
method in your Client
class.
Also you do not need to create a new instance for List<Clients> clientDetails
.
Also, if you do not want to iterate on the keys but on the values directly do this:
for (List<Client> clientDetails : this.clientCatalogue.values())
{
for(Client cl : clientDetails)
{
System.out.println("Details of client are: " + cl.toString());
}
}
Upvotes: 2
Reputation: 26574
TreeMap inherits it's toString method from AbstractMap which it looks like will attempt to output a pretty representation of the map, but if you haven't given a toString implementation of clientDetails you aren't going to get anything pretty. A sample of your string output would help to know for sure though.
Upvotes: 0
Reputation: 8874
You need to implement toString()
in your Client
class to return the details you want to see printed.
Upvotes: 0
Reputation: 26418
You should override toString() method in Client class. When you try to print any object through sysout then its toString method will be called, In your case its called of the Object's class. So override it as how you want sysout print it for you.
Upvotes: 0