Forums Register Login

Add Strings in HashMap

+Pie Number of slices to send: Send
Hi,

I want to create a HashMap which is having the key as 1,2,3..... and the value as strings. When i try adding it, i could find only the put method whioch takes only the objects as the input. Can anyone please tell me hiow can i add these strings and integer values into the hashmap table. Thanks.
+Pie Number of slices to send: Send
You mean this put? :
http://java.sun.com/j2se/1.4.2/docs/api/java/util/HashMap.html#put(java.lang.Object,%20java.lang.Object)

You can put here Integer and String. You can't put explicit primitives there.
+Pie Number of slices to send: Send
Yes. This put method only.

Error i am getting is "No match was found for the method put(int, java.lang.String) in type java.util.HashMap
+Pie Number of slices to send: Send
You can't put int in there as it expects Object. So i guess you should wrap your int in Integer...
+Pie Number of slices to send: Send
Hi Renu,

With Java 1.5 came autoboxing and generics. You can do it in two ways.
Using generics


You can use this without generics also. Like this


If you are still sticking on to 1.4, you can write it like this
[edit]Add newlines to keep all text within screen width. CR[/edit]
+Pie Number of slices to send: Send
Hi Bala,
Its Working.
I have added something like this.

Now i want to get the value of arraylength when i give the tempdate value.

I want to know how to which variable i can assign this to.Can i do something like

Thanks
+Pie Number of slices to send: Send
 

renu richard wrote: . . . I want to know how to which variable i can assign this to.Can i do something like

Yes, as long as the compiler knows you are returning an Integer. It will work in Java5 if you declare the Map as Map<Something, Integer> and instantiate it with the types too. In Java1.4 and older, you would have to use a class cast and some method of the Integer class to get the value.
His brain is the size of a cherry pit! About the size of this ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 6271 times.
Similar Threads
Web App HashMap Serilization
addition of two Hash maps
HashMap values
Hashmap in java
HashMap size
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 07:27:17.