简体   繁体   English

为什么java.util.Map.values()允许您从返回的Collection中删除条目

[英]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. 特别是当存在重复值时,在该C​​ollection上调用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 . 作为示例用例,假设您有一个名为contactListMap<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. 特别是当存在重复值时,在该C​​ollection上调用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? 由于您可能想要删除值的合理原因,为什么不选择允许此操作?

  • Maybe there's a given value where you want to remove every instance of it from the Map . 也许有一个给定的值,你想从Map删除它的每个实例。
  • Maybe you want to trim out every third key/value pair for some reason. 也许你想出于某种原因削减每三个键/值对。
  • Maybe you have a map from hotel room number to occupancy count and you want to remove everything from the map where the occupancy count is greater than one in order to find a room for someone to stay in. 也许你有一张从酒店房间号码到入住人数的地图,你想要从地图中删除所有入住人数大于1的地图,以便找到一个可以留在的人的房间。
  • ...if you think about it more closely, there are plenty more examples like this... ......如果你仔细考虑一下,还有更多这样的例子......

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM