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

查看:91
本文介绍了为什么Java 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> <$ c $的集合c> Map.Entry< K,V> ,不是吗?

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

感谢Cletus一个最权威的答案,但我仍然想知道为什么,如果你可以已经将地图< 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.


如果 Map Collection ,元素是什么?唯一合理的答案是键值对

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> 将是非常好的!

Exactly, interface Map<K,V> extends Set<Map.Entry<K,V>> would be great!


非常有限(而不是特别有用)映射抽象。

那么case为什么 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)!

推荐答案

Java Collections API设计常见问题




这是设计。我们认为
映射不是集合,
集合不是映射。因此,
对于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).

a Collection,什么是
元素?唯一合理的回答
是键值对,但是这个
提供了非常有限的(而不是
特别有用的)Map抽象。
你不能问一个给定的键
映射到什么值,也不能删除条目
给定的键,而不知道它映射到什么
值。

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上的三个Collection
视图操作(keySet,
entrySet和values)中。虽然它是
原则,可能查看列表为
a映射到元素的映射索引,
这有一个讨厌的属性
从列表中删除一个元素
更改与删除的元素之前的每个
元素相关联的键。
这就是为什么我们在列表上没有地图视图
操作。

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");

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

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

映射需要唯一键,否则会违反这个规则。或者如果你在 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.

可以说, equals() / hashCode()关系 Map.Entry 纯粹是关键,但即使有问题,更重要的是,它真的添加任何值吗?你可能会发现这个抽象分解,一旦你开始查找。

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 实际上是作为一个

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)的初步证据。

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天全站免登陆