1

I am using multiple hashtables for a program viz table1 table2 table3 etc. Each table has 3 key-value pairs. the keys are same with different values. Now in the UI of my program i have a comobobox to pick the table name from user. Once it is selected i am storing it in a variable and would like to use this variable to open the appropriate table.

Thanks Eric Coolman for this piece which i have edited here for my query.

Hashtable table1 = new Hashtable();

table1 .put("COL1", Double.toString(dvalue1));
table1 .put("COL2", value2);
table1 .put("COL3", value3);

Hashtable table2 = new Hashtable();

table2 .put("COL1", Double.toString(dvalue4));
table2 .put("COL2", value5);
table2 .put("COL3", value6);

// reading that value back:

double dvalue1 = Double.parseDouble((String)table1.get("COL1"));
double value6= Double.parseDouble((String)table2.get("COL3"));`

here how can i use a variable instead of table1 and table2? what is the syntax. I am new to java.

2 Answers2

4

First of, your structure looks like you are in object denial: what you've got as Hashtable object should probably be objects of a custom type with 3 fields (col1, col2 and col3; or more readable versions of that).

Next: you can simply use a third variable to switch on which object you read from:

Hashtable readFromMe;
if (someCondition)
  readFromMe=table1;
else
  readFromMe=table2;

Object o = readFromMe.get("COL3");

This code would read from table1 if someCondition is true and from table2 otherwise.

Also, a shorter version of the first 5 lines of the code above would be Hashtable readFromMe = someCondition ? table1 : table2;.

Community
  • 1
  • 1
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • Even shorter: `(someCondition? table1 : table2).get("COL3");` :) – Marko Topolnik Jul 09 '12 at 11:50
  • @MarkoTopolnik: yes, it's shorter, but I'd argue that it's a tad less readable and especially for a beginner it might be hard to comprehend (they tend to be used to calling methods on variables and not on arbitrary expressions). – Joachim Sauer Jul 09 '12 at 12:10
  • @ Sauer: Thanks but, I have more than 6 such tables therefore I would like to have a variable address the hashtable name.@ Topolnik: I am using codename one for developing an application which i learn does not yet support hashmaps. hence the use. – Rajesh Acharya Jul 09 '12 at 12:17
  • Are all of those tables the same structure? Then you might want an array or another map (`HashMap`). – Joachim Sauer Jul 09 '12 at 12:18
  • It's a common problem among beginners. At first it looks "simpler" to use a map for everything as opposed to creating new classes, but in the long run you'll run into many problems (for example I see you already to a lot of casting that wouldn't be necessary with a custom class). The term "object denial" is a name for this problem that I'm trying to get into common use ;-) – Joachim Sauer Jul 09 '12 at 12:30
1

Put your both the tables in another hash table and access the table you want using the key.

Hashtable<String, Hashtable> tables = new Hashtable<String, Hashtable>();
Hashtable<String, String> table1 = new Hashtable<String, String>();

table1 .put("COL1", Double.toString(dvalue1));
table1 .put("COL2", value2);
table1 .put("COL3", value3);

tables.put("table1", table1);


Hashtable<String, String> table2 = new<String, String> Hashtable();

table2 .put("COL1", Double.toString(dvalue4));
table2 .put("COL2", value5);
table2 .put("COL3", value6);

tables.put("table2", table2);

// reading that value back:

double dvalue1 = Double.parseDouble(tables.get("table1").get("COL1"));
double value6= Double.parseDouble(tables.get("table2").get("COL3"));`
Ramesh PVK
  • 15,200
  • 2
  • 46
  • 50
  • Thanks for this. I had a query on using nested hashtables which you answered before my asking. but since i have more than 6/7 tables i would like to know if the hashtable name can be called from a variable. – Rajesh Acharya Jul 09 '12 at 12:20
  • I did not get this "if the hashtable name can be called from a variable" ? – Ramesh PVK Jul 09 '12 at 12:29
  • I mean using a string variable to call the relevant hashtable. As i said i have a comobobox on my UI where the user selects the table. I can store this selection in a string variable and then use this variable to call the relevant table for getting the key value pairs. – Rajesh Acharya Jul 09 '12 at 12:35
  • Yes you should associate each string unique string variable to each table. – Ramesh PVK Jul 09 '12 at 12:36
  • I think my question is still unanswered. If, String var = table1; then how to use 'var' instead of 'table1'. in the above code. – – Rajesh Acharya Jul 09 '12 at 13:50
  • You cannot assign String var = table1; which is hashtable. You should name Hashtable variable as table1 and follow the same convention for keys in hashtable i.e, "table1". – Ramesh PVK Jul 10 '12 at 04:38
  • thanks for sticking around. i think i am not putting my question properly. In your code above, while reading the values (last 2 lines) "table1" & "table2" are called which are names of the respective hashtables. Can i use a string variable for this something like this: --same code-- String name = "tables"; String name1 = "table1"; String name2 = "table2"; double dvalue1 = Double.parseDouble(name.get(name1).get("COL1")); so that i still get the same result. hope i am clear. – Rajesh Acharya Jul 10 '12 at 05:44
  • But it doesn't in my case, double dvalue1 = Double.parseDouble((String)table1.get("COL1")); (this works) but String name = "table1"; double dvalue1 = Double.parseDouble(name.get("COL1")); this doesn't. There is red line below get saying cannot find symbol method get. I am using netbeans. Is the syntax correct. – Rajesh Acharya Jul 10 '12 at 06:15
  • String tablename = "table1";double dvalue1 = Double.parseDouble(tables.get(tablename).get("COL1")); – Ramesh PVK Jul 10 '12 at 06:18