For Programmers: Free Programming Magazines  


Home > Archive > Java Help > January 2006 > Compare two hashtables with same keys but different values.









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Compare two hashtables with same keys but different values.
Ravi Velamuri

2006-01-25, 9:58 pm

Hi,
I got to compate two hashtables {methodname, return value}.
both hashtables will have the same number of keys and ofcourse SAME
keys BUT the values will be different. Values could be primitive or non
primitive.

Now, I have to compare such two hashtables and listout all the values
(keys for that values also) in Hashtable1 that are diffrentcompared
with Hashtable2 .

Plz adivse, or post some sample code. Thx.

Best,
Ravi Velamuri.

Monique Y. Mudama

2006-01-26, 4:00 am

On 2006-01-26, Ravi Velamuri penned:
> Hi, I got to compate two hashtables {methodname, return value}.
> both hashtables will have the same number of keys and ofcourse SAME
> keys BUT the values will be different. Values could be primitive or
> non primitive.
>
> Now, I have to compare such two hashtables and listout all the
> values (keys for that values also) in Hashtable1 that are
> diffrentcompared with Hashtable2 .
>
> Plz adivse, or post some sample code. Thx.
>


This sounds suspiciously like a homework assignment.

How about you show us how you've started the assignment, and we can
try to give you pointers?

By the way, are you using the java.util.Hashtable or your own
implementation? I ask because you can't put primitives into a
java.util.Hashtable.

--
monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Roedy Green

2006-01-26, 4:00 am

On 25 Jan 2006 18:39:12 -0800, "Ravi Velamuri"
<ravi.velamuri@gmail.com> wrote, quoted or indirectly quoted someone
who said :

>Plz adivse, or post some sample code. Thx.


you would iterate collection 1 to get the keys, then lookup in
collection 1 and collection 2 and compare values as you go.

Sample code for the iteration is posted at
http://mindprod.com/jgloss/hashmap.html

A version that should be slightly faster would do an EntrySet
enumeration. Then you don't have to look up the value for collection
1.

Code for that is also posted at the same place.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Remi Arntzen

2006-01-26, 4:00 am


Monique Y. Mudama wrote:
> By the way, are you using the java.util.Hashtable or your own
> implementation? I ask because you can't put primitives into a
> java.util.Hashtable.


acctually you can put primatives into a hashtable via Autoboxing (The
Java Language Specification, Third Edition (5.1.7 Boxing Conversion)),
but you cant have primitives WITHIN a hashtable or other sort of
collection.

Roedy Green

2006-01-26, 4:00 am

On 25 Jan 2006 21:22:14 -0800, "Remi Arntzen" <Remi.Arntzen@gmail.com>
wrote, quoted or indirectly quoted someone who said :

>
>acctually you can put primatives into a hashtable via Autoboxing (The
>Java Language Specification, Third Edition (5.1.7 Boxing Conversion)),
>but you cant have primitives WITHIN a hashtable or other sort of
>collection.


See http://mindprod.com/jgloss/hashmap.html
for sample code to do it with both manual and automatic boxing.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Monique Y. Mudama

2006-01-26, 4:00 am

On 2006-01-26, Remi Arntzen penned:
>
> Monique Y. Mudama wrote:
>
> acctually you can put primatives into a hashtable via Autoboxing
> (The Java Language Specification, Third Edition (5.1.7 Boxing
> Conversion)), but you cant have primitives WITHIN a hashtable or
> other sort of collection.


Semantics, I guess. I would say that you're not actually putting
primitives into a Hashtable; you're just using a syntactic shortcut to
create objects to put into a Hashtable.

--
monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Ravi Velamuri

2006-01-26, 7:05 pm

Monique:
Let me put more description of the home work assignment :)

I will have One Object say TradeObject
I have to get all the GETmethods in TradeObject with values and put
methodname and return value into a Hashtable (infact, i can use any
collection that solves my purpose,need not be only Hashtable).

here, return value from GETmethods can be anything like primitives or
non primitives.
If primitive then convert it into Object and put it in Hashtable.

NonPrimitive could be anything like, Vector, Hashtable, HashMap,
String, Feese, CounterParty etc. list goes on.

So my compare method has to return me all the key, values pairs that
are different in two Trade Objects.

let me know if any further info required on this. Thx.
Ravi.

Monique Y. Mudama wrote:
> On 2006-01-26, Ravi Velamuri penned:
>
> This sounds suspiciously like a homework assignment.
>
> How about you show us how you've started the assignment, and we can
> try to give you pointers?
>
> By the way, are you using the java.util.Hashtable or your own
> implementation? I ask because you can't put primitives into a
> java.util.Hashtable.
>
> --
> monique
>
> Ask smart questions, get good answers:
> http://www.catb.org/~esr/faqs/smart-questions.html


Monique Y. Mudama

2006-01-26, 7:06 pm

On 2006-01-26, Ravi Velamuri penned:
> Monique: Let me put more description of the home work assignment :)
>
> I will have One Object say TradeObject I have to get all the
> GETmethods in TradeObject with values and put methodname and return
> value into a Hashtable (infact, i can use any collection that solves
> my purpose,need not be only Hashtable).
>
> here, return value from GETmethods can be anything like primitives
> or non primitives. If primitive then convert it into Object and put
> it in Hashtable.
>
> NonPrimitive could be anything like, Vector, Hashtable, HashMap,
> String, Feese, CounterParty etc. list goes on.
>
> So my compare method has to return me all the key, values pairs that
> are different in two Trade Objects.
>
> let me know if any further info required on this. Thx. Ravi.


I'll quote myself:
[color=darkred]
> Monique Y. Mudama wrote:

Write some code, get to the point where you can't figure out what's
going wrong, then post it, identifying the specific line number or
region that is confusing you.

--
monique

Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com