Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Map object
06-27-2008, 01:13 PM
Post: #1
Question Map object
I learnt that Map is an interface that maps unique keys to values. A key is an object that we use to retrieve a value at a later date. Give a key and a value, we can store the value in a Map object..Now my confusion is if Map is an Interface how come it can be instantiated i.e.,how can we have Map as an object. Kindly clarify me.
Find all posts by this user
Quote this message in a reply
06-27-2008, 03:55 PM
Post: #2
RE: Map object
We are not instantiating the interfaces. We are actually instantiating classes that implement these interfaces. For eg: in our case to store a key value pair we actually use a Hashmap or Hashtable which implements the Map interface.
Find all posts by this user
Quote this message in a reply
06-28-2008, 04:01 PM
Post: #3
RE: Map object
We cannot instantiate an Interface. But we can instantiate a class that implement an interface. For eg: We have Hashtable or HashMap which implements the Map interface. So we can assign a Hashtable or HashMap to a Map object. For eg:

[java]
Map map = new HashMap();
[/java]
Visit this user's website Find all posts by this user
Quote this message in a reply
07-01-2008, 04:19 PM
Post: #4
RE: Map object
Thanks Albin and javacodez .But I want to know what is the difference in the below two instantiations?

HashMap hm=new HashMap();

and

Map m=new HashMap();

When do we use the former and when do we use the latter?
Find all posts by this user
Quote this message in a reply
07-01-2008, 04:34 PM
Post: #5
RE: Map object
HashMap hm = new HashMap(); This code is very specific and hm can contain only a HashMap. But when we say Map m = new HashMap(), m becomes more generic. At later time we can assign a Hashtable to m if we want. But the same thing is not possible with hm.
Visit this user's website Find all posts by this user
Quote this message in a reply
07-01-2008, 05:04 PM
Post: #6
RE: Map object
Ok thanks a lot Albin.
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump: