Can we add Hashtable/HashMap to a vector

Showing Answers 1 - 9 of 9 Answers

Kanhaiya

  • Dec 13th, 2006
 

Yes we can do that. As Hashtable/HashMap are objects. so we can store that in vector. Thanks

  Was this answer useful?  Yes

penchalaiah

  • May 4th, 2007
 

Write a small code and show how it is possible

  Was this answer useful?  Yes

import java.util.*;
class HashtableinVector
 {
 public static void main(String args[]) throws Exception
  {
  Vector v=new Vector();
  Hashtable h1=new Hashtable();
  h1.put("Sachin", new Integer(75));
  h1.put("Dhoni", new Integer(68));
  h1.put("Yuvraj", new Integer(81));
  v.add(h1);

  Hashtable h2=new Hashtable();
  h2.put("Gilchrist", new Integer(74));
  h2.put("Ponting", new Integer(135));
  h2.put("Hussey", new Integer(42));
  v.add(h2);

  Hashtable h3=new Hashtable();
  h3.put("Pietersen", new Integer(25));
  h3.put("Collingwood", new Integer(64));
  h3.put("Bell", new Integer(56));
  v.add(h3);
  
  
  Enumeration e;
  ListIterator lit=v.listIterator();
  while(lit.hasNext())
   {
   e=((Hashtable)lit.next()).keys();
   while(e.hasMoreElements())
    System.out.println(e.nextElement());
   }
  }
 }

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions