为什么不Map#removeAll(Collection<?>)? [英] why not Map#removeAll(Collection<?>)?

查看:953
本文介绍了为什么不Map#removeAll(Collection<?>)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么Java中的 Map 界面没有 removeAll(Collection<?> c)方法删除键,例如它有 map.remove(Object)

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

我知道我总是可以做< c $ c> map.keySet()。removeAll(..) ..但这是一个原因 Map 没有 removeAll()并鼓励我们使用 map.keySet()。removeAll(..)

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(..) ?

推荐答案

集合API的理念是尽可能小和简单。 Map上的集合视图允许您执行此操作,因此不需要额外的方法。

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.

keySet 方法会返回地图的视图

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

接口设计中更常见的问题:为什么接口X没有方便的方法Y?更详细地介绍了Martin Fowler对 MinimalInterface HumaneInterface

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.

这篇关于为什么不Map#removeAll(Collection&lt;?&gt;)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆