• Post Reply Bookmark Topic Watch Topic
  • New Topic

Printing a Hashmap  RSS feed

 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am trying to figure out how to connect to a web service done in .net

anyway, i found some sample code in axis folder and i am now trying to trace the program.

In tracing, i would like to know the contents of the hashmap and in doing so, i would like to print its contents.

Since i am new to java, i don't know much on hashmaps and i would just like to ask how to print it

this is my hashmap



i would like to print map before the return statement

any help would be much appreciated
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could try System.out.println(map.toString());
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can this also be applicable to objects?

thanks
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yep. But this will print something like "java.lang.Object@defa1a"
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could do something like this.

 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh ok. thanks

now how can i print the contents of an object?
coz the thing is, it retrieves results and places it in the object which then puts it in the hashmap.

i can now print those in the hashmap for checking purposes, but how to do that in object without generating this "java.lang.Object@defa1a" ?

thanks
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The story gets a bit complicated.
If the value you retrieved in outputs.get(name); is an object overriding the toString() method, it should print something more friendly.
What are you actually storing in "outputs" ?
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is my "outputs", just above the code for Hashmap map = new Hashmap();

Map outputs = call.getOutputParams();
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That doesn't tell me what kind of things you are storing in this map
Anyway, if the objects in the map override toString(), it should be OK.

For example, if "outputs" contains String objects (String class overrides toString), then calling the System.out.print as I suggested should be fine.

However, if the map contains objects not overridding toString, it will print the kind of unfriendly message posted above.
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh. ok.

it works just fine now. thank you very much

i do have another question though, if you don't mind.

i have declared a global object variable as in
public Object ret2 = new Object();

but when i use that in my main method, it outputs an error saying non-static variable cannot be referenced from a static method, or something like that.

all i do is just try to print it's value in my main method, as in
System.out.println(ret2);
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your main method is static, but your member variable is not.
You should either
1. Declare the member variable as static
-> static public Object ret2 = new Object();

2.Or instanciate your class in your main:

MyClass myclass = new MyClass();
System.out.println(myclass.ret2);

You are doing strange things
[ January 19, 2006: Message edited by: Satou kurinosuke ]
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i did instantiate my class. i will send the code tomorrow. i have to go home now.
i'm doing a program to try to connect to a web service done in asp.

thank you so much for all your help
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!