Home Tutorial Java Iterator Java Hashmap Iterator

 
 

Java Hashmap Iterator
Posted on: November 3, 2009 at 12:00 AM
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()) {
			System.out.println(it.next());
		}
	}
}

Output

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

Related Tags for Java Hashmap Iterator :


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.