1

My primary language has been C#, though lately I've been doing more Java development. In C#, I can define a Dictionary like this:

using System.Collections.Generic;

...

Dictionary<string, string> myDict = new Dictionary<string, string>();

However, if I want to create a similar object in Java, I need to do this:

import java.utils.Map;
import java.utils.HashMap;

...

Map<String, String> myMap = new HashMap<String, String>();

Why is Java designed so that Map<> is created with a HashMap<> and two different imports are required to use it?

Just curious.

Update

It never even crossed my mind that Map could be an interface. It doesn't follow the convention of prefixing the interface name with an I. I'm surprised that such a convention isn't used there.

quakkels
  • 11,676
  • 24
  • 92
  • 149
  • You could just write `HashMap myMap = new HashMap();` and then you only need one import. –  Jan 24 '14 at 19:35
  • 2
    Take a look at [What does it mean to “program to an interface”?](http://stackoverflow.com/q/383947/1393766) – Pshemo Jan 24 '14 at 19:43
  • I'm pretty sure that he knows what's an interface and how it's used :) – Svetlin Zarev Jan 24 '14 at 19:47
  • I don't know C#, but from my quick perusal of online docs, it looks like you can do `IDictionary myDict = new Dictionary()`. Do C# programmers not do this? What if you find out that a [TreeMap is better than a HashMap in some cases](http://stackoverflow.com/questions/8669946/application-vulnerability-due-to-non-random-hash-functions)? – yshavit Jan 24 '14 at 19:50
  • If you want you can make `Serializable s = new HashMap` the idea is the same you can always refer to an object with its type. – nachokk Jan 24 '14 at 19:54
  • 1
    @yshavit Yes, in C# you would use the `IDictionary` interface just that way. The statement `using System.Collections.Generic` makes both `IDictionary` and `Dictionary` available, because they are both in that namespace, so only one `using` is needed. It is similar to the way that `import java.utils.*` does the job for both `Map` and `HashMap`. – Kevin Panko Jan 24 '14 at 20:20
  • What?!?! It never occurred to me that `Map` is an interface... Why isn't it called `IMap`? – quakkels Jan 24 '14 at 21:54
  • 1
    @quakkels: Because Java wasn't created by Microsoft, so doesn't use MS notation. So called [Hungarian notation](http://en.wikipedia.org/wiki/Hungarian_notation) is pretty much only used in the Microsoft ecosystem. – Chris Dodd Jan 24 '14 at 22:33
  • @quakkels: From a consumer standpoint, classes and interfaces in Java are almost interchangeable; the creators of Java likely thought that meant there was no need to make them be visibly different, while those of .NET likely thought that absent a naming convention they'd be too likely mistaken for each other in the cases where they weren't interchangeable. – supercat Jan 24 '14 at 22:57

4 Answers4

5

Map is an interface, while HashMap is a concrete implementation, just like TreeMap

BTW You can use only HashMap if you like:

HashMap<k,v> hashmap = new HashMap<k,v>();

Svetlin Zarev
  • 14,713
  • 4
  • 53
  • 82
3

Map is an interface that HashMap implements. enter image description here

We can do ,

HashMap<String, String> map = new HashMap<String, String>();

and

Map<String, String> map = new HashMap<String, String>();

The advantage to using Map<String, String> is that you can change the underlying object to be a different kind of map without breaking your contract with any code that's using it. If you declare it as HashMap<String, String>, you have to change your contract if you want to change the underlying implementation.

Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
2

It wasn't 'designed to require two types', but it is an interface, and any interface requires an implementing class somewhere.

user207421
  • 305,947
  • 44
  • 307
  • 483
1

As others said, Map is an interface that HashMap implements.

Java contains three general-purpose Map implementations: HashMap, TreeMap, and LinkedHashMap

Map<String, String> myMap = new HashMap<String, String>();

will only allow the use of functions defined in the Map interface, while

HashMap<String, String> myMap = new HashMap<String, String>();

will allow the use of all public functions in HashMap (Map interface methods + hashMap methods).

update from the oracle website: http://docs.oracle.com/javase/tutorial/collections/interfaces/map.html

The Java platform contains three general-purpose Map implementations: HashMap, TreeMap, and LinkedHashMap. Their behavior and performance are precisely analogous to HashSet, TreeSet, and LinkedHashSet, as described in The Set Interface section.

but as mentioned in the comments below, java has actually more Map implementations: http://docs.oracle.com/javase/7/docs/api/java/util/Map.html

eddie
  • 1,252
  • 3
  • 15
  • 20
Rami.Q
  • 2,486
  • 2
  • 19
  • 30
  • There are more than three. – user207421 Jan 24 '14 at 19:58
  • ... and `EnumMap`, and `ConcurrentHashMap`, and `Properties`, and [others](http://docs.oracle.com/javase/7/docs/api/java/util/Map.html)... – yshavit Jan 24 '14 at 19:59
  • @EJP: see this link: http://docs.oracle.com/javase/tutorial/collections/interfaces/map.html – Rami.Q Jan 24 '14 at 20:00
  • @Rami.Q Those are three of the "newbie-oriented" implementations, but there are more implementations. The javadoc for `Map` lists all of the ones that come with the JDK. – yshavit Jan 24 '14 at 20:11
  • @yshavit, i know, but i wrote what oracle said, i 'll update my answer to include the others – Rami.Q Jan 24 '14 at 20:13
  • @Rami.Q The Javadoc for java.util.Map is the normative reference, not the tutorial, but in any case your link doesn't say there are *only* three implementations. You can't seriously be trying to deny that EnumMap, ConcurrentHashMap, WeakHashMap, Properties, etc., are implementations of Map. Can you? – user207421 Jan 24 '14 at 22:00
  • ... and if you were quoting from an external source you should have cited it properly. – user207421 Jan 24 '14 at 22:10
  • @EJP: i didn't say, that they are ONLY 3 implementations. the main reason why i mentioned the 3 implementations is just to clearify for the OP, that there are other implementations for map beside hashmap. for that i used the quoting from oracle. – Rami.Q Jan 25 '14 at 00:09