Share on Google+Share on Google+

Java Hashmap Iterator

In this section of tutorial we will learn how to use the HashMap with the Iterator interface. We will create an example to display the contents of the hashMap using Iterator

  • Java HashMap Iterator is a collection class. It implements the Map interface.
  • It keeps the data in the key and value form.
  • Java HashMap has no iterator method.
  • So use the entrySet() method.It returns the data in Set object form.
  • Set's all elements can be traversed by the Iterator.

Java Hashmap Iterator Example
import java.util.*;
public class hashmap1 {
	public static void main(String[] args) {
		HashMap map = new HashMap();
		map.put("a", "Apple");
		map.put("b", "Boy");
		map.put("c", "cat");
		map.put("d", "Dog");
		Set s = map.entrySet();
		Iterator it = s.iterator();
		while (it.hasNext()) {


d=Dog b=Boy c=cat a=Apple


Posted on: November 3, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.