Java Map为什么不扩展Collection? [英] Why doesn't Java Map extend Collection?

查看:88
本文介绍了Java Map为什么不扩展Collection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Map<?,?>不是Collection<?>令我感到惊讶.

我认为,如果这样声明,那将是很有意义的:

I thought it'd make a LOT of sense if it was declared as such:

public interface Map<K,V> extends Collection<Map.Entry<K,V>>

毕竟,Map<K,V>

After all, a Map<K,V> is a collection of Map.Entry<K,V>, isn't it?

那么有充分的理由为什么不这样实现呢?

So is there a good reason why it's not implemented as such?

感谢Cletus提供最权威的答案,但是我仍然想知道为什么,如果您已经可以将Map<K,V>视为Set<Map.Entries<K,V>>(通过entrySet()),它不仅扩展了该接口.

Thanks to Cletus for a most authoritative answer, but I'm still wondering why, if you can already view a Map<K,V> as Set<Map.Entries<K,V>> (via entrySet()), it doesn't just extend that interface instead.

如果MapCollection,则有哪些元素?唯一合理的答案是键值对"

If a Map is a Collection, what are the elements? The only reasonable answer is "Key-value pairs"

是的,interface Map<K,V> extends Set<Map.Entry<K,V>>太棒了!

但这提供了非常有限的(并且不是特别有用的)Map抽象.

但是如果是这种情况,那么为什么接口要指定entrySet?某种程度上它必须是有用的(而且我认为为该职位辩护很容易!).

But if that's the case then why is entrySet specified by the interface? It must be useful somehow (and I think it's easy to argue for that position!).

您不能询问给定键所映射的值,也不能在不知道给定键所映射的值的情况下删除该键的条目.

You can't ask what value a given key maps to, nor can you delete the entry for a given key without knowing what value it maps to.

我并不是说这就是Map的全部内容!它可以并且应该保留所有其他方法(entrySet除外,现在已经多余了!)

I'm not saying that that's all there is to it to Map! It can and should keep all the other methods (except entrySet, which is redundant now)!

推荐答案

来自

为什么Map不扩展Collection?

这是设计使然.我们觉得 映射不是集合,并且 集合不是映射.因此,它 地图扩展几乎没有意义 收藏界面(或副界面) 反之亦然).

Why doesn't Map extend Collection?

This was by design. We feel that mappings are not collections and collections are not mappings. Thus, it makes little sense for Map to extend the Collection interface (or vice versa).

如果地图是集合,那么什么是 元素?唯一合理的答案 是键值对",但这是 提供了非常有限的(而不是 特别有用)地图抽象. 您不能问给定密钥的值是多少 映射到,也不能删除该条目 对于给定的键,却不知道是什么 它映射到的值.

If a Map is a Collection, what are the elements? The only reasonable answer is "Key-value pairs", but this provides a very limited (and not particularly useful) Map abstraction. You can't ask what value a given key maps to, nor can you delete the entry for a given key without knowing what value it maps to.

可以扩大收藏范围 地图,但这引发了一个问题: 有什么钥匙?真的没有 满意的答案,并强迫一个 导致界面不自然.

Collection could be made to extend Map, but this raises the question: what are the keys? There's no really satisfactory answer, and forcing one leads to an unnatural interface.

可以将地图视为( 键,值或对),以及这个事实 体现在三个收藏" 查看操作"(在Maps上(keySet, entrySet和值).虽然在 原则上,可以将列表视为 映射到元素的Map索引, 具有讨厌的特性 从列表中删除一个元素 更改与每个关联的密钥 元素在已删除元素之前. 这就是为什么我们没有地图视图 列表上的操作.

Maps can be viewed as Collections (of keys, values, or pairs), and this fact is reflected in the three "Collection view operations" on Maps (keySet, entrySet, and values). While it is, in principle, possible to view a List as a Map mapping indices to elements, this has the nasty property that deleting an element from the List changes the Key associated with every element before the deleted element. That's why we don't have a map view operation on Lists.

更新:我认为引言回答了大多数问题.值得强调的是有关条目集合的部分不是特别有用的抽象.例如:

Update: I think the quote answers most of the questions. It's worth stressing the part about a collection of entries not being a particularly useful abstraction. For example:

Set<Map.Entry<String,String>>

将允许:

set.add(entry("hello", "world"));
set.add(entry("hello", "world 2");

(假设创建一个Map.Entry实例的entry()方法)

(assuming an entry() method that creates a Map.Entry instance)

Map需要唯一键,因此会违反此键.或者,如果您在条目的Set上加上唯一键,那么从一般意义上说,它实际上并不是Set.这是Set,有更多限制.

Maps require unique keys so this would violate this. Or if you impose unique keys on a Set of entries, it's not really a Set in the general sense. It's a Set with further restrictions.

可以说Map.Entryequals()/hashCode()关系纯粹是关键,但即使这样也有问题.更重要的是,它真的增加了任何价值吗?一旦开始研究极端情况,您可能会发现这种抽象方法崩溃了.

Arguably you could say the equals()/hashCode() relationship for Map.Entry was purely on the key but even that has issues. More importantly, does it really add any value? You may find this abstraction breaks down once you start looking at the corner cases.

值得注意的是,HashSet实际上是作为HashMap实现的,而不是相反的.这纯粹是实现细节,但仍然很有趣.

It's worth noting that the HashSet is actually implemented as a HashMap, not the other way around. This is purely an implementation detail but is interesting nonetheless.

entrySet()存在的主要原因是简化遍历,因此您不必遍历键,然后查找键.不要将它作为Map应该是条目(imho)的Set的表面证据.

The main reason for entrySet() to exist is to simplify traversal so you don't have to traverse the keys and then do a lookup of the key. Don't take it as prima facie evidence that a Map should be a Set of entries (imho).

这篇关于Java Map为什么不扩展Collection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆