import java.util.Hashtable; import java.util.Enumeration; public class HTDemo{ public static void main(String []str){ //Creating new Hatshtable Hashtable ht = new Hashtable(); //Adding objects of Entry class to Hashtable ht.put("Emma Watson",new Entry("Emma Watson","9741631235")); ht.put("Hugh Jackman",new Entry("Hugh Jackman","7353421215")); ht.put("Eric Bana",new Entry("Eric Bana","9471164719")); ht.put("Steve Jobs",new Entry("Steve Jobs","9132135451")); //Enumerating elements of Hashtable displayElements(ht); ht.remove("Eric Bana"); System.out.println("===============After removal=============="); displayElements(ht); } static void displayElements(Hashtable htable){ Enumeration e = htable.keys(); String key; Entry ent; while(e.hasMoreElements()){ key = (String)e.nextElement(); ent = (Entry)htable.get(key); System.out.println("Name -> " + ent.getName()); System.out.println("Phone -> " + ent.getPhone()); System.out.println("-------------------------------"); } } } class Entry{ private String name; private String phone; Entry(){ name=""; phone=""; } Entry(String nm,String pn){ this.name = nm; this.phone = pn; } String getName(){ return this.name; } String getPhone(){ return this.phone; } void setName(String nm){ this.name = nm; } void setPhone(String pn){ this.phone = pn; } }
Sunday, January 16, 2011
Using Hashtable in Java
Labels:
Java
Subscribe to:
Post Comments (Atom)
Hi,
ReplyDeleteThanks for this Nice article just to add while discussing about HashMap its worth mentioning following questions which frequently asked in Java interviews now days like How HashMap works in Java or How get() method of HashMap works in JAVA very often. on concept point of view these questions are great and expose the candidate if doesn't know deep details.
Javin
FIX Protocol tutorial
Thanks Javin for your valuable feedback. I will try to post articles on important concepts in future.
ReplyDelete