2

I have a doubt on the design methodology that why we implement the code to the interface. This is very much observed in primitive data types. Like I am not getting the difference between these two :

Map<Integer, String> mymap = new HashMap<Integer, String>(); 

And

HashMap<Integer, String> mymap = new HashMap<Integer, String>();

Is there any difference between these two? I mean each and every place where we are going to use mymap will remain same in both the cases.

I am sorry if the question seems to be of no use but I really not getting that how this is going to make any difference later on where mymap will be used. Please help?

Thanks..

Note - I have already seen this question on SO but it is not giving what I want.

4
  • 1
    What is it specifically about the other question you link to that isn't giving you what you want? Commented Feb 22, 2012 at 10:18
  • the question u have linked has got very good answers Commented Feb 22, 2012 at 10:33
  • But I didn't get my answer as there is nothing specified about primitive types interface coding style as I have mentioned in my question. Commented Feb 22, 2012 at 11:02
  • 1
    Map is not a primitive type. int is, boolean is, etc. Commented Feb 26, 2012 at 19:36

6 Answers 6

7

The second option limits you to always use HashMap, even if some day TreeMap could be more useful.

In the first one you can change the particular implementation easier - you only have to change one line of code. It's especially visible if you return your map from methods - the method return type doesn't have to change.

Coding to an interface also helps mocking the object during tests, but I assume that's not the case here.

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for your answer..!! +1 for return type not have to change.
The second option also removes all specific functionality of HashMap. If you want the extra functions available from the HashMap type, you shouldn't needlessly restrict the type. The old "you can swap it out later" argument is stupid; that just means the initial evaluation of the required functionality for the map was done poorly. Restricting the return type for no other reason than that also just needlessly hides functionality that should be available on the calling level.
@Nyerguds No one is arguing against exposing the functionality when needed. The point here is that one should try expose as little as necessary to give themselves flexibility later. If you need HashMap then go ahead and use HashMap, but if all you need is a Map then use that, even if the implementing type happens to be a HashMap.
@soulcheck Again, that only applies to return types. I see no conceivable reason to put that in your initial declaration. And "flexibility later" isn't a reason; it's the same nonsensical "you can swap it out later" argument. You want to swap it out later? Then you did it wrong first, and deserve the extra work.
@Nyerguds you can contrast your anecdotical evidence with mine all day - changing implementation class happens all the time while actually using any particular characteristics of, for example, HashMaps or TreeMaps after instantiation happens seldom in my case. OP observed coding vs interface in the wild, he asked a question about what he saw and got an answer.
|
1

Because mymap can be instantiated somewhere else with a different implementation of Map, you should not rely on it being an instance of HashMap in the code using it.

3 Comments

I got you but can you please elaborate any case where I should not rely on it being an instance of HashMap ??
For example if it is created as a TreeMap.
Nonsense; the example explicitly initializes it as new HashMap. The only thing that does is hiding HashMap-specific functionality. Unless that type comes from an external source and it returned as the interface type, there is never a reason to do this.
0

Using Map mymap allows you to change the implementation later. For example, if at some point you need mymap to be ordered, you just change the initialization to LinkedHashMap.

Comments

0

If you are only using this inside your type (meaning: private) and instantiate it yourself, it doesn't make a real difference.

It starts getting interesting if the public interface of your type exposes a Map vs. a HashMap.

3 Comments

If, later on, in your private method, you decide a TreeMap would be a better collection, coding to the interface still protects you even in a private method.
IMHO this advantage is more theoretical than practical: Mostly, you will only have to change the field / variable declaration specifying the data type. According to my experience, interfaces often lack methods which the concrete type offers causing reimplementations of existing functionality or casts. Nevertheless, it's a good practice to think about the "right" data type for fields in variables also for private members.
@winSharp Map is pretty much complete though. Apart from putIfAbsent you can even use say a ConcurrentHashMap without problems.
0

In the specific instance you propose, it may not make a difference, but it's good practice to get yourself used to always use the interface in your declarations because there are legitimate instances where you will need that flexibility.

1 Comment

It's good practice to deliberately never make use of the specific functions available on HashMap or ArrayList objects? That's what you're claiming?
0

The former allows you to change to:

Map<Integer, String> mymap = new TreeMap<Integer, String>(); 

for example, without breaking all the rest of your code.

A much more minor advantage is that your IDE can show you the methods for the interface, rather than the (potentially much larger number of) methods for the implementation.

Comments

Your Answer

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.