In this post, we will show you how to use the ImmutableList.of() method in Java, and explain why it is useful and how it works. We will also compare it with other ways of creating immutable lists in Java, and highlight some of its advantages and limitations.

1. Overview of ImmutableList.of() method

There are several ways to create an instance of ImmutableList in Java. One of them is to use the Guava ImmutableList.of() method. The of() method is a static factory method that returns an immutable list containing the given elements. The syntax of the ImmutableList.of() method is as follows:

 
The of() method has several overloaded versions that accept different numbers of parameters. For example, of() returns an empty immutable list, of(E element) returns an immutable list containing a single element, of(E e1, E e2) returns an immutable list containing two elements, and so on, with last one overloaded to take a varargs parameter.

 
The method returns an instance of ImmutableList containing the given elements, that cannot be modified directly or indirectly. Any attempt to do so will result in an UnsupportedOperationException. It throws a NullPointerException if any of the elements is null.

2. Usage of ImmutableList.of() method

To use the ImmutableList.of() method, you need to provide the elements that you want to include in the immutable list. The elements can be any objects that are not null. The order of the arguments determines the order of the elements in the returned list. You can use this method like this:

Download Code

 
Note that if you provide more than twelve elements, this method will work fine. However, if any of the elements is null, this method will throw an exception at runtime. For example:

Download Code

3. Advantages of using the ImmutableList.of() method

Using the ImmutableList.of() method has many advantages, such as:

  • It is concise and expressive. You can create an immutable list with a fixed number of elements in one line of code, without having to create a mutable list first and then wrap it with Collections.unmodifiableList() or ImmutableList.copyOf().
  • It is efficient and optimized. It avoids unnecessary copying and allocation of intermediate lists, and uses a smart algorithm to choose the optimal implementation of ImmutableList based on the number and characteristics of the elements.
  • It is consistent and reliable. It always returns an ImmutableList that preserves the insertion order of the elements. It also throws an exception if any of the elements is null, instead of silently ignoring them or allowing them.

4. Other ways of creating Immutable Lists in Java

There are other ways of creating immutable lists in Java, such as using List.of(), ImmutableList.copyOf(), or Collections.unmodifiableList(). How do they compare with using ImmutableList.of()? Here are some differences:

  • List.of() returns an immutable list that contains a fixed number of elements. It preserves the order of the elements, but does not allow null values. It also does not accept collections or iterables as input, and requires you to specify each element as a separate argument.
  • ImmutableList.copyOf() returns an immutable list that contains all the elements from a collection or an iterable. It preserves the order of the elements, but does not check for null values. It also creates a copy of the input, which may be inefficient.
  • Collections.unmodifiableList() gives a view of a list that cannot be changed, but the list it depends on may not be immutable. If the list it depends on is modified, the view will also change. It also does not keep the order of the elements as they were inserted, and does not validate if there are any null values.

5. Conclusion

In this post, we have shown you how to use Guava’s ImmutableList.of() method in Java, and explained why it is useful and how it works. We have also contrasted it with other methods of making immutable lists in Java, and emphasized some of its strengths and weaknesses.

If you are interested in learning more about this method, you can check out the Guava official website or its GitHub repository.