Iterating over and deleting from Hashtable in Java(Java中的Hashtable迭代和删除)
问题描述
我在 Java 中有一个 Hashtable,想要遍历表中的所有值并在迭代时删除特定的键值对.
I have a Hashtable in Java and want to iterate over all the values in the table and delete a particular key-value pair while iterating.
如何做到这一点?
推荐答案
您需要使用显式 java.util.Iterator 来迭代 Map 的条目设置而不是能够使用 Java 6 中可用的增强的 For 循环语法.以下示例遍历 Integer、String 的 Map对,删除 Integer 键为 null 或等于 0 的任何条目.
You need to use an explicit java.util.Iterator to iterate over the Map's entry set rather than being able to use the enhanced For-loop syntax available in Java 6. The following example iterates over a Map of Integer, String pairs, removing any entry whose Integer key is null or equals 0.
Map<Integer, String> map = ...
Iterator<Map.Entry<Integer, String>> it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, String> entry = it.next();
// Remove entry if key is null or equals 0.
if (entry.getKey() == null || entry.getKey() == 0) {
it.remove();
}
}
这篇关于Java中的Hashtable迭代和删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java中的Hashtable迭代和删除
基础教程推荐
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
