[英]Why does java.util.Map.values() allow you to remove entries from the returned Collection
Why does java.util.Map.values()
allow you to delete entries from the returned Collection when it makes no sense to remove a key value pair based on the value? 为什么
java.util.Map.values()
允许您从返回的Collection中删除条目,因为根据值删除键值对是没有意义的? The code which does this would have no idea what key the value(and hence a key) being removed is mapped from. 执行此操作的代码将不知道要删除的值(以及因此的键)是从哪个键映射的。 Especially when there are duplicate values, calling remove on that Collection would result in an unexpected key being removed.
特别是当存在重复值时,在该Collection上调用remove将导致删除意外的键。
it makes no sense to remove a key value pair based on the value
根据值删除键值对是没有意义的
I don't think you're being imaginative enough. 我认为你没有足够的想象力。 I'll admit there probably isn't wide use for it, but there will be valid cases where it would be useful.
我承认可能没有广泛使用它,但会有一些有用的有效案例。
As a sample use case, say you had a Map<Person, TelephoneNumber>
called contactList
. 作为示例用例,假设您有一个名为
contactList
的Map<Person, TelephoneNumber>
。 Now you want to filter your contact list by those that are local. 现在,您希望按本地搜索联系人列表进行过滤。
To accomplish this, you could make a copy of the map, localContacts = new HashMap<>(contactList)
and remove all mappings where the TelephoneNumber
starts with an area code other than your local area code. 为此,您可以制作地图的副本
localContacts = new HashMap<>(contactList)
并删除所有使用区域代码以外的区域代码开始的TelephoneNumber
映射。 This would be a valid time where you want to iterate through the values
collection and remove some of the values: 这将是您想要遍历
values
集合并删除一些values
的有效时间:
Map<Person, TelephoneNumber> contactList = getContactList();
Map<Person, TelephoneNumber> localContacts = new HashMap<Person, TelephoneNumber>(contactList);
for ( Iterator<TelephoneNumber> valuesIt = localContacts.values().iterator(); valuesIt.hasNext(); ){
TelephoneNumber number = valuesIt.next();
if ( !number.getAreaCode().equals(myAreaCode) ) {
valuesIt.remove();
}
}
Especially when there are duplicate values, calling remove on that Collection would result in an unexpected key being removed.
特别是当存在重复值时,在该Collection上调用remove将导致删除意外的键。
What if you wanted to remove all mappings with that value? 如果要删除具有该值的所有映射,该怎么办?
It has to have a remove
method because that's part of Collection
. 它必须有一个
remove
方法,因为它是Collection
的一部分。 Given that, it has the choice of allowing you to remove values or throwing an UnsupportedOperationException
. 鉴于此,它可以选择允许您删除值或抛出
UnsupportedOperationException
。 Since there are legitimate reasons that you might want to remove values, why not choose to allow this operation? 由于您可能想要删除值的合理原因,为什么不选择允许此操作?
Map
. Map
删除它的每个实例。 In short: there are plenty of situations where this might be useful and implementing it doesn't harm anyone who doesn't use it, so why not? 简而言之:在很多情况下,这可能是有用的,实施它不会伤害任何不使用它的人,为什么不呢?
The Collection
returned is a special Collection
, and its semantics are such that it knows how values in it relate back to the Map
it came from. 返回的
Collection
是一个特殊的Collection
,它的语义使得它知道它中的值如何与它来自的Map
有关。 The javadoc indicates what Collection
operation the returned collection supports. javadoc指示返回的集合支持的
Collection
操作。
I think there is quite often a use for removing a value based on a key; 我认为基于密钥删除值通常是有用的; other answers show examples.
其他答案显示例子。 Given that, if you want to remove a certain value, why would you only want one particular key of it removed?
鉴于此,如果您想删除某个值,为什么您只想删除它的一个特定键? Even if you did, you'd have to know which key you wanted to remove (or not, as the case may be), and then you should just remove it by key anyway.
即使你这样做了,你也必须知道要删除哪个密钥(或者不是,视情况而定),然后你应该通过密钥将其删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.