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.

asked Jan 24, 2014 at 19:32
9
  • You could just write HashMap<String, String> myMap = new HashMap<String, String>(); and then you only need one import. Commented Jan 24, 2014 at 19:35
  • 2
    Take a look at What does it mean to "program to an interface"? Commented Jan 24, 2014 at 19:43
  • I'm pretty sure that he knows what's an interface and how it's used :) Commented Jan 24, 2014 at 19:47
  • 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. Commented Jan 24, 2014 at 20:20
  • 1
    @quakkels: Because Java wasn't created by Microsoft, so doesn't use MS notation. So called Hungarian notation is pretty much only used in the Microsoft ecosystem. Commented Jan 24, 2014 at 22:33

4 Answers 4

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>();

answered Jan 24, 2014 at 19:34

Comments

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.

answered Jan 24, 2014 at 19:40

Comments

2

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

answered Jan 24, 2014 at 19:37

Comments

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,2503 gold badges15 silver badges20 bronze badges
answered Jan 24, 2014 at 19:57

8 Comments

There are more than three.
... and EnumMap, and ConcurrentHashMap, and Properties, and others...
@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, i know, but i wrote what oracle said, i 'll update my answer to include the others
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.