This week's book giveaway is in the OCAJP forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "HashMap" Watch "HashMap" New topic
Author

HashMap

manikandan malathy
Greenhorn

Joined: Sep 17, 2008
Posts: 13
How to store integer and string in a single hashmap?
rakesh sugirtharaj
Ranch Hand

Joined: Dec 16, 2007
Posts: 151
That can be done in a non generic map i suppose(That is not recommended of course). But why would you want to store a integer and String? Or may be you are asking about storing it like Map<Integer,String>?


Cheers!
RSR
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40495
    
  28
If you are mimicking a phone book you would want String (=name) as the Key and Integer (=number) as the Value. Only you can't store phone numbers beginning with 0 because they are interpreted as octal.
Neha Agarwal
Greenhorn

Joined: Mar 12, 2008
Posts: 16
This can be done in the following manner

import java.util.HashMap;


public class Test2{

public static void main (String args[])
{
HashMap hashMap = new HashMap();
hashMap.put(1, "One");
hashMap.put(2, "Two");
System.out.println(hashMap.get(1));
}

}
 
 
subject: HashMap