18

Why doesn't the Map interface in Java have a removeAll(Collection<?> c) method to remove keys, like it has map.remove(Object)?

I know I can always do map.keySet().removeAll(..) .. but is this a reason that Map doesn't have removeAll() and encourages us to go with map.keySet().removeAll(..) ?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
Premraj
  • 7,802
  • 8
  • 45
  • 66
  • 1
    Somehow [related](http://stackoverflow.com/questions/2651819/why-doesnt-java-map-extends-collection). – Buhake Sindi Apr 28 '11 at 10:33
  • 2
    if you remove key then doesn't value and entry gets removed? and if remove() takes a key then its variant also will take keys – Premraj Apr 28 '11 at 10:38
  • @Bivas: since the question talks about using `map.keySet()` I presume the intent is of removing map entries by specifying a collection of keys – MarcoS Apr 28 '11 at 10:40
  • 2
    @Bivas: I don't get your objection. There is an existing method `remove(Object key)` that is documented as "Removes the mapping for a key from this map if it is present". How could the convenient method `removeAll(Collection keys)` be ambiguous? It would do what `removeAll()` always does, i.e. perform a `remove(key)` for each element `key` that is contained in the collection `keys`. No doubt that the missing `removeAll()` has lead to thousands of unnecessary for-loops in production code. (Yes, I know about the `keySet().removeAll()` solution. People don't use that a lot). – jschreiner Mar 20 '14 at 15:17

2 Answers2

17

The philosophy behind the collections APIs is to be as small and simple as possible. The Collection views on Map allow you to perform this operation already, so there is no need for an extra method.

The keySet method returns a view of the Map. Operations on the key set are reflected on the map.

The more general question on interface design: Why doesn't interface X have convenient method Y? is addressed in more depth by Martin Fowler's discussion of MinimalInterface vs HumaneInterface.

z7sg Ѫ
  • 3,153
  • 1
  • 23
  • 35
  • are you referring to map.getKeySet().removeAll(..) ? – Premraj Apr 28 '11 at 10:41
  • 1
    That and the method name doesn't have to explain what it is doing which means it can't be simple and concise. `removeAllByKey` or something - eurgh. – Tom Hawtin - tackline Apr 28 '11 at 10:41
  • 1
    but Map already has remove which takes key and not removeByKey or something – Premraj Apr 28 '11 at 10:48
  • @Falcon yes, i've edited it. Hope this helps. @Tom And I think it's important to emphasize that Collection != Map. They are different beasts, and I have often seen them conflated in Q/As here. – z7sg Ѫ Apr 28 '11 at 10:49
  • @Falcon The remove method in map means 'remove map entry with key' X, perhaps it should have been called removeKey.. – z7sg Ѫ Apr 28 '11 at 10:51
  • yes but since it is called remove so can we have removeAll(Collection> keys) ? – Premraj Apr 28 '11 at 11:00
1

Because Map is not Collection, not extends Collection interface. Maps implementations USE collection interface to provide they own functionallity.

Think about situation like this:

  • you have Map with removeAll(..) method.
  • vou call this method and map removes...
  • so what they should remove? Keys, values or pairs - entries - key:value?

Map can provide methods:

  • removeAllKeys() - parameter is collection of keys
  • removeAllValues() - parameter is collection of values
  • removeAllEntries() - parameter is collection of pair and remove entry if only value is mapped by key. If in map exist value with diffrend key or vice-versa then that entry isn't removed

but in this case you have three methods not one.

So put removeAll method to Map interface is not clear to understand wich types of objects should be check and remove - keys, value, both or pairs.

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
Koziołek
  • 2,791
  • 1
  • 28
  • 48
  • 1
    I seriously have a question - does remove key shouldn't remove value and entry ? what's the behavior for current remove method? In any mappings ever it will make sense to remove key without value? – Premraj Apr 28 '11 at 12:05
  • in this case you should write in java doc somethog like removeAll should get collection of keys as parameter. And of course id should work fine. The other hand removeAll doesn't say clearly what you remove. Methods should has names that say what method do. removeAll in Map interface not say this. – Koziołek Apr 28 '11 at 12:25
  • 1
    This is a moot point. Removing a map key means removing the entry, there are no keyless value. That's what remove() does and that's what removeAll(Collection keys) would do. – einpoklum Jan 13 '13 at 08:30