Home » Modeling » EMF » Is it not possibile to use instance of eclass as map key?
|
Re: Is it not possibile to use instance of eclass as map key? [message #972651 is a reply to message #972588] |
Mon, 05 November 2012 20:01 |
|
Hi, John,
EObjects use object identity for equals() comparison. The short answer
is "no". If you need to put them in a map, you can use the
IdentityHashMap or perhaps you can create a custom map that uses
EcoreUtil::equals(EObject, EObject) for comparison if you really need
structural equivalence.
Cheers,
Christian
On 2012-11-05 18:52:15 +0000, John M. said:
> Hello,
>
> I am trying to use an eclass instance as key of a map.
> But it is the same object with a different hashcode. Is it not possible
> to do that?
|
|
|
Re: Is it not possibile to use instance of eclass as map key? [message #973227 is a reply to message #972651] |
Tue, 06 November 2012 06:46 |
Ed Merks Messages: 33218 Registered: July 2009 |
Senior Member |
|
|
Christian,
Actually you can of course have EObjects as the keys of a map, but you
can't (i.e., it's not supported to) override hashCode or equals. As
such x.equals(y) == (x == y) is always true for EObjects. So the John's
statement "they're the same object with different hashcode" is a
contradiction, i.e., if the hash codes are different, they're definitely
not the same object. Note that using mutable objects as keys in a map
is dangerous because any mutations of the object that affect the
hashCode will definitely corrupt the map.
On 05/11/2012 9:01 PM, Christian W. Damus wrote:
> Hi, John,
>
> EObjects use object identity for equals() comparison. The short
> answer is "no". If you need to put them in a map, you can use the
> IdentityHashMap or perhaps you can create a custom map that uses
> EcoreUtil::equals(EObject, EObject) for comparison if you really need
> structural equivalence.
>
> Cheers,
>
> Christian
>
>
> On 2012-11-05 18:52:15 +0000, John M. said:
>
>> Hello,
>>
>> I am trying to use an eclass instance as key of a map.
>> But it is the same object with a different hashcode. Is it not
>> possible to do that?
>
>
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
Re: Is it not possibile to use instance of eclass as map key? [message #973587 is a reply to message #973227] |
Tue, 06 November 2012 13:04 |
|
Hi, Ed,
Yes, it's because of the mutability of EObjects that I suggested using
them as keys in a map is not practical. (I assume that the
impossibility of a thing the OP already actually did was not in
question :-)
I suppose if you don't override the equals() and hashCode() methods,
then the behaviour of the map devolves to IdentityHashMap-like, but one
is often encouraged to implement these methods in things that are map
keys.
Thanks for the clarification.
cW
On 2012-11-06 06:46:55 +0000, Ed Merks said:
> Christian,
>
> Actually you can of course have EObjects as the keys of a map, but you
> can't (i.e., it's not supported to) override hashCode or equals. As
> such x.equals(y) == (x == y) is always true for EObjects. So the
> John's statement "they're the same object with different hashcode" is a
> contradiction, i.e., if the hash codes are different, they're
> definitely not the same object. Note that using mutable objects as
> keys in a map is dangerous because any mutations of the object that
> affect the hashCode will definitely corrupt the map.
>
> On 05/11/2012 9:01 PM, Christian W. Damus wrote:
>> Hi, John,
>>
>> EObjects use object identity for equals() comparison. The short answer
>> is "no". If you need to put them in a map, you can use the
>> IdentityHashMap or perhaps you can create a custom map that uses
>> EcoreUtil::equals(EObject, EObject) for comparison if you really need
>> structural equivalence.
>>
>> Cheers,
>>
>> Christian
>>
>>
>> On 2012-11-05 18:52:15 +0000, John M. said:
>>
>>> Hello,
>>>
>>> I am trying to use an eclass instance as key of a map.
>>> But it is the same object with a different hashcode. Is it not possible
>>> to do that?
|
|
|
Goto Forum:
Current Time: Thu Sep 26 08:15:09 GMT 2024
Powered by FUDForum. Page generated in 0.03247 seconds
|