Map#replaceAll如果您使用的是Java 8+,则可以使用:
map.replaceAll((k, v) -> null);
Run Code Online (Sandbox Code Playgroud)
如果没有,循环Entrys可能是最简单的方法.从上面的链接,默认实现Map#replaceAll相当于:
for (Map.Entry<K, V> entry : map.entrySet())
entry.setValue(function.apply(entry.getKey(), entry.getValue()));
Run Code Online (Sandbox Code Playgroud)
其中function是参数,所以你可以这样做:
for (Map.Entry<K, V> entry : map.entrySet()) {
entry.setValue(null);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2881 次 |
| 最近记录: |