Working with lists: ImmutableList

When you are faced with a task that involves using lists, you might want to consider the following question: are the elements in my list ever going to change, or is it enough to just add my elements to the list once and use them across my tests. Is my list a constant? In case your elements will not change, you can use an ImmutableList to store them, which brings a major advantage: defining a list in one line. ImmutableList is part of the ‘guava’ library.

Why use such a thing? Well, the major benefit is that creating a list can be done with less code than using other methods. Adding all the elements to the list is done in one simple step. That is, if you know beforehand the values for the elements that you want to add. As the name might suggest, the list cannot be changed once it was defined. Even though ImmutableList implements the List interface, and therefore inherits the “add” or “remove” operations, those cannot be applied to your list.

Let’s consider a simple example: you would like to do some work on a list of Strings. The easiest example would be to output to the console the value of each element. The classical approach would be to: first define a list, and then add all the elements. For example:

List<String> listOfStrings = new ArrayList<>();
listOfStrings.add("first string");
listOfStrings.add("second string");
listOfStrings.add("another string?");
System.out.println(listOfStrings);

The code above will work fine, but you will have way too many lines of code. The benefit of this approach as opposed to an ImmutableList is that you can change the elements in the list whenever you want.
The alternative declaration, the one using an ImmutableList type, would only be:

List<String> listOfStrings = ImmutableList.of("first string", "second string","another string?");
System.out.println(listOfStrings);

In both cases the output to the console would look the same, but it takes less time to write the things you want to be printed and it’s quite easy to use.
Now, in case you used an “add” or “remove” method on the list by mistake, your code will throw an UnsupportedOperationException and the adding or removing will not be performed. You can still use non altering operations, like “contains” or “equals”.

To benefit from this type of lists, you will need to import the Guava library into your project. When using Maven, you will have to put the following entry in your pom.xml file:

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
Advertisements

One thought on “Working with lists: ImmutableList”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s