Can someone tell me a way to get a object's string value from a map? when i do a get on the key im getting a object and when i do a toString() on it i get the object's name@hashcode value. Im trying to get the string value that is inside the object. When i debug it seems like the object has a String that contains the value. I want to get that value. Thanks.
To the Object class! The toString method returns class name an @ sign and hashcode in hex. You appear to be getting the correct behaviour for an un-overridden toString() method. I think you need simply to override the toString method in whichever class you are using.
thanks Ritchie. Can't i just cast the object to a String array and get the values from it? It seems like the object that i'm getting is a String array object cause when i do a toString() on that object i get something like [ljava.lang.String@hashcode. Also as i said earlier when debugging i see a String containing the value i need.
Better (in Java 5 or Java 6) to declare the map as Map<Something, String> then you can only put Strings in as values, and you can dispense with the classcast.
But, if you are sure the values will only be Strings then you can cast them to String and use a for-each or for loop to print out the individual values, yes. There isn't a simple way to override the toString() method of an array; I don't think you can write public class MyArray extends String!
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop