Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

Comparing entries in a Map

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi team,

my requirement is i have two hash table and i want to compare two hash table based on specify key. From below example i want to compare two hashtable based on "ID" and key - is ID, Name, Sal also value is - 101,102,103 like wise. Please share your comments with snippets code.

Example:

HashTable 1

ID    Name  Sal
101  ABC    $100
102  CDE    $200
103  EFH    $300

HashTable 2

ID    Name  Sal
101  XYZ    $100
102  YUD    $200

Thank you.
 
Marshal
Posts: 68917
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I think your question is different enough from the old question to merit a th‍read of its own. I can organise that.
Please explain what you are comparing. Your examples weren't clear, I am afraid.
 
AvinashS Sinha
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank for your reply..

Key is Id, Value is - (101,102,103),
Key is Name, value is -  (ABC, BCD)
same for rest in two hash table.

I want to compare two hash table based on ID and find out the common and unique data.
 
Campbell Ritchie
Marshal
Posts: 68917
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am afraid that doesn't tell me anything helpful. Do you want to put the entries into sets or something?
 
Marshal
Posts: 25452
65
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You go through one of the "hash tables" (hopefully you used HashMap or something not hopelessly antique). For each entry, see if its key has a matching entry in the other "hash table". If it does, then you have "common" data (if that's what you meant by "common"). If it doesn't, then your data is "unique" (if that's what you meant).

That should get you started, I think. Post back if you still have more questions.
 
Campbell Ritchie
Marshal
Posts: 68917
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It might be easier to achieve with one of the Sets available from a Map; there are three different Sets available and three different methods in the Map interface that return those Sets.
 
AvinashS Sinha
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am doing like  below way:


i am getting false in both syso statement. Please guide me.
 
Campbell Ritchie
Marshal
Posts: 68917
275
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use the code button; I went back and edited your post with the code button, and doesn't it look better . Unfortunately, I have accentuated your inconsistent indentation, and a bug which we have never quite managed to correct has made >< appear where it should only read <. Please avoid abbreviations like syso. Please avoid variable names like ht1, ht2, e2, and query1. If you come back to that code in a few months, you will find those names incomprehensible.
As suggested earlier, don't use the HashTable class. that is legacy code.
Why would you expect the two Sets to contain each other completely? That is equivalent to saying the two Maps are equal to each other.
 
Bartender
Posts: 7077
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I'm reading this correctly, it loops through all the rows in the result set and for each row the keys are the meta-data column labels. And seeing as how keys must be unique, otherwise they overwrite a previous entry, you'd end up with a hash table containing only the last row of the result set.

Have you tried printing out the hash table to see if it is filled the way you imagine?
 
Carey Brown
Bartender
Posts: 7077
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems the code you've provided won't even compile.
 
So I left, I came home, and I ate some pie. And then I read this tiny ad:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic