Home Tutorial Java Iterator Java HashMap iterator and example

 
 

Share on Google+Share on Google+
Java HashMap iterator and example
Posted on: November 3, 2009 at 12:00 AM
Advertisement
In this Java tutorial, we will see how to use the iterator interface with HashMap. We will create an example to display the contents of the HashMap.

  • Java HashMap Iterator is an interface. It keeps the data in the key and value form.
  • It is implemented by HashMap. hashMap doesnot have iterator method.
  • So use the entrySet() method to get the data in Set object form.
  • Set's all elements can be traversed by the Iterator.


Example of Java HashMap Iterator
import java.util.*;

public class hashmap {
	public static void main(String[] args) {
		HashMap hash = new HashMap();
		hash.put("roll", new Integer(12));
		hash.put("name", "jack");
		hash.put("age", 22);
		Set s = hash.entrySet();
		Iterator i = s.iterator();
		while (i.hasNext()) {
			System.out.println(i.next());
		}
	}
}

Output

roll=12 age=22 name=jack
Advertisement

Related Tags for Java HashMap iterator and example:


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.

Posted on: November 3, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com