为什么Map不扩展Collection接口

编程入门 行业动态 更新时间:2024-10-22 10:53:33
本文介绍了为什么Map不扩展Collection接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

为什么 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接口

本文发布于:2023-06-07 15:21:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/568551.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:接口   Map   Collection

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!