Русский English Tags View Sergey Zolotaryov's profile on LinkedIn Sign-in
Useful for loop for lists template for Eclipse
Permanent link 22-06-2018 anydoby java eclipse

It's been a while since I'v stopped using foreach loop constructions in Java in tight places due to implicit creation of iterators. IMO the compiler could be much smarter with lists and just avoid creation of iterators when it sees a random access collection. In methods that are called 100500 times this can cause quite a big win in terms of GC load.

But typing these loops in is not the fastest (probably that's why the foreach loop was invented in the first place) way to program.

Here's what I came up with for Eclipse to save on keystrokes:


for (int ${index} = 0; ${index} < ${collection}.size(); ${index}++) {
	${type:elemType(collection)} ${name:newName(type)} = ${collection}.get(${index});
    ${cursor}
}

Go to Preferences/Java/Editor/Templates and add this snippet to the new template, name it something unique, like loop, set the context to Java statements and apply.

Now when you are near to a list (collection in fact, but that's a shortcoming of Eclipse autosuggestion syntax, it only knows about iterables, arrays and collections, but not lists) typing loop and ctrl+space will save you some moments to insert the boilerplate.

Add a comment

Previous article Centralized SSL configuration in Camel