Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Is it not possibile to use instance of eclass as map key?
Is it not possibile to use instance of eclass as map key? [message #972588] Mon, 05 November 2012 18:52 Go to next message
John M. is currently offline John M.Friend
Messages: 198
Registered: July 2010
Senior Member
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 #972651 is a reply to message #972588] Mon, 05 November 2012 20:01 Go to previous messageGo to next message
Christian Damus is currently offline Christian DamusFriend
Messages: 1270
Registered: July 2009
Location: Canada
Senior Member

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 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33137
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 Go to previous message
Christian Damus is currently offline Christian DamusFriend
Messages: 1270
Registered: July 2009
Location: Canada
Senior Member

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?
Previous Topic:[XCore] GenModel-Importer cannot handle cyclic package imports
Next Topic:ecorediag class instance
Goto Forum:
  


Current Time: Fri Apr 19 10:11:44 GMT 2024

Powered by FUDForum. Page generated in 0.02043 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top