Isn't it against Map to use the remove () method, but not removeall ()?

Possible duplicate:
why not Map # removeAll (Collection <? gt;)?

I just read why not Map # removeAll (Collection)? . I believe this was a “wrong question”, as a result of which the problem was resolved.

The two deletion methods for the card obviously have the following signature:

remove(K key); removeAll(Collection<K> keys); 

This would be consistent and clear, and there would be little to no explanation for him. On the map, you delete entries, and “deleting a key” means deleting its entry, since there are no values ​​without a key.

Now it could be argued (as indicated in relation to the related question) that there is no removeAll() , because the map interface is kept minimalistic, and you can simply use removeAll() in the map key set. The fact is that the same argument applies to remove() : you can use myMap.keySet().remove(someKey) instead of mymap.remove(someKey) .

Thus, if the interface should be Minimilastic and not 'Humane' , both methods are redundant; and if it is the other way around, both methods must exist.

What am I missing?

+4
source share
2 answers

Obviously, I respect Martin Fowler and all the people who discuss these topics, but I often doubt that “philosophical thinking” about software development brings significant achievements - and the discussion often ends with people making tough statements.

However, I think that the reason that removeAll() missing is not a "minimalist and humane interface" problem for the same reason as you: I see you can remove the key-value pair by acting on Set<K> returned by keySet() , so the remove(K key) method is also useless.

Perhaps the reason is something more pragmatic, like a question of semantics (I'm going to explain) or frequency of use - and therefore perceived usefulness.

For Collection<E> , removeAll(Collection<?> c) pretty simple to understand: it removes all elements of the collection that are also in c . Its dual addAll(Collection<? extends E> c) .

For a Map that is not a Collection , removeAll(...) can be defined for keys, values, or records, and this can lead to confusion (semantic problem).

So why Map.remove(K key) ? This is provided for convenience. Yes, the approach is a humane interface ; -)

Just kidding. I want to convey that, in my humble opinion, the rules in software development are set out after everything is done, and not earlier, therefore they are often post-hoc-justifications. Maybe Map.remove(K key) exists because someone felt the need to place it there, and not because of a 100% consistent, scientific, reasonable and wise approach to modeling.

They are going to close the question because no one noticed your remark about remove(K key) . I did.:)

+1
source

I think this is because remove() is an expensive operation. The implementation performs a binary and / or hash search, and then deletes the found entry. Therefore, the user should not blindly make massive strikes. He / she must accurately remove one by one, only if necessary.

If there was a method for effectively removing volume, given the integral hashes of several elements or something, then removeAll would exist.

+1
source

All Articles