Guava’s ImmutableList class in Java
This post will talk about Guava’s ImmutableList class in Java, which provides several static utility methods for creating Immutable lists in Java and operating on them.
1. Using ImmutableList.of() method
Guava’s ImmutableList.of() returns an immutable list having the given elements. It has 7 overloaded versions, the last one with var-args to handle any number of elements.
static <E> ImmutableList<E> of(E element)
static <E> ImmutableList<E> of(E e1, E e2)
static <E> ImmutableList<E> of(E e1, E e2, E e3)
static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4)
static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4, E e5)
static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E… others)
Here’s Java program to demonstrate Guava’s ImmutableList.of() method:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import com.google.common.collect.ImmutableList; import java.util.List; class Main { public static void main(String[] args) { List<Integer> first = ImmutableList.of(); System.out.println(first); List<Integer> second = ImmutableList.of(1, 2, 3, 4, 5); System.out.println(second); Integer[] arr = { 13, 14, 15, 16, 17, 18 }; List<Integer> third = ImmutableList.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, arr); System.out.println(third); } } |
2. Using Builder
Guava also provides a builder for creating immutable list instances, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import com.google.common.collect.ImmutableList; import java.util.Arrays; class Main { // Java program to create an immutable list instance using Guava's // `ImmutableList.builder()` method public static void main(String[] args) { // `builder()` returns a builder for creating immutable list instances // `addAll()` adds each list element to the immutable list // `build()` returns a newly created immutable list ImmutableList<Integer> list = ImmutableList.<Integer>builder() .addAll(Arrays.asList(1, 2, 3)) .add(4) .add(5) .build(); System.out.println(list); } } |
3. Using ImmutableList.copyOf() method
Guava’s ImmutableList.copyOf() method returns an immutable list containing elements of the specified collection, a non-primitive array, or an iterator.
|
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 26 27 28 |
import com.google.common.collect.ImmutableList; import java.util.Arrays; import java.util.List; class Main { // Java program to create an immutable list instance using // Guava's `ImmutableList.copyOf()` method public static void main(String[] args) { Integer[] ints = { 1, 2, 3 }; List<Integer> mutableList = Arrays.asList(1, 2, 3, 4, 5); // 1. Create an immutable list from elements of the given array ImmutableList<Integer> first = ImmutableList.copyOf(ints); System.out.println(first); // 2. Create an immutable list from the given mutable list ImmutableList<Integer> second = ImmutableList.copyOf(mutableList); System.out.println(second); // 3. Create an immutable list from the given iterator ImmutableList<Integer> third = ImmutableList.copyOf( mutableList.iterator()); System.out.println(third); } } |
4. Using ImmutableList.reverse() method
Guava’s ImmutableList.reverse() returns a reversed view of the specified immutable list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import com.google.common.collect.ImmutableList; class Main { // Java program to demonstrate Guava's `ImmutableList.reverse()` method public static void main(String[] args) { ImmutableList<Integer> immutableList = ImmutableList.of(1, 2, 3, 4, 5); ImmutableList<Integer> reversedList = immutableList.reverse(); System.out.println(reversedList); // prints [5, 4, 3, 2, 1] } } |
5. Using ImmutableList.subList() method
Guava’s ImmutableList.subList() method returns an immutable list of the elements between the specified indices. The starting index is inclusive, and the ending index is exclusive.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import com.google.common.collect.ImmutableList; class Main { // Java program to demonstrate Guava's `ImmutableList.subList()` method public static void main(String[] args) { ImmutableList<Integer> list = ImmutableList.of(0, 1, 2, 3, 4, 5); ImmutableList<Integer> sublist = list.subList(1, 4); System.out.println(sublist); // prints [1, 2, 3] } } |
6. Using ImmutableList indexOf() and lastIndexOf() method
Guava’s ImmutableList.indexOf() method returns the index of the first occurrence of the specified element in the immutable list and returns -1 if the element is not present in the list.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import com.google.common.collect.ImmutableList; class Main { // Java program to demonstrate Guava's `ImmutableList.indexOf()` method public static void main(String[] args) { ImmutableList<Integer> immutableList = ImmutableList.of(1, 2, 3, 4, 5); int target = 2; int index = immutableList.indexOf(target); if (index != -1) { System.out.println("Target is present in the list at index " + index); } else { System.out.println("Target is not present in the list"); } } } |
ImmutableList.indexOf() method works similarly but returns the index of the last occurrence.
7. Using ImmutableList.contains() method
Guava’s ImmutableList.contains() method determines if the specified element is present in the immutable list or not.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import com.google.common.collect.ImmutableList; class Main { // Java program to demonstrate Guava's `ImmutableList.contains()` method public static void main(String[] args) { ImmutableList<Integer> immutableList = ImmutableList.of(1, 2, 3, 4, 5); int target = 2; if (immutableList.contains(target)) { System.out.println("Target is present in the list"); } else { System.out.println("Target is not present in the list"); } } } |
That’s all about Guava’s ImmutableList class in Java.
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 :)