3

I have created a map in MVEL and I have to iterate over it using foreach. How would I do that?

There is a similar question: How to iterate over a map in mvel But in that case the map was created in Java and had a method to return array of keys (entrySet) which is not the case with me.

//MVEL
map = [
'a': 'a1',
'b': 'b2',
'c': 'c3'
];

foreach (key: map) {
    System.out.println(key);
}

I have tried both map and map.entrySet in the foreach loop but none seems to work.

Note: I test it using MVEL command line and using MVEL version 2.2.0.15

Community
  • 1
  • 1
Fakhruddin
  • 43
  • 1
  • 5
  • I don't think there is any difference. Map created in MVEL is still a Map. Just follow the same way to iterate should work. – Adrian Shum Jun 27 '13 at 03:12
  • @AdrianShum have tried using key:map.entrySet but it does not work. Also, tried key:map and key:map.keys but to no avail. Getting "failed to access property" error. – Fakhruddin Jun 27 '13 at 04:02
  • strange enough... your accepted question is simply what I said: just treat it as a map.... and that's exactly what's told in answer of the other question you quoted. – Adrian Shum Jun 27 '13 at 07:26

2 Answers2

2

Although you have accepted an answer, I think it is better to add something as not to mislead other people:

... had a method to return array of keys (entrySet) which is not the case with me

First, Map is a Map. Map created in MVEL is simply a "Java" Map. The way to iterate is just the same and they are providing same methods

Second, entrySet() is not returning "array of keys". It is returning a Set of Entry (as its name suggests).

I am not sure why you cannot use entrySet as it works just fine for me. I suspect you have do foreach (e : map.entrySet). That will not work, because in MVEL, property navigation can mean several thing, like bean properties (which means it will call map.getEntrySet()), or looking up a map (which means it will call map.get('entrySet')), or getting the field (which means 'map.entrySet'). However all these are not valid for your case. You simply want to invoke map.entrySet() method so that you should just do foreach (e : map.entrySet())

The proper way to do is something like this:

map = ['a':'a1', 'b':'b1'] ; 
foreach(entry : map.entrySet()) {
    System.out.println('key ' + entry.key + ' value ' + entry.value)
};
Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
  • It works when I do map.entrySet().I am new to MVEL and got confused when I saw this syntax in the other question (mentioned in description). As you have mentioned that entrySet could have various meanings, can you explain (or give links) which would actually run (or the priorities of selecting) – Fakhruddin Jun 27 '13 at 13:37
  • the code I gave you can run... may I know the problem of that? And, it is not `entrySet` that having different meanings, it is MVEL's property navigation syntax. When you are navigating a property by `foo.prop`, MVEL will try to interpret the meaning of `prop` property by various ways. Try to get yourself familiar with basics of Java and MVEL. – Adrian Shum Jun 28 '13 at 01:17
  • The code is running fine, just wanted to know what the mvel was looking for when I used it without the parentheses. – Fakhruddin Jul 01 '13 at 07:57
  • 1
    @FakhruddinRangwala ic, got it. That's described in MVEL's document http://mvel.codehaus.org/MVEL+2.0+Property+Navigation – Adrian Shum Jul 02 '13 at 01:31
0

You can use something like this:

map = [
'a': 'a1',
'b': 'b2',
'c': 'c3'
];

foreach (key : map.keySet()) {
        System.out.println("Key:" + key + " Value:" + map[key]);

}

It outputs:

Key:b Value:b2
Key:c Value:c3
Key:a Value:a1
hj3
  • 1