Java has a lot of boilerplate code, although each new version tries to remove a bit more every time. One area particularly affected is when you have to declare and initialise lists, maps or sets (some of it hopefully simplified in Java 7). Here’s some sample code:
import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; ... Map<String, String> map = new HashMap<String, String>(); List<String> list = new ArrayList<String>(); Set<String> set = new HashSet<String>(); String[] array = new String[] {};
To create any of these, you have to type a lot. Sometimes you make a mistake and have to retype. And half a minute later you’ve got one line of code that just declares a list. Also, you can have Eclipse automatically add imports when you save, but Eclipse normally can’t guess the correct List, instead prompting you to choose between java.util.List and java.awt.List (why isn’t there an option to use the former as the default?).
But Eclipse templates can help you avoid the hassle. Not only will it create the correct import statements, but also remove the effort of typing out the same type in the interface and implementation collection. If you’re new to templates, then see this tip to get an overview.
Import the templates automatically
Download the templates below then import them into Eclipse.
- Declaration templates: This bundle includes:
arraylist: A List declaration together with an ArrayList initialisation. Automatically imports the correct list (java.util.List).
hashmap: A Map declaration together with a HashMap initialisation.
hashset: A Set declaration together with a HashSet initialisation. - Initialisation templates: This bundle includes:
initarray: An array declaration together with an array initialisation block.
initlist: A List declaration together with an Arrays.asList() call for easy list initialisation.
initmap: A Map declaration together with a call to Apache Commons Lang’s ArrayUtils.toMap() that makes light work of map initialisation. Commons Lang is sometimes already a dependency of external libraries so you may already be dependent on it, but this can extend to any other collection library (eg. Google Collections). You may get an unchecked warning because the method toMap() doesn’t use generics. I normally just add a @SuppressWarning(“unchecked).
Related Tips
- How to tweak Eclipse templates to suit you
- Convert string concatenations into StringBuilder or MessageFormat calls with Eclipse’s Quick Fix
- Disable Eclipse formatting for certain sections of code only
- JUnit testing made easier with Eclipse templates
- Instantly show a class/file in the Package/Project Explorer in Eclipse
Good article. You can use fast code eclipse plugin to generate these, there is no template to manage. Also it gives you a choice to create getter/setters at the same time.
Thanks for that Gautam. I checked it out only briefly but it looks promising. I like the fact that it’s customisable and looks flexible. Certainly something I’ll play around with a bit more.