We help IT Professionals succeed at work.

# Comparing Hash Tables and accessing values dynamically

on
126 Views
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
Comment
Watch Question

## View Solution Only

Commented:
Use hashtable.keys() function to get the keys of both the hasttable.

Iterate recursively to get the key that present in both the hashtable

thanks
CERTIFIED EXPERT
Top Expert 2016

Commented:
What happens if key1(1) and key1(2) have different values?

Commented:
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".
``````

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());

}
}
}
}
}
``````

Commented:
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.
CERTIFIED EXPERT
Top Expert 2016
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)

Commented:
woow. CEHJ has a better solution..

Plz go vth CEHJ

Commented:
Hi CEHJ,
this is for the Hash Set (never used it) ,
Does this work for Hash Tables?? (I am using Hash Tables)
CERTIFIED EXPERT
Top Expert 2016

Commented:
't1' and 't2' in my code are Hashtable

Commented:
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)
CERTIFIED EXPERT
Top Expert 2016

Commented:
Just iterate the Set:
``````for (Object key : common) {
Object value = t1.get(key);
}
``````

Commented:
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.
CERTIFIED EXPERT
Top Expert 2016

Commented:
>>I have never used a Hashset ,

It's really no different than using Hashtable

>>I am having a problem iterating the set

CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

###### Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

Deciding to stick with EE.

Mohamed Asif

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
###### Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Connect with Certified Experts to gain insight and support on specific technology challenges including:

• Troubleshooting
• Research
• Professional Opinions
Unlock the solution to this question.