• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

HashMap

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
i need your help regarding following program.
import java.util.HashMap;
class A {
public static void main(String[] args) {
HashMap<String,Object) map= new HashMap<String,Object>;
map.put("mode","create");
if(map.get("mode").equals("create")) {
System.out.println("ok");//it is executing
}
String mode=map.get("get");//gives compilation error
}
}
here it is comparing Object with String and give 'true' but while assigning,
it gives compilation error why?
anybody help me.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have a typo in your code:


Should be '>'
 
Venkat Kanneganti
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
by mistaken i typed like that,but that is
HashMap<String,Object> map=new HashMap<String,Object>();
while comparing with equals(),it works
but when i am assigning it not works.
can you help me?
 
Ranch Hand
Posts: 179
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Object is a super class of Class String.
We can have a string object stored in an object reference variable but when we try the other way round it gives a "type mismatch" compilation error.

As in inheritance we can have a subclass object stored in a super class reference variable but the other way gives an error.

Hope this helps.
 
Venkat Kanneganti
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ravi,
this not about storing,
map.get("mode").equals("create")
here by using map.get("mode") we wil get Object,this is compared with String 'create'
using equals(),it gives true.
but while assigning to String it gives compilation error,why?
 
Ravikanth kolli
Ranch Hand
Posts: 179
Mac Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
Even now i think it is the same thing that i talked about previously.
String.equals(Object), is the syntax for the equals method of string.

So the string "create" that is returned from the hashmap is an object, so it works fine with that. But when it comes to the assignment we are trying to load an Object object into a string reference variable. Thus we get the compilation error.

I think i am right in this one? am I?
[ March 14, 2008: Message edited by: Ravikanth kolli ]
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its related to the use of Generics as well. You have restricted the value part of your map to Object class.

So map.get("..") returns an Object.

An Object can't be assigned to a String without explicit casting. Compilation should fail here.


String.equals(...) expects an Object as parameter. So it works without problems.
 
Venkat Kanneganti
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You to all for your Responses
Regards
Venkat
 
Morning came much too soon and it brought along a friend named Margarita Hangover, and a tiny ad.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic