- 共享收藏教程
- 共享收藏 - 主页
- Commons Collections - 概述
- Commons Collections - 环境设置
- Commons Collections - 包接口
- Commons Collections - BidiMap 接口
- Commons Collections - MapIterator 接口
- Commons Collections - OrderedMap 接口
- Commons Collections - 忽略 Null
- Commons Collections - 合并和排序
- Commons Collections - 转换对象
- Commons Collections - 过滤对象
- Commons Collections - 安全空支票
- 共享收藏 - 包容性
- 共享收藏 - 交叉口
- Commons Collections - 减法
- 下议院收藏 - 联盟
- 共享馆藏资源
- 共享收藏 - 快速指南
- Commons Collections - 有用的资源
- 共享收藏 - 讨论
Commons Collections - MapIterator 接口
JDK Map 接口的迭代非常困难,因为迭代是在 EntrySet 或 KeySet 对象上完成的。MapIterator 提供了对 Map 的简单迭代。下面的例子说明了同样的情况。
MapIterator 接口示例
MapIteratorTester.java 的示例如下 -
import org.apache.commons.collections4.IterableMap;
import org.apache.commons.collections4.MapIterator;
import org.apache.commons.collections4.map.HashedMap;
public class MapIteratorTester {
public static void main(String[] args) {
IterableMap<String, String> map = new HashedMap<>();
map.put("1", "One");
map.put("2", "Two");
map.put("3", "Three");
map.put("4", "Four");
map.put("5", "Five");
MapIterator<String, String> iterator = map.mapIterator();
while (iterator.hasNext()) {
Object key = iterator.next();
Object value = iterator.getValue();
System.out.println("key: " + key);
System.out.println("Value: " + value);
iterator.setValue(value + "_");
}
System.out.println(map);
}
}
输出
输出如下:
key: 3
Value: Three
key: 5
Value: Five
key: 2
Value: Two
key: 4
Value: Four
key: 1
Value: One
{3=Three_, 5=Five_, 2=Two_, 4=Four_, 1=One_}