I need to store some names in Vector and then I want to convert them to HashTable in Java. I don’t understand the exact concept of vectors in Java. Please help. I created a vector and did something like this;
import java.util.*;
class VectorHash{
public static void main(String [] args){
Vector v = new Vector();
Hashtable table=new Hashtable();
v.addElement("Australia");
v.addElement("Brazil");
v.addElement("India");
v.addElement("US");
v.addElement("UK");
table.addElements(v);
for (Enumeration e = table.elements();table.hasMoreElements();){
System.out.println(e.nextElement());
}
}}
Its not working.
Hashtables maps keys to values (each element is a pair key-value)
http://docs.oracle.com/javase/6/docs/api/java/util/Hashtable.html
Vector is a list of values (each element is a value)
http://docs.oracle.com/javase/6/docs/api/java/util/Vector.html
You could convert from Vector to Hashtable, setting all the values to any non-null object, but I don’t know why you want to do that.