Home Tutorial Java Iterator Java Map Iterate

 
 

Share on Google+Share on Google+
Java Map Iterate
Posted on: November 4, 2009 at 12:00 AM
Advertisement
In this section of tutorial we will learn how to use the Map with the Iterator interface. We will create an example to display the contents of the map using Iterator

  • Map is an Interface in the Collection Framework.
  • Map is implemented by its two sub classes
    • HashMap
    • Treemap
  • Map has no iterator method.
  • So use the entrySet() method.It returns the data as Set


Example of Java Map Iterate
import java.util.*;

public class map1 {
	public static void main(String[] args) {
		Map map = new TreeMap();
		char grade[] = { 'a', 'b', 'c', 'd' };
		int x = 1;

		for (char c : grade) {
			map.put(x, c);
			x++;
		}
		Set s = map.entrySet();
		Iterator i = s.iterator();
		while (i.hasNext()) {
			System.out.println(i.next());
		}
	}
}

Output :

1=a 2=b 3=c 4=d
Advertisement

Related Tags for Java Map Iterate:


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 4, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com