• Post Reply Bookmark Topic Watch Topic
  • New Topic

Hashtable comparison  RSS feed

 
Chris Syo
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, how to compare two hash table?



Basically, compare htable1 with htable2 for similar values and display result.

Display the value which is not present in either htables.

Thanks for any help.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Syo wrote:Basically, compare htable1 with htable2 for similar values and display result.

please don't use a Raw type in new code.
regarding your problem.you can iterate over a Set(returned by keySet()) Or Enumeration(returned by keys()) of first hashtable and use contains method to check for the specified element in the other hashtable like below:


Read the API for better understanding--->Hashtable<K,V> , Set<K> , Enumeration<K>.

Hope it helps!

Kind Regards,
Praveen.
 
Chris Syo
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:. . . please don't use a Raw type in new code. . . .


Hi Praveen, thanks for your quick reply.

Sorry, what do you mean by "please don't use a Raw type in new code."

Cheers!
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Syo wrote:
Sorry, what do you mean by "please don't use a Raw type in new code."


Have you learned about Generics yet? If not, then don't worry about it yet ... except to remember that how you are using the collections here is *not* recommended.

Henry
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's ok.are you familiar with generics in java.concisely,generics is something that would buy you a type safety for your programme.i am not going to discuss it in detail here.may be it is one of the topic you are going to learn in the upcoming chapters.
In case if you feel impatience to know this topic-->read this Generics tutorial from oracle.
Raw type is just use of the generic class(or any generic type) without mentioning the type parameter for it.
For example in API,Hashtable is declared as Hashtable<K,V>,means in hashtable keys of type K is only allowed and will be mapped to values of type V only.
For example-
--->new Hashtable(),here your Hashtabel can have any type of its key,value pairs i.e.,i can put("abc",1),put(1,"abc"),put(new Object(),3)..etc and on calling getters i have to apply a cast on the result..(even then you would get a warning from a compiler because compiler doesnot know about-you are casting to a correct type or not and thus may cause runtime failure of your programme.)
--->Hashtable<String,Integer>,it would only allow key of type String and values of type Integer only and you don't have to apply cast after applying getters on hashtabel. if i try putting like put(new Object(),1),put(1,"abc")..etc it would result in compile time error.

Please don't get deep in this topic if you have not read it yet..it's one of the difficult topics in java to understand.
In fact the one who had years of experience with java gets puzzled with this topic several times.
This is another good link to read about FAQ in generics.

Hope it helps!

Kind Regards,
Praveen.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generics: Simplified version

Consider a bottle. By definition it can hold any fluid. Like milk, water or even beer! (my favourite)
Imagine a mother telling her child to go and put the fresh milk in the bottle. Unfortunately, the kid puts it in the bottle containing beer! Disaster!
Solution: Label the bottles.

This is exactly what Generics does.
So instead of Bottle you would write Bottle<Water>, Bottle<Milk> and Bottle<Beer>
Once you do this, the compiler will NOT allow milk to go into the beer bottle.

One interesting thing to note here: Generics are a compile time(only) feature. (This is one of the favourite interview questions for some strange reason by the way)
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
very nice example maneesh .
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris Syo wrote:. . . Sorry, what do you mean by "please don't use a Raw type in new code."

Cheers!
You have already got some good answers. To work from those replies: the following is a raw type (on the right):-
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class Hashtable is an old collection class, which has been superseded by HashMap. Use HashMap instead of Hashtable.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:very nice example maneesh .

Thank you!
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried using a raw type in real life recentlyGenerics was introduced to prevent that sort of thing happening in your programming.
 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To reel it away from generics and back to the original question, if it were just straight up comparing contents, the equals() method would have been sufficient. However, since you have to determine differences and report which elements are not common to both, then you have to do a little more than just call the equals() method. 

This code also doesn't do what you seem to think it does:

After this code executes, each Hashtable will only have one entry:

htable1 will have { IN --> "B" }
htable2 will have { IN --> "D" }

If you did this:


 
Junilu Lacar
Sheriff
Posts: 11481
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You wrote this comment:

That comment uses the wrong terminology. Based on your code, the keys are whatever IN and OUT are.  The values that you are trying to put into the hash tables are the strings "A", "B", and "D". So it would be misleading for your program to display "Hash table contains key A but not D".  It would be correct to say "Hash table contains the value A but not the value D".

I'm pointing this out not to nitpick but to make sure that it doesn't reflect a basic lack of understanding of how this class works that could be adding to your confusion
 
Chris Syo
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:Generics: Simplified version

Consider a bottle. By definition it can hold any fluid. Like milk, water or even beer! (my favourite)
Imagine a mother telling her child to go and put the fresh milk in the bottle. Unfortunately, the kid puts it in the bottle containing beer! Disaster!
Solution: Label the bottles.

This is exactly what Generics does.
So instead of Bottle you would write Bottle<Water>, Bottle<Milk> and Bottle<Beer>
Once you do this, the compiler will NOT allow milk to go into the beer bottle.

One interesting thing to note here: Generics are a compile time(only) feature. (This is one of the favourite interview questions for some strange reason by the way)


wow, just a clever explanation.
 
Chris Syo
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Chris Syo wrote:. . . Sorry, what do you mean by "please don't use a Raw type in new code."

Cheers!
You have already got some good answers. To work from those replies: the following is a raw type (on the right):-


Thanks, Ritchie.
 
Chris Syo
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:You wrote this comment:

That comment uses the wrong terminology. Based on your code, the keys are whatever IN and OUT are.  The values that you are trying to put into the hash tables are the strings "A", "B", and "D". So it would be misleading for your program to display "Hash table contains key A but not D".  It would be correct to say "Hash table contains the value A but not the value D".

I'm pointing this out not to nitpick but to make sure that it doesn't reflect a basic lack of understanding of how this class works that could be adding to your confusion


Thanks, I think learning must be everyday.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My pleasure to help Why are you using Hashtable rather than HashMap? Have a look in the Java™ Tutorials about Maps: 1 2. Most people have stopped using Hashtable.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!