The Artima Developer Community
Sponsored Link

Java Answers Forum
help with testing a hashtable

1 reply on 1 page. Most recent reply: Jan 27, 2003 11:26 AM by Matt Gerrans

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
ben

Posts: 57
Nickname: loftty
Registered: Oct, 2002

help with testing a hashtable Posted: Jan 27, 2003 2:09 AM
Reply to this message Reply
Advertisement
hello everyone please help, as i have been stuck on this problem for ages.
I have this code below, that test to see what other values are equal st1, and once it has found them, get there key values and put them into arraylist, and remove the values that i have already tested.
But my problem is how the heck can i now test the next value that is in the hash table. for example
test 1: test st1 get the keys that match st1 put them into a arraylist and then remove the values from the hashtable.(i can do the first test).
this is my problem the next tests.
loop though somehow.
test 2: test st2 if it has already been used test st3, if st2 has not been use get the keys that are equal to st2 and put them into the arraylist and remove the values from the hashtable.
and keep testing each value in the hashtable untill all of the values have been tested.
could someone please help me
thanks for your time
ben

import java.io.*;
import java.util.*;
import java.lang.*;
import java.sql.*;
public class has21 {

public static void main(String[] args) {
ArrayList arr = new ArrayList();
Hashtable hash = new Hashtable();
String a = "firstname"; //column names
String b = "lastname";
String c = "id";
String d = "adress1";
String k = "phone";
String f = "county";
String g = "post code";
String h = "country";
String i = "goods in";
String j = "goods out";
String st1 = "cust";
String st2 = "cust"; //table names
String st3 = "details";
String st4 = "details";
String st5 = "item";
String st6 = "details";
String st7 = "item";
String st8 = "item";
String st9 = "item";
String st10 = "cust";
hash.put(a, st1);
hash.put(b, st2);
hash.put(c, st3);
hash.put(d, st4);
hash.put(k, st5);
hash.put(f, st6);
hash.put(g, st7);
hash.put(h, st8);
hash.put(i, st9);
hash.put(j, st10);


Enumeration e = hash.keys();
while (e.hasMoreElements()) {
String tempkey = (String) e.nextElement();
String tempvalue = (String) hash.get(tempkey);
if(tempvalue.equals(st1)) {
arr.add(tempkey);
hash.remove(tempkey);

}

}
System.out.println(hash);


}

}


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: help with testing a hashtable Posted: Jan 27, 2003 11:26 AM
Reply to this message Reply
Hmm. I don't understand what you are trying to do. Nevertheless, I'll offer some suggestions.

- First of all, you probably should prefer a HashMap, unless you really need the synchronization of the Hashtable.

- It looks like you are trying to use the map as a database table. It is easy to imagine problems with what you are doing because each key in a map must be unique. So, if you have rows that have firstname of "Bob" they will stomp each other. Maybe you are intending only to read one row and flush the map, but that will even fail on the case with someone named "Thomas Thomas" or "James James."

- I'm not sure what you are trying to accomplish, but maybe you might consider creating a CustomerInfo object, or something like that and simply have an array list of those.

- Please use the [java] and [/java] tags when posting code!

Flat View: This topic has 1 reply on 1 page
Topic: validation checks Previous Topic   Next Topic Topic: RMI Vs Servlet-Http-Servlet

Sponsored Links



Google
  Web Artima.com   

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