quantros
asked on
Comparing Hash Tables and accessing values dynamically
Hi ,
I have 2 Hash Tables (hash1) and (hash2) , both has a set of values.
I need to compare them and retrieve the keys which are equal Dynamically.
For example if hash1 has 3 keys h1,h2,h3 and
hash2 has 3 keys h4,h5,h1.
I should be able to retrieve (access) the h1 key dynamically , without hard coding any keys or values or indexes (I need to set this key further in the application).
Any examples.
Thanks
I have 2 Hash Tables (hash1) and (hash2) , both has a set of values.
I need to compare them and retrieve the keys which are equal Dynamically.
For example if hash1 has 3 keys h1,h2,h3 and
hash2 has 3 keys h4,h5,h1.
I should be able to retrieve (access) the h1 key dynamically , without hard coding any keys or values or indexes (I need to set this key further in the application).
Any examples.
Thanks
What happens if key1(1) and key1(2) have different values?
ASKER
I am able to retrieve the keys but I am having issue iterating through
the keys and accessing the common Keys.
I added the code tht I used to loop through the Hash Tables and get the common keys.
(I a getting all the keys and values in both the Hash Tables)
Can you help me to do this "recursive iteration".
the keys and accessing the common Keys.
I added the code tht I used to loop through the Hash Tables and get the common keys.
(I a getting all the keys and values in both the Hash Tables)
Can you help me to do this "recursive iteration".
Enumeration p = hashXml.keys();
Object objp;
Enumeration q = hash.keys();
Object objq;
for(int h=0 ; h<hashXml.size();h++)
{
while (p.hasMoreElements())
{
objp = p.nextElement();
for(int g=0;g<hash.size();g++)
{
while (q.hasMoreElements())
{
objq = q.nextElement();
if(objp.equals(objq))
{
//set the values tht are common in both the Hash Tables hash and hashXml
chartNode.attribute(objp.toString()).setValue((hash.get(objq)).toString());
}
}
}
}
}
ASKER
Hi CEHJ,
There are few common keys among the 2 Hash Tables,
I need to access these common keys dynamically.
Nothing happens when key1(1) and key1(2) are different ,
I donot know the index of the common keys.(key1 of the hash1 may be equal to key3 of hash2)
We need to capture these keys.
There are few common keys among the 2 Hash Tables,
I need to access these common keys dynamically.
Nothing happens when key1(1) and key1(2) are different ,
I donot know the index of the common keys.(key1 of the hash1 may be equal to key3 of hash2)
We need to capture these keys.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
woow. CEHJ has a better solution..
Plz go vth CEHJ
Plz go vth CEHJ
ASKER
Hi CEHJ,
this is for the Hash Set (never used it) ,
Does this work for Hash Tables?? (I am using Hash Tables)
this is for the Hash Set (never used it) ,
Does this work for Hash Tables?? (I am using Hash Tables)
't1' and 't2' in my code are Hashtable
ASKER
Thanks CEHJ ,
It works but How do I loop through the "common" to get each of the values in
the HashSet.
cos I need to set these keys in
chartNode.attribute(common1).setValue(common1); (look a the code snippet)
It works but How do I loop through the "common" to get each of the values in
the HashSet.
cos I need to set these keys in
chartNode.attribute(common1).setValue(common1); (look a the code snippet)
Just iterate the Set:
for (Object key : common) {
Object value = t1.get(key);
}
ASKER
I have never used a Hashset ,
I am having a problem iterating the set can you elaborate
the for loop without using the foreach.
I need to just loop through the "Common" using the for loop to access each of the values.
I am having a problem iterating the set can you elaborate
the for loop without using the foreach.
I need to just loop through the "Common" using the for loop to access each of the values.
>>I have never used a Hashset ,
It's really no different than using Hashtable
>>I am having a problem iterating the set
What problem are you having? Please post your code
It's really no different than using Hashtable
>>I am having a problem iterating the set
What problem are you having? Please post your code
:-)
Iterate recursively to get the key that present in both the hashtable
thanks