Eclipse Zone is brought to you in partnership with:

Having worked with a number of languages and tools over the years, I've learned one thing: work with the tools you have, learn them, master them, then make life easier by sharing the knowledge. Whether Java, C++, VB, or COBOL, I've always applied the same philosophy with success. I love studying manuals and being a keyboard addict, the list of keyboard shortcuts is often my favorite page. Byron is a DZone MVB and is not an employee of DZone and has posted 18 posts at DZone. View Full User Profile

Quickly Declare and Initialise Lists, Maps and Arrays With Eclipse Templates

03.31.2010
| 12536 views |
  • submit to reddit

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).
From http://eclipseone.wordpress.com/2010/03/30/declare-initialise-lists-maps-arrays-with-eclipse-templates/
Published at DZone with permission of Byron M, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:

Comments

Marcs Linuxs replied on Wed, 2010/03/31 - 6:07am

hi. thanks for the templates and for eclipse on e blog.  I signed the rss feed , waiting for more useful tips. I also agree with you about learning the tools and eclipse is a wonderful tool

Chris Roberts replied on Wed, 2010/03/31 - 9:35pm

 Thanks for the great tip and helpful templates, but I should point out that Eclipse *does* let you ignore java.awt.List in favor of java.util.List.  If you go to Windows > Preferences > Java > Appearance > Type Filters, you'll see that you can filter out (for example) java.awt classes from content assist, quick fixes, and the Open Type dialog box.  StackOverflow has a pointer to this too:  http://stackoverflow.com/questions/1602515/how-to-configure-eclipse-to-autocomplete-using-java-util-list-and-not-java-awt-li.  Still, your custom templates presented here have several more useful features than just this...

Byron M replied on Sun, 2010/04/04 - 1:15pm

Thanks for the compliment, Chris. Glad I could help.

And thanks for pointing out Type Filters. I've known about them, but hadn't thought of using them for this. I'd always thought of telling Eclipse to always default to (ie. include) java.util.List instead of excluding everything else (including other Lists that tend to appear in the dialog depending on the libraries you're using).

Byron M replied on Sun, 2010/04/04 - 1:18pm in response to: Marcs Linuxs

No problem, Marcs. If you haven't already done so, have a look through the Tips Archive where you can see all the tips I've posted so far.

Keep that RSS going - there are lots more tips on the way.

Elhanan Maayan replied on Tue, 2010/04/06 - 2:13am

thanks, i was wondering are there any libraries for detail formatters for compext types? (like map list etc..)

 

Rehman Khan replied on Sat, 2012/02/25 - 4:41am

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.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.