javaintellij-ideakeyboard-shortcutscode-snippetslive-templates

Live Template fore Intellij IDEA for Iterating through Map


We can iterate through collection easily by pressing Ctrl+Alt+T,

And then I wanted to create such template for iterating through map: I wrote these lines to template text box:

for (Map.Entry<$ELEMENT_TYPE$> $VAR$ : $SELECTION$.entrySet()) {
  $END$
}

Now it is generating these codes:

 HashMap<String,Object> map=new HashMap<String,Object>();
    for (Map.Entry<Object> objectEntry : map.entrySet()) {

    }

Map.Entry<Object> should be Map.Entry<String,Object>. I cannot find a way to introduce variable correctly. How can I do that?


Solution

  • It is easier if you just type iter and then Tab.

    You will get a drop-down and there you can choose map.entrySet() and it will give you:

    for (Map.Entry<String, Object> stringObjectEntry : map.entrySet()) {
    
    }