且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么Map不扩展Collection接口

更新时间:2023-01-14 12:41:45

集合采用一个值的元素。地图假设键/值对的条目。它们可以被设计为重用相同的公共接口,然而它们实现的一些方法是不兼容的,例如

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)