Java Hashtable Iterator


 

Java Hashtable Iterator

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

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

  • This collection belongs to Map interface family.
  • It puts the data in the key and value form.
  • Like hasmap, it has no iterator() method.
  • Use the entrySet() method.It returns the data in Set object form.
  • Set's all elements can be traversed by the Iterator.


Java Hashtable Iterator Example
import java.util.*;

public class hashtable {

	public static void main(String[] args) {
		Hashtable hastab = new Hashtable();
		hastab.put("a", "andrews");
		hastab.put("b", "bob");
		hastab.put("c", "christina");
		hastab.put("d", "dude");
		hastab.put("e", "era");
		Set s = hastab.entrySet();
		Iterator it = s.iterator();

		while (it.hasNext()) {
			System.out.println(it.next());
		}
	}
}

Output:

  • Share on Facebook
  • Tweet
  • Share on Google+
  • Post to Tumblr
  • Pin it
  • Add to Pocket
  • Submit to Reddit
  • Share on LinkedIn
  • Publish on WordPress
  • Save to Pinboard
  • Ads