File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Compare two Hashtables ?? !! :-( Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply 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: 18670
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
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Compare two Hashtables ?? !! :-(
 
Similar Threads
reading elements in hashtable in an order
What is the difference between HashSet and HashTable?
compare the two xml files attributes, elements & sub elements
arraylist in java
Hashtable