0

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

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by prem2
0

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.

Edited by musthafa.aj: n/a

0

Why should not we use like the below statement?

Iterator iobj=hobj.iterator();

Using enumeration

Enumeration eobj=eobj.elements();
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.