Sidebar

How can I iterate a maps entrySet in QIE without jshint inspection errors?

0 votes
319 views
asked Jul 15, 2013 by rich-c-2789 (16,240 points)
How can I iterate a maps entrySet in QIE without jshint inspection errors?

1 Answer

0 votes
 
Best answer

 

Try this:

var HashMap = java.util.HashMap;

var populatedMap = new HashMap();
populatedMap.put(123, 'OneTwoThree');
populatedMap.put(456, 'FourFiveSix');

var entryArray = populatedMap.entrySet().toArray();
for (var i=0; i < entryArray.length; i++) {
   var entry = entryArray[i];
   qie.debug('The key=' + entry.getKey() + ' with value=' + entry.getValue());
}

Note the highlighted code to get the entrySet and the use of length vs size.  The same type of declaration will also work for the keySet and values collections of a map.

See this link for another option using an iterator: http://stackoverflow.com/questions/3479146/importing-a-map-in-javax-scripting-javascript-environment

 

answered Jul 15, 2013 by rich-c-2789 (16,240 points)
selected Dec 17, 2013 by ron-s-6919
commented Jun 26, 2014 by gary-t-8719 (14,860 points)
An alternative way that works with a TreeMap but will destroys the map at the end of the iteration.  Not a good solution but keeping track of this here.

var size = populatedMap.size();
for (var i = 0; i < size; i++) {
   var entry = populatedMap.firstEntry();
   populatedMap.remove(entry.getKey());
...