Hi ,
I have implemented the Hashtable and enumeration concepts in java.The program is executed fine but i have a doubt.When i try to print the keys and elements in the Hashtable by using enumeration ,it start prints from the second key and second element.
Did any one know why it should not start printing from the first key and element.

import java.io.*;
import java.util.Hashtable;
import java.util.Enumeration;
class Hashtable_Program
{
	public static void main(String args[])throws IOException
	{
		System.out.println("Sample hash table program functionlaity");
		Hashtable Sample_Table=new Hashtable();
		int Table_Value;
		Sample_Table.put("First",1);
		Sample_Table.put("Second",2);
		Sample_Table.put("Third",3);
		System.out.println("The size of the hash table is "+Sample_Table.size());
		//Enumeration
		Enumeration e=Sample_Table.keys();
		while(e.hasMoreElements()){
			System.out.println("The hashtable keys  are "+e.nextElement());
		}
		e=Sample_Table.elements();
		while(e.hasMoreElements()){
			System.out.println("The hashtable elements are "+e.nextElement());
		}
		
	}
}

output:
The size of the hash table is 3
The hashtable keys are Second
The hashtable keys are Third
The hashtable keys are First
The hashtable elements are 2
The hashtable elements are 1
The hashtable elements are 3


Thank you,
prem

If you need predictable iteration order, use a hashtable implementation which supports the same. Drop HashTable in favour of LinkedHashMap.

Edited 6 Years Ago by ~s.o.s~: n/a

If you need predictable iteration order, use a hashtable implementation which supports the same. Drop HashTable in favour of LinkedHashMap.

Which functionality supports for hashtable can u spcify it clearly.I dont want to use LinkedHashMap.

The functionality you require is not present in HashTable (predictable iteration order; i.e. iterating over keys in the order which they were inserted) hence you'd have to use LinkedHashMap. If you are feeling adventurous, you can extend HashTable to mimic LinkedHashMap but that would be *really* unwieldy and not recommended.

BTW, why is it that you don't want to use a LinkedHashMap?

hi
Thanks for your reply .i will try to use linked hashmap in my programs.

Prem2

change the key-value pair syntax boss...
objname.put("key","value");
.. then u will get exact o/p..

@satyarao: I think after one year the OP has either solved this or given up, regardless of the value or otherwize of your suggestion.

This question has already been answered. Start a new discussion instead.