Java Map iterator with example


 

Java Map iterator with example

In this tutorial we will see how to use the Java Map iterator interface with ArrayList. We will create an example to display the contents of the ArrayList.

In this tutorial we will see how to use the Java Map iterator interface with ArrayList. We will create an example to display the contents of the ArrayList.

  • Java Map Iterator is an interface. It keeps the data in the key and value form.
  • It is implemented by HashMap, Tree Map.
  • Map has no iterator method.
  • So use the entrySet() method to get the data in Set object form.
  • Use the entrySet() method to get the data in Set object form.


Java Map Iterator with Example
import java.util.*;

public class map {
	public static void main(String[] args) {
		Map map = new TreeMap();
		map.put("a", new Integer(1000));
		map.put("b", 2000);
		map.put("c", new Integer(3000));
		map.put("d", new Integer(4000));

		Set s = map.entrySet();
		Iterator i = s.iterator();
		while (i.hasNext()) {
			System.out.println(i.next());
		}
	}
}

Output

a=1000 b=2000 c=3000 d=4000

Ads