为什么 java.util.Map interface扩展 java.util.Collection 接口?不是 java.util.Map 一组键值对?
Why doesn't java.util.Map interface extend the java.util.Collection interface? Isn't a java.util.Map a collection of Key-Value pairs?
推荐答案集合采用一个值的元素。地图假设键/值对的条目。它们可以被设计为重用相同的公共接口,然而它们实现的一些方法是不兼容的,例如
Collection assume elements of one value. Map assumes entries of key/value pairs. They could have been engineered to re-use the same common interface however some methods they implement are incompatible e.g.
Collection.remove(Object) - removes an element. Map.remove(Object) - removes by key, not by entry.您可以将地图建模为条目集合,这是地图.entrySet()会。
You could model a Map as a collection of entries, which is what Map.entrySet() does.
有一些共同的方法; size(), isEmpty(), , putAll / addAll(),但是这些不太可能作为独立的接口具有很大的价值。 (可以使用Map.entrySet())
There are some methods in common; size(), isEmpty(), clear(), putAll/addAll() but these are unlikely to have much value as a stand alone interface. (Again Map.entrySet() can be used instead)
更多推荐
为什么Map不扩展Collection接口
发布评论