• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

hashmap query?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please go through the following example:I have always known that a hashmap take objects as parameters. So if the parameters to the map are String objects , so is that the reason we are able to insert into the paramsMap() object?

public static final String FUNCTION_NAME = "functionName";
public static final String CREATE_FOLDER_FUNCTION_NAME = "CreateFolder";
public static final String INTERACTION_VERB = "interactionVerb";
public static final int SYNC_SEND_RECEIVE = 1;
public static final String EXECUTION_TIMEOUT = "executionTimeout";




HashMap paramsMap = new HashMap();
paramsMap.put(ATT_Macros.FUNCTION_NAME,
ATT_Macros.CREATE_FOLDER_FUNCTION_NAME);
paramsMap.put(ATT_Macros.INTERACTION_VERB,
new Integer(ATT_Macros.SYNC_SEND_RECEIVE));
paramsMap.put(ATT_Macros.EXECUTION_TIMEOUT, new Long("10000"));




 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


What exactly you want to achieve??
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think I understand your question. Can you elaborate?
 
Sowm Herur
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I wanted to understand as String are also objects they can be just put in a hashmap using map.put("Name","Sowmya");
As string are objects implicitly we need not create instance of string like what we have done for long creating a map.put("Reg-no",new Long("23")) instance?
Hashmap map=new Hashmap();
map.put("Name","Sowmya");

say for reg-no we need to put it like

map.put("Reg-no",new Long("23"));

 
Vivek Singh
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sowm Herur wrote:
I wanted to understand as String are also objects they can be just put in a hashmap using map.put("Name","Sowmya");


Yes, have a look at the code that i have attached.But its not a GOOD PRACTICE to have string Key.
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


say for reg-no we need to put it like

map.put("Reg-no",new Long("23"));



Well, actually you can do this:


beacuse of a feature called autoboxing. But you are right, ultimately you can only put Objects in Maps.

 
Marshal
Posts: 71098
292
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you are creating String objects . . . if you write "Campbell" that is a String object equal to C-a-m-p-b-e-l-l. Strings are particularly good for "keys" in Maps because they reflect what people use on paper (look at your address book: that is a map from name to address or phone number, and the "name" is a String). Strings are also good for "keys" because they don't change. If you try . . . you will never get "123456" back.
 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings are good keys because that class is immutable. You can make any class immutable and use it as a key without doubt. So, foo is not good because it is mutable class and with changing values of its fields, its hashcode will change making it altogether different key.
 
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic