Doubt on Hashtable inside the Hashtable | Selenium Forum
M
Posted on 09/10/2015
My code for hashtable inside the hashtable is -

import java.util.Hashtable;


public class test_hashtable {

public static void main(String[] args) {
Hashtable<String, String> table1=new Hashtable <String, String>();
table1.put("City1", "Bangalore");
table1.put("City2", "Pune");
table1.put("City3", "Delhi");

Hashtable<String, String> table2=new Hashtable <String, String>();
table2.put("PIN1", "12345");
table2.put("PIN1", "56798");
table2.put("PIN1", "67895");
table2.put("PIN1", "01234");

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

table.put("name", table1);
table.put("ZIP", table2);

System.out.println(table.get("name"));
System.out.println(table.get("ZIP"));




}

}

I am getting the output displayed as : {City3=Delhi, City2=Pune, City1=Bangalore}
{PIN1=01234}

What could be the reason?

M
Replied on 09/10/2015

[quote:17qzqrfu]Hashtable<String, String> table2=new Hashtable <String, String>();
table2.put("PIN1", "12345");
table2.put("PIN1", "56798");
table2.put("PIN1", "67895");
table2.put("PIN1", "01234");[/quote:17qzqrfu]

you have put the same key.

so, "12345" is replaced by "56798" and so on.

so thats why the output is {PIN1=01234}

use this
[quote:17qzqrfu]Hashtable<String, String> table2=new Hashtable <String, String>();
table2.put("PIN1", "12345");
table2.put("PIN2", "56798");
table2.put("PIN3", "67895");
table2.put("PIN4", "01234");[/quote:17qzqrfu]


M
Replied on 10/10/2015

[quote="qtpselenium.supp0rt@gmail.com":akywb31v][quote:akywb31v]Hashtable<String, String> table2=new Hashtable <String, String>();
table2.put("PIN1", "12345");
table2.put("PIN1", "56798");
table2.put("PIN1", "67895");
table2.put("PIN1", "01234");[/quote:akywb31v]

you have put the same key.

so, "12345" is replaced by "56798" and so on.

so thats why the output is {PIN1=01234}

use this
[quote:akywb31v]Hashtable<String, String> table2=new Hashtable <String, String>();
table2.put("PIN1", "12345");
table2.put("PIN2", "56798");
table2.put("PIN3", "67895");
table2.put("PIN4", "01234");[/quote:akywb31v][/quote:akywb31v]

Thank you. Now, after chaning the key to PIN1,PIN2,PIN3,PIN4 - my output is -
{City3=Delhi, City2=Pune, City1=Bangalore}
{PIN4=01234, PIN3=67895, PIN2=56798, PIN1=12345}

Where as in your example, I don't see the keys r getting displayed. Only the value is getting displayed. Also, It is only one value is getting displayed for the same syntax what I have used above. Please help me in understanding this. Thank you.


M
Replied on 11/10/2015

[quote:1e4e76qs]Where as in your example, I don't see the keys r getting displayed. Only the value is getting displayed. Also, It is only one value is getting displayed for the same syntax what I have used above. Please help me in understanding this. Thank you.[/quote:1e4e76qs]

the actual usage of hash tables is that you call value by key.

but if you want the keys for some reason this is it.
http://www.tutorialspoint.com/java/util/hashmap_keyset.htm