Thursday, December 8, 2011

Iterate through HashMap template

I created this simple Eclipse template to loop through HashMap:
 ${:import(java.util.Map.Entry)}${String} key;  
 ${Object} value;  
 for (Entry<${String}, ${Object}> ${iterable_element} : ${test:localVar(Map)}.entrySet()) {  
   key = ${iterable_element}.getKey();  
   value = ${iterable_element}.getValue();  
   ${cursor}  
 }  
  • ${:import(java.util.Map.Entry)} - imports java.util.Map.Entry package
  • ${hashMap:localVar(Map)} - shows all variables to user that are of type Map
  • ${cursor} - After template is created, cursor will be positioned here
Here how it looks in eclipse:

To add template in Eclipse, choose Window->Preferences->Java->Editor->Templates->New. As an context select Java statement.

References:
http://help.eclipse.org/helios/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Fconcepts%2Fconcept-template-variables.htm

No comments:

Post a Comment