Apache Commons Collections - MapIterator Interface



Overview

The JDK Map interface is pretty difficult to iterate as Iteration to be done on EntrySet or over the KeySet objects. MapIterator provides simple iteration over Map.

Interface Declaration

Following is the declaration for org.apache.commons.collections4.MapIterator<K,V> interface −

public interface MapIterator<K,V>
   extends Iterator<K>

Methods

The methods for MapIterator interface are as follows −

Sr.No. Method & Description
1

K getKey()

Gets the current key, which is the key returned by the last call to next().

2

V getValue()

Gets the current value, which is the value associated with the last key returned by next().

3

boolean hasNext()

Checks to see if there are more entries still to be iterated.

4

K next()

Gets the next key from the Map.

5

void remove()

Removes the last returned key from the underlying Map (optional operation).

6

V setValue(V value)

Sets the value associated with the current key (optional operation).

Methods Inherited

This interface inherits methods from the following interface −

  • java.util.Iterator.

Example - Printing key-value pairs using MapIterator Interface

CommonCollectionsTester.java

package com.tutorialspoint;

import org.apache.commons.collections4.IterableMap;
import org.apache.commons.collections4.MapIterator;
import org.apache.commons.collections4.map.HashedMap;

public class CommonCollectionsTester {
   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);
      }
   }
}

Output

The output is stated below −

key: 3
Value: Three
key: 5
Value: Five
key: 2
Value: Two
key: 4
Value: Four
key: 1
Value: One

Example - Modifying value during iteration using MapIterator Interface

CommonCollectionsTester.java

package com.tutorialspoint;

import org.apache.commons.collections4.IterableMap;
import org.apache.commons.collections4.MapIterator;
import org.apache.commons.collections4.map.HashedMap;

public class CommonCollectionsTester {
   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();
         iterator.setValue(value + "_");
      }
      System.out.println(map);
   }
}

Output

The output is stated below −

{3=Three_, 5=Five_, 2=Two_, 4=Four_, 1=One_}

Example - Removing value during iteration using MapIterator Interface

CommonCollectionsTester.java

package com.tutorialspoint;

import org.apache.commons.collections4.IterableMap;
import org.apache.commons.collections4.MapIterator;
import org.apache.commons.collections4.map.HashedMap;

public class CommonCollectionsTester {
   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();
         if(key == "1"){
            iterator.remove();
         }
      }
      System.out.println(map);
   }
}

Output

The output is stated below −

{3=Three, 5=Five, 2=Two, 4=Four} 
Advertisements