• Post Reply Bookmark Topic Watch Topic
  • New Topic

which one to prefer  RSS feed

 
ravindra patil
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am putting some key values in map
e.g Map m = new HashMap();

m.put("name", "Sachin");
so while comparing key in if condition
which one to use

directly comparing or by first getting that value in some variable and then comparing

if(((String)m.get("name")).equal("Sachin"))
{

}or

String name=(String)m.get("name");

 
Surendra Kumar
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using the key value in multiple places, use second approach.
Else the first one looks good so as to reduce the amount of code.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And more importantly, learn about generics:

No cast necessary!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!