wood burning stoves 2.0*
The moose likes Java in General and the fly likes Compare two Hashtables ?? !! :-( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Compare two Hashtables ?? !! :-(" Watch "Compare two Hashtables ?? !! :-(" New topic
Author

Compare two Hashtables ?? !! :-(

manish basotia
Ranch Hand

Joined: Jan 12, 2004
Posts: 47
How can i compare two Hashtable and get the common elements in them. And is there any limit on the number of elements that can be added to Hashtable.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by manish basotia:
How can i compare two Hashtable and get the common elements in them. And is there any limit on the number of elements that can be added to Hashtable.
Yuck, Hashtable. What's wrong with HashMap?

Hashtable does contain a functional implementation of equals(), so that's easy. To get the common elements, you can get the set of Map.Entry objects from both maps, and use Collection.retainAll() along the following lines:
HTH

- Peter
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Oh, and in practice the only limit is your memory.

- Peter
manish basotia
Ranch Hand

Joined: Jan 12, 2004
Posts: 47
Thanks Peter

But your code
Set common = new HashSet(map1.entrySet());
common.retainAll(map2.entrySet());

This will return me only the keys i want the key- value pairs. and it has to be done in the most efficient ways, already my application is heavily loaded. Please let me know. i am not sticking to hashtable or Hashmap. But the goal has to be achieved in the most efficient way, in the minimum time. as tomorrow the hashtable or hashmap may contains hundreds of entires.

Waiting for reply
Thanks With Regards,
Manish B
[ June 02, 2004: Message edited by: manish basotia ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
This will return me only the keys i want the key- value pairs.

Umm, what are you talking about? Have your read the API for entrySet()? It returns a Set of Entry objects (of course), which by definition contain both key and value. I don't understand your question.


"I'm not back." - Bill Harding, Twister
emna bb
Greenhorn

Joined: Feb 24, 2010
Posts: 2
you will find here the solution implemented
http://mrtextminer.wordpress.com/2007/09/14/java-hashtable-sorted-by-values/
enjoy
 
GeeCON Prague 2014
 
subject: Compare two Hashtables ?? !! :-(