Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I covert on object into string

 
Gopu Akraju
Ranch Hand
Posts: 242
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Map with object(class) as key and value pair.

I am trying to print them as follows:



Ann is a map as below:



Whne I try to print the key and value pair, using toString() {pairs.getKey().toString() and pairs.getValue().toString()}, I am not able to print them as it prints as below:


How do I convert the ojects into string to print? Thnaks.
 
Sagar Rohankar
Ranch Hand
Posts: 2905
1
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to override toString() method in your AnnType and Ann class, by default the Object#toString() method returns hash code !!
like this

 
Gopu Akraju
Ranch Hand
Posts: 242
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Sagar.

I tried as below in both the classes:



But I still get the same output as I mentioned earlier.
 
Sagar Rohankar
Ranch Hand
Posts: 2905
1
Java Spring Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The main intention behind overriding Object#toString() , method in your class is to print some meaningful data , explaining your class ,

eg,
This may be the implementation of your Ann class


So whenever you print object , tou endup calling Ann#toString() method ;


And this code you implemented is the default code of Object#toString() method

Hope this clarifies
[ July 08, 2008: Message edited by: Sagar Rohankar ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic