Map vs Map<K,V>
I'd like to find out why UnmodifiableMap.decorate(Map map)
from Apache Commons operates on Map
and returns Map
instead of Map<K,V>
. What's the di开发者_运维技巧fference between these two approaches?
I believe the Apache Commons collections predate generics, and haven't been updated for them.
You may wish to look at this unofficial version which is a port of Apache Commons Collections to generics, or at Guava for an alternative library with support for other collections. (There are others, of course.)
I think it is because of some sort of backward compatibility. The Map<K,V>
can be used just in Java 5 and above.
Jon is probably right.
You may be able to cast to Map when it's retruned.
Map<K,V> myMap = (Map<K,V>)UnmodifiableMap.decorate(Map map);
精彩评论