- Commons Collections 教程
- Commons Collections - 主頁
- Commons Collections - 概述
- Commons Collections - 環境設定
- Commons Collections - Bag 介面
- Commons Collections - BidiMap 介面
- Commons Collections - MapIterator 介面
- Commons Collections - OrderedMap 介面
- Commons Collections - 忽略空值
- Commons Collections - 合併 & 排序
- Commons Collections - 轉換物件
- Commons Collections - 篩選物件
- Commons Collections - 安全空值檢查
- Commons Collections - 包含
- Commons Collections - 交集
- 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_}
廣告