鍍金池/ 教程/ Linux/ Apache通用集合MapIterator接口
Apache Commons Collections教程
Apache通用集合開發(fā)環(huán)境
Apache通用集合過濾對象
Commons Collections簡介
Apache通用集合聯(lián)合
Apache OrderedMap接口
Apache通用集合Bag接口
Apache通用集合安全空檢查
Apache通用集合差集
Apache通用集合包函關(guān)系
Apache通用集合BidiMap接口
Apache通用集合相交
Apache通用集合轉(zhuǎn)換對象
Apache通用集合合并與排序
Apache通用集合忽略Null
Apache通用集合MapIterator接口

Apache通用集合MapIterator接口

JDK Map接口很難作為迭代在EntrySetKeySet對象上迭代。 MapIterator提供了對Map的簡單迭代。下面的例子說明了這一點。

MapIterator接口示例

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);
   }
}

執(zhí)行上面示例代碼,得到以下結(jié)果 -

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_}