开发者

Typing convention for local collections in Java

I recently ran across a set of code which instantiated local maps as following:

HashMap<String, Object> theMap = new HashMap<String, Object>();

Typically, when I've seen HashMaps used (and used them myself), the local variables are simply Map (the interface), rather than being tied to开发者_StackOverflow中文版 the specific implementation. Obviously this is required if the Map could potentially be instantiated as various Map types (e.g. accepting a parameter). However, in the case of something like the above where it's defined and instantiated at the same point, is there an underlying reason to only use the interface type, or is it simply style/convention?


(I originally misunderstood the question based on the title, but I've included both type and variable conventions as both are interesting.)

What's important is that it's a map: something you look things up in. The rest is an implementation detail.

I would suggest giving it a semantic name, e.g.

Map<String, Object> nameToSessionMap = ...

... that way when you read the code, you'll know what the keys and values are meant to be.

As for the type of the variable - again, I'd typically use the interface rather than the implementation partly because it indicates I'm not using any members which are specific to the type. I don't want to emphasize the implementation in the code, usually... it means when I do care about the implementation, I can make that more obvious.


Declaring the object as a Map will allow the compiler to protect you from calling methods which are specific to HashMap. This will allow you to substitute another Map implementation in the future without worrying about having method calls which do not exist in the Map interface.


In general people use mostly Map to make the least amount of assumptions on the implementation.

It cannot be that the Classname is used for the additional methods as only clone() is added by HashMap, which has fallen in disuse (for good reasons).

What could be is that the map needs to be Serializable for one reason or another, and the plain Map interface does not extend it, but HashMap does implement it.


Even in this case, it keeps it generic. Coding to interface ensures you are using a Map and not a specific implementation of it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜