The Artima Developer Community
Sponsored Link

Java Answers Forum
Hashtable

1 reply on 1 page. Most recent reply: Apr 4, 2002 7:21 PM by steve strongheart

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Maysoon

Posts: 64
Nickname: hm
Registered: Mar, 2002

Hashtable Posted: Apr 3, 2002 10:56 AM
Reply to this message Reply
Advertisement
hi all..

i have a Hashtable in which each key of it points to
a vector, i want to save this hashtable that
contains several vectors, can i do something like this:


ObjectOutputStream hash = new ObjectOutputStream
(new FileOutputStream("file.text"));

Hashtable h=new Hashtable(6);
Vectror v1=new Vector(2,2);

v1.addElement("1");
v1.addElement("2");
v1.addElement("3");

h.pu t("he",v1);
h.put("she",v1);
h.put("me",v1);

hash.writeObject(h);

please help ..


steve strongheart

Posts: 12
Nickname: stronghear
Registered: Apr, 2002

Re: Hashtable Posted: Apr 4, 2002 7:21 PM
Reply to this message Reply
your Hashtable h has three copies of v1

(currently h.get("he").equals(X) returns true for "me" and "she")

the description of the situation suggests that youy want h to have three vectors, each with it's own elements.
v1=new Vector(2,2);
v2= new Vector(2,2);
v3= new Vector(2,2);
 
v1.add("hi");
v1.add("bye");
v1.add("yo");
 
v2.add("bike");
v2.add("bus");
v2.add("car");
 
v3.add("sis");
v3.add("dad");
v3.add("mom");
 
h.pu t("he",v1);
h.put("she",v2);
h.put("me",v3);
try
{
  h.writeObject(new ObjectOutputStream
(new FileOutputStream("ht.oof"))
}catch(Exception XX){XX.printStackTrace(); }


reading back
HashTable h2=null;
try
{
  InputStream in = new ObjectInputStream(new FileInputStream("ht.oof")); // rem use same filename ... i use a 'custom' extension ".oof" (ObjectOutFile) 
 
  try
  {
    h2= (HashTable) in.readObject();
  } catch(ClassCastExcption ccx){}
}catch(IOException iox ){}
 
Vector vhe,vshe =null,vme;
// Enumeration keys = h2.keys(); -if keys were unknown.
  try
  {
    vshe=(Vector) h2.get("she");
  } catch(ClassCastExcption ccx){}
  k-0; 
  while (k<vshe.size()) System.out.println(vshe.get(k++).toString() );
 

Flat View: This topic has 1 reply on 1 page
Topic: PixelGrabber and Netscape Previous Topic   Next Topic Topic: End of file

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use