• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Getting objects value in a map

 
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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[0] that contains the value. I want to get that value. Thanks.
 
Marshal
Posts: 75857
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
sridhar row
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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[0] containing the value i need.
 
Campbell Ritchie
Marshal
Posts: 75857
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Better (in Java 5 or Java 6) to declare the map as Map<Something, String[]> then you can only put String[]s in as values, and you can dispense with the classcast.

But, if you are sure the values will only be String[]s 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
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic