Guava ImmutableList.of() method in Java
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:
|
1 2 3 4 5 6 7 8 |
public static <E> ImmutableList<E> of() public static <E> ImmutableList<E> of(E element) public static <E> ImmutableList<E> of(E e1, E e2) .. public static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10, E e11) public static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10, E e11, E e12, E... others) |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import com.google.common.collect.ImmutableList; class Main { public static void main(String[] args) { // Create an empty immutable list ImmutableList<String> emptyList = ImmutableList.of(); System.out.println(emptyList); // [] // Create an immutable list with one element ImmutableList<String> singletonList = ImmutableList.of("one"); System.out.println(singletonList); // [one] // Create an immutable list with two elements ImmutableList<String> pairList = ImmutableList.of("one", "two"); System.out.println(pairList); // [one, two] // Create an immutable list with three elements ImmutableList<String> tripleList = ImmutableList.of("one", "two", "three"); System.out.println(tripleList); // [one, two, three] // Create an immutable list with four elements ImmutableList<String> quadList = ImmutableList.of("one", "two", "three", "four"); System.out.println(quadList); // [one, two, three, four] } } |
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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import com.google.common.collect.ImmutableList; class Main { public static void main(String[] args) { // This will work ImmutableList<String> moreThan12Elements = ImmutableList.of( "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o" ); // [a, b, c, d, e, f, g, h, i, j, k, l, m, n, o] System.out.println(moreThan12Elements); // This will throw NullPointerException ImmutableList<String> list = ImmutableList.of("foo", null, "bar"); } } |
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()orImmutableList.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
ImmutableListbased on the number and characteristics of the elements. - It is consistent and reliable. It always returns an
ImmutableListthat preserves the insertion order of the elements. It also throws an exception if any of the elements isnull, 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 allownullvalues. 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 fornullvalues. 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 anynullvalues.
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.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)