Hi ,
I am having a hashtable which consists of some keys and values.I can able to get the information from the hashtable using iterator or Enumeration.

import java.util.*;
import java.util.Hashtable.*;
import java.util.Iterator.*;
import java.util.Enumeration.*;
import java.io.*;
class ex_hashtable
{
	public static void main(String args[])throws IOException{
	Hashtable<Integer,String> hobj=new Hashtable<Integer,String>();
	hobj.put(1,"one");
	hobj.put(2,"two");
	System.out.println("The size of the hashtable size is "+hobj.size());
	Iterator iobj=hobj.keySet().iterator();
	while(iobj.hasNext()){
		System.out.println("hai");
		iobj.next();
		}
	}
}

In Iterator declaration.
What is the purpose of using the keySet() in the iterator?
What is keySet()?

Iterator iobj=hobj.keySet().iterator();

Thank you,

With Regards,
prem

Recommended Answers

All 2 Replies

KeySet which returns all the key values stored in hashmap.

from the key you can get corresponding value for that key from the hashmap or hashtable.

try to find difference between keyset and map.entry.

Why should not we use like the below statement?

Iterator iobj=hobj.iterator();

Using enumeration

Enumeration eobj=eobj.elements();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.