• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashMap

 
manikandan malathy
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to store integer and string in a single hashmap?
 
rakesh sugirtharaj
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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));
}

}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic