seeing values instead if java.lang.String

Hello I have this fairly simple code..import java.util.*;

publicclass HashMapDemo{

publicstaticvoid main(String[] args){

String[] names={"123123","afasdf","asghnbgf"};

HashMap hm =new HashMap<String,String[]>();

hm.put("Rohit", names);

hm.put("Mohit",new Double(123.22));

hm.put("Ashish",new Double(1200.34));

hm.put("Khariwal",new Double(99.34));

hm.put("Pankaj",new Double(-19.34));

Set set = hm.entrySet();

Iterator i = set.iterator();

while(i.hasNext()){

Map.Entry me = (Map.Entry)i.next();

System.out.println(me.getKey() +" : " + me.getValue() );

}

}

}

Currently it shows me this:

Mohit : 123.22

Rohit : [Ljava.lang.String;@3e25a5

Ashish : 1200.34

Pankaj : -19.34

Khariwal : 99.34

For Rohit I want to see the values not the Java.lang.String@3e25a5. Any help

?

Thanks

[1791 byte] By [h2opologirly69a] at [2007-11-27 7:06:03]
# 1
Why are you mixing types in a Hashmap. What purpose can this serve other than as a source of bugs?
petes1234a at 2007-7-12 18:57:18 > top of Java-index,Java Essentials,Java Programming...
# 2

names is an array. Arrays don't override toString.

Eithe iterate over the array and print out each value individually, or use java.util.Arrays.toString, or, if you're on pre-1.5, use java.util.Arrays.asList (which returns a List that overrides toString()).

http://developer.java.sun.com/developer/Books/effectivejava/Chapter3.pdf

jverda at 2007-7-12 18:57:19 > top of Java-index,Java Essentials,Java Programming...
# 3
What you're seeing is the output of the default toString method, which arrays inherit from java.lang.Object.If you want to see nicer output, use a java.util.List.
paulcwa at 2007-7-12 18:57:19 > top of Java-index,Java Essentials,Java Programming...
# 4
> Why are you mixing types in a Hashmap. What purpose> can this serve other than as a source of bugs?Oh, yeah. That's very icky bad stinky.
jverda at 2007-7-12 18:57:19 > top of Java-index,Java Essentials,Java Programming...
# 5
> Why are you mixing types in a Hashmap. What purpose> can this serve other than as a source of bugs?Sometimes, newbies do this as a naive, incorrect way of making structs.Of course, the correct way to do that in Java is to define a class.
paulcwa at 2007-7-12 18:57:19 > top of Java-index,Java Essentials,Java Programming...
# 6
By the way, your subject: seeing values instead if java.lang.StringYou're seeing the results of the toString method of the String[] class. Since arrays don't override Object's toString, you get the default "class + hashcode" results.
jverda at 2007-7-12 18:57:19 > top of Java-index,Java Essentials,Java Programming...
# 7
b/c i am getting data that is like this....string [] one= {"roshit","roshit","david"};string [] two={"asfsdf","vcwwr","okwoe"};i want to make sure if i call roshit i get both those values in string to for him and david gets his one value..
h2opologirly69a at 2007-7-12 18:57:19 > top of Java-index,Java Essentials,Java Programming...
# 8

> b/c i am getting data that is like this....

>

> string [] one= {"roshit","roshit","david"};

> string [] two={"asfsdf","vcwwr","okwoe"};

>

> i want to make sure if i call roshit i get both those

> values in string to for him and david gets his one

> value..

Then you should have a

Map < String , Set < String > > //where the set may be a singleton, or

Map < String, YourClassHere >

And wasn't this answered in your other thread: http://forum.java.sun.com/thread.jspa?threadID=5181116

Message was edited by:

Hippolyte

Hippolytea at 2007-7-12 18:57:19 > top of Java-index,Java Essentials,Java Programming...
# 9
What, are those rows in a database? And "rohit" and "david" are column names? And you're trying to associate the values with the columns?And what does any of that have to do with the Doubles in the map you created earlier?
paulcwa at 2007-7-12 18:57:19 > top of Java-index,Java Essentials,Java Programming...
# 10
nothing with the doubles i just found the code and tried to manipulateit and didnt change it b/c i was just playing with code. those two strings [] are columns.. one column is names and the other column is data associated with those names.
h2opologirly69a at 2007-7-12 18:57:19 > top of Java-index,Java Essentials,Java Programming...
# 11

I don't know what problem you're having now. My earlier post told you how to address the problem you mentioned initially, and others gave you some other good general advice.

You can't still be having the same problem you initially had, unless you ignored or didn't undrerstand what I told you. If you ignore it, please read it. If you didn't understand, plesae say so and be specific about what part you didn't understand.

If you've moved on to a new problem, please explain that problem in detail.

jverda at 2007-7-12 18:57:19 > top of Java-index,Java Essentials,Java Programming...
# 12

Hi,

The i.next() method will return the Object. You need to show the values manually. Otherwise it will display the classname@hashCode will be displayed.

See the following code and execute it. You will understand what the problem was?

public class DemoString

{

public static void main(String[] args)

{

String str[]={"Hello","How","are","you"};

//I am going to print the string array, it will print the classname@hashCode

System.out.println(str);

//I am going to print the double object, it will print the double value.

Double d=new Double(30.23);

System.out.println(d);

}

}

If you have any queries please let me know.

Thanks & Regards,

Santhosh Reddy Mandadi

santhosh-mcaa at 2007-7-12 18:57:19 > top of Java-index,Java Essentials,Java Programming...
# 13
As already explained, he's calling an array's toString, which does not override Object's toString. Please don't comment unless you have something to add--it merely confuses the issue.
jverda at 2007-7-12 18:57:19 > top of Java-index,Java Essentials,Java Programming...