|Do we have any class equivalent to LinkedHashMap in EMF? [message #1003801]
||Mon, 21 January 2013 21:35
| Roza Ghamari
Registered: January 2013
I am looking for keeping the order of my entries in the map based on the insertion order. So basically I am looking for creating a LinkedHashMap in my ecore model.
I need to persist this map, hence I'd like to use the serialization and proxy resolution feature of EMap. I tried to cast a LinkedHashMap to EcoreEmap by ((EMap.InternalMapView)myLinkedHashMap).eMap(); but that didn't work.
I can think of having a list and map together, and keep the order in list, but I'd really like to avoid duplication of reference, since these two can get out of sync.
Do you have any suggestions?
|Re: Do we have any class equivalent to LinkedHashMap in EMF? [message #1004412 is a reply to message #1004175]
||Wed, 23 January 2013 02:25
| Ed Merks
Registered: July 2009
The method for put in org.eclipse.emf.common.util.BasicEMap.put(K, V)
looks like this.
public V put(K key, V value)
int hash = hashOf(key);
if (size > 0)
int index = indexOf(hash);
Entry<K, V> entry = entryForKey(index, hash, key);
if (entry != null)
V result = putEntry(entry, value);
Entry<K, V> entry = newEntry(hash, key, value);
It's clear that a new entry (a put for a key not already in the map) is
added to the end of the list. So the list is not sorted and the order
of that list appears to match exactly your stated needs.
Your general statement about maps doing sorting isn't true either. Only
things like SortedMap do sorting. Most maps maintain an order that's
effectively random based on the hashCodes of the keys and can be
completely reordered by the addition or removal of a key.
I'd be curious to see what test you wrote that confirms sorting but I'll
continue to assert that an EMap is a list and that the iterator
org.eclipse.emf.common.util.BasicEMap.iterator() which is implemented
public Iterator<Map.Entry<K, V>> iterator()
will return you the entries in list order and of course you have
complete control over that order and can even change it using the EMaps
move methods (because an EMap is also an EList).
On 22/01/2013 7:31 PM, Roza Ghamari wrote:
> Hi ED Merks,
> Although an EMap is a list, but maps do not keep the order of
> insertions and they sort based on the Key. I have confirmed it by
> writing a test and the results showed that the map is sorted based on
> the keys (similar to java.util map). I should add that ecore uses
> ECoreEMap class which extends BaseEMap.
Powered by FUDForum
. Page generated in 0.02232 seconds