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?
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.
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.
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()) {
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With