Search code examples
collectionsxtend

Create an immutable map from a collection of pairs in Xtend


I have a collection of objects O from which I want to create an (immutable) map of two features (say, O.name and O.value).

My best solution so far is

newHashMap(o.map[it.name -> it.value])

But this will instantiate and initialize a new HashMap which is not acually what I want. Instead I want an immutable map without unneeded instantiation, similarly what

o.toInvertedMap[value]

returns - but this maps O to O.value.

Is there a method in the Xtend library to achieve what I want?

Note: to add some context to my general question above, I actually want to get a map of attribute names and values for my EMF EObjects:

newHashMap(eObject.eClass.EAttributes.map[it.name -> eObject.eGet(it).toString])

Edit:

I just found this:

newImmutableMap(o.map[it.name -> it.value])

This seems more like what I want. Is this the "best" way to write it?


Solution

  • The best way I have found so far is (as in the Update above):

    newImmutableMap(o.map[name->value])