1

Possible Duplicate:
Java - HashMap vs Map objects

does the hash-map have multiple values?

Community
  • 1
  • 1
  • What lanaguage? If Java, duplicate of [Java - HashMap vs Map objects](http://stackoverflow.com/questions/1348199/java-hashmap-vs-map-objects) – Michael Petrotta Jan 02 '11 at 03:27

3 Answers3

2

In Java, a Map is an interface whereas a HashMap implements the Map interface. In other words, a HashMap can be instantiated and assigned to a Map variable

Map myMap = new HashMap();

A HashMap and a Map can contain multiple key/value pairs, but they cannot contain duplicate keys.

jamesmortensen
  • 33,636
  • 11
  • 99
  • 120
  • 1
    Just to clarify -- An instantiated Map class on it's own CANNOT have multiple keys per one pair. "A map cannot contain duplicate keys; each key can map to at most one value." http://docs.oracle.com/javase/6/docs/api/java/util/Map.html – westonplatter Mar 22 '12 at 07:24
2

A hash map is a particular implementation of a map, using a hash function. Maps always have (up to) one value per key.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
1

With reference to Java language, Map is an interface in java.util package that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value. HashMap is a Hash table based implementation of the Map interface. HashMap provides all of the optional map operations, and permits null values and the null key. If you want to use multiple values for a single key, then just use either a Map<K, Collection> or Google Collections MultiMap<K, V>. In case the key is a String, hash map will create a hash of the string and use that hash to index an array, giving constant lookup. However, there would be some collision detection required as the hash of a string can produce the same index as the hash of another string. Therefore complexity is added to manage collisions.

Map:

  • Cannot contain duplicate values.
  • Each key can map to atmost one value.

HashMap:

  • Hash creation (might be linear complexity to string size depending on algorithm to create hash).
  • Constant lookup with hash.
  • If Collision taken into consideration, adds up linear complexity to number of collisions for the same hash.
Piyush Mattoo
  • 15,454
  • 6
  • 47
  • 56
  • The question said nothing about Java. A map is an abstract data type that can be used in any language. In fact, he's clarified he's actually talking about C. Your answer also contradicts itself. Map *is* a interface in Java, and it doesn't specify any implementation. Thus, it's obviously wrong to say it has log complexity. In either Java or another language, there are multiple ways to implement a map. Hash maps and tree maps (which do have log complexity) are two of those ways. There is also no need for the key to be a string. – Matthew Flaschen Jan 02 '11 at 03:49
  • Sure! I happen to assume something before the clarification that he was referring to C language. Further, you are right that the key doesn't have to be String always but that was just an example perhaps not worded correctly. – Piyush Mattoo Jan 02 '11 at 03:54
  • The bullet points under Map still aren't correct. Maps can be implemented many ways, and a hash map *is* a map. Those bullet points might make sense for a `TreeMap`. – Matthew Flaschen Jan 02 '11 at 04:03
  • Sure, Thanks for bring this to attention. – Piyush Mattoo Jan 02 '11 at 04:17