The Artima Developer Community
Sponsored Link

Java Answers Forum
Hashtable

7 replies on 1 page. Most recent reply: Aug 27, 2003 3:10 AM by Draganka Tusheva

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 7 replies on 1 page
Maysoon

Posts: 64
Nickname: hm
Registered: Mar, 2002

Hashtable Posted: Apr 3, 2002 12:13 PM
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 ..


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Hashtable Posted: Apr 3, 2002 2:55 PM
Reply to this message Reply
It seems kind of redundant to have a Hashtable that contains Vectors. What exactly are you trying to accomplish? Since you are setting each value to the same Vector, I can't see what end you are trying to achieve.

Also, you may be abled to accomplish what you need with HashMap and, if necessary, ArrayList. If you call the HashMap's toString() and print the results, that may be what you are looking for.

Maysoon

Posts: 64
Nickname: hm
Registered: Mar, 2002

Re: Hashtable Posted: Apr 4, 2002 3:27 PM
Reply to this message Reply
hi Mr:

i put only an example to ask if i can store a hashtable that contains vectors in a file,but in fact
i have a list of users ,in which they will be the keys
to the Hashtable,then each key points to a vector,
this vectors contain classes ,these classes contains
data about the user messages (subject,from,file name)

then i will store this hashtable in a file of object
ohh,is it complex...!

but i use this model instead of using database
to speedup the program .


so tell me can i store this hashtable in a file
or not??
thank you very much

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Hashtable Posted: Apr 4, 2002 9:54 PM
Reply to this message Reply
Yes, Hashtable and HashMap are both Serializable, so you can store them to file. You should make sure the objects you are storing in the Hashtable are Serializable, too.

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: Hashtable Posted: Apr 5, 2002 2:55 AM
Reply to this message Reply
You may have to put you own implementation of the readObject / writeObject to improve the Serialization of the Object you wanna put into the Vector / HashSomething but it should be strainght forward.

Draganka Tusheva

Posts: 8
Nickname: tusheva
Registered: Aug, 2003

Hei Maysoon I have almost the same problems ... can you pls write Posted: Aug 20, 2003 7:08 AM
Reply to this message Reply
Hi I am new to the forum but I wanted to write to you because I am using the same idea as yours for something that I write...
So I want to create a hashtable that will store as a key Strings and as a value Vectors... So basically I have many classes that implement one interface that defines a common behaviour for all of them.Each of the class has four fields
sourceID, destinationID , sourceWeight ,destinationWeight
I want to make the hashtable so that the key will be the
sourceID and the vector that corresponds to this sourceID will contain the objects that have this sourceID as a field.The idea is that i will make on a later point search in all of the stuff. Now what I have done till now is to make the hashtable only when i pass only one argument to it namely something of type one of my classes .. But as I want to make my programm to be dynamic i was thinking of collecting every object that is created in a vector and after that searching this vector somehow and within some for loops making my program.. But it is all so complicated.Anyway if somebody have any idea of how this can happen I will be ver glad that one can share it with me... And what do you think
hope to hear something from you soon

Kishori Sharan

Posts: 211
Nickname: kishori
Registered: Feb, 2002

Re: Hashtable Posted: Aug 20, 2003 7:13 PM
Reply to this message Reply
Here is a working example of what you want. I have created this example just to demonstate that you can do it. You can fill in the details.
package test;
 
import java.util.Hashtable;
import java.util.Vector;
import java.io.*;
 
public class HashTableTest {
  private static String fileName = "persondata.dat";
  public static void main(String[] args) {
   storeTable();
   restoreTable();
  }
public static void restoreTable() {
    try {
      FileInputStream in = new FileInputStream(fileName);
      ObjectInputStream s = new ObjectInputStream(in);
      Hashtable ht = (Hashtable) s.readObject();
      System.out.println ( "Restored:" + ht) ;
    }
    catch (Exception e){
      System.out.println (e.getMessage()) ;
    }
 
 }
 
 public static void storeTable() {
   Hashtable ht = new Hashtable();
   Vector v1 = new Vector();
   Vector v2 = new Vector();
 
   v1.add(new Person (101, "John"));
   v1.add(new Person (102, "Mary"));
 
   v2.add(new Person (201, "Bill"));
   v2.add(new Person (202, "Ed"));
 
   ht.put("set1", v1);
   ht.put("set2", v2);
 try {
   FileOutputStream out = new FileOutputStream(fileName);
   ObjectOutputStream s = new ObjectOutputStream(out);
   s.writeObject(ht);
   s.flush();
   System.out.println("Stored:" + ht);
 }
 catch ( Exception e){
   System.out.println(e.getMessage());
 }
 }
}
 
class Person implements java.io.Serializable {
  private String name;
  private int id ;
 
 public Person ( int id, String name){
   this.id = id;
   this.name = name;
 }
  public String toString() {
    return name + "(" + id + ")";
  }
}
 

Draganka Tusheva

Posts: 8
Nickname: tusheva
Registered: Aug, 2003

Re: Hashtable Posted: Aug 27, 2003 3:10 AM
Reply to this message Reply
Thank you.I have done it by myself and I think that it looks ok.I appreciate your help very much.
Cheers

Flat View: This topic has 7 replies on 1 page
Topic: hi accesing database tthrough applet Previous Topic   Next Topic Topic: Customizing JFormattedTextField to create IP Address Field

Sponsored Links



Google
  Web Artima.com   

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