Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 829
Jama A. Avatar asked Oct 01 '12 08:10

Jama A.


People also ask

How do you iterate through a map object?

Use the forEach() method to iterate over a Map object. The forEach method takes a function that gets invoked for each key/value pair in the Map , in insertion order. The function gets passed the value, key and the Map object on each iteration.

Can you iterate through a map?

Using forEach(action) method : In Java 8, you can iterate a map using Map. forEach(action) method and using lambda expression. This technique is clean and fast.


2 Answers

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()) {

}
like image 61
maba Avatar answered Oct 19 '22 17:10

maba


To view a list of live template available: Ctrl + J and then Tab.

From there you will have list of live template, iter (for each loop) will be on the list.

like image 44
Sivabalan Avatar answered Oct 19 '22 16:10

Sivabalan