Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compare two Hashtables ?? !! :-(

 
manish basotia
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, and in practice the only limit is your memory.

- Peter
 
manish basotia
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
emna bb
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you will find here the solution implemented
http://mrtextminer.wordpress.com/2007/09/14/java-hashtable-sorted-by-values/
enjoy
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic