70

Possible Duplicate:
Java - HashMap vs Map objects

I want to know the difference between HashMap and Map in java..??

Community
  • 1
  • 1

5 Answers5

79

Map is an interface, i.e. an abstract "thing" that defines how something can be used. HashMap is an implementation of that interface.

Lars Kotthoff
  • 107,425
  • 16
  • 204
  • 204
34

Map<K,V> is an interface, HashMap<K,V> is a class that implements Map.

you can do

Map<Key,Value> map = new HashMap<Key,Value>();

Here you have a link to the documentation of each one: Map, HashMap.

Community
  • 1
  • 1
WalterM
  • 2,686
  • 1
  • 19
  • 26
7

Map is an interface; HashMap is a particular implementation of that interface.

HashMap uses a collection of hashed key values to do its lookup. TreeMap will use a red-black tree as its underlying data store.

duffymo
  • 305,152
  • 44
  • 369
  • 561
5

Map is an interface in Java. And HashMap is an implementation of that interface (i.e. provides all of the methods specified in the interface).

smessing
  • 4,330
  • 1
  • 22
  • 19
3

HashMap is an implementation of Map. Map is just an interface for any type of map.

Stephan Herrmann
  • 7,963
  • 2
  • 27
  • 38
aseychell
  • 1,794
  • 17
  • 35