Guava ImmutableList.subList() method in Java
In this post, we will show you how to use the Guava ImmutableList.subList() method in Java, and explain its benefits. We will also compare it with other alternatives.
Guava ImmutableList class provides static utility methods for creating and operating on immutable lists in Java. One of them is the ImmutableList.subList() method, which returns an immutable list of the elements between the specified indices.
1. Usage of ImmutableList.subList() method
The ImmutableList.subList() method is an instance method that returns a view of the elements between the specified indices. The starting index is inclusive, and the ending index is exclusive. The syntax of this method is as follows:
|
1 |
public ImmutableList<E> subList(int fromIndex, int toIndex) |
The fromIndex parameter is the starting index (inclusive) of the sublist, which must be between 0 and size(). The toIndex parameter is the ending index (exclusive) of the sublist, which must be between fromIndex and size(). The return value is an immutable list of the elements between index fromIndex (inclusive) and toIndex (exclusive).
The method is useful when you want to access a subset of an immutable list, without copying or changing the list. For example, to get a sublist of an immutable list of strings from index 0 to index 2 (exclusive), you can use the following code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import com.google.common.collect.ImmutableList; class Main { public static void main(String[] args) { // create an immutable list of strings ImmutableList<String> list = ImmutableList.of("avocado", "mango", "pear", "cherry"); // get a view of the first two elements ImmutableList<String> sublist = list.subList(0, 2); // print the sublist System.out.println(sublist); // [avocado, mango] } } |
2. Alternatives to subList() method
If you don’t want to use the subList() method from Guava, there are some other alternatives to create a sublist of an immutable list in Java.
One alternative is to use the standard List.subList() method, which takes two indices as parameters and returns a view of the portion of this list between them. This method works similarly to the subList() method from Guava, except that it is called on a List instance. For example, you can use the following code to create a sublist of an immutable list using List.subList():
|
1 2 3 4 5 6 7 8 9 10 11 |
import com.google.common.collect.ImmutableList; import java.util.List; class Main { public static void main(String[] args) { List<String> list = ImmutableList.of("avocado", "mango", "pear", "cherry"); List<String> sublist = list.subList(0, 2); System.out.println(sublist); // [avocado, mango] } } |
Another alternative is to use the Java 8 Stream API, which provides a way to process collections of data in a declarative way. You can use the Stream.skip() and Stream.limit() methods, which return streams consisting of the remaining elements after discarding or limiting the first n elements respectively. You can then collect the stream into a new list using the Collectors.toList() method. For example, you can use the following code to create a sublist of an immutable list using Stream:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import com.google.common.collect.ImmutableList; import java.util.List; import java.util.stream.Collectors; class Main { public static void main(String[] args) { ImmutableList<String> list = ImmutableList.of("avocado", "mango", "pear", "cherry"); List<String> sublist = list.stream() .skip(0) // skip zero elements .limit(2) // limit to two elements .collect(Collectors.toList()); // collect into a new list System.out.println(sublist); // [avocado, mango] } } |
The advantage of using this alternative is that it allows you to create a sublist in a functional and elegant way. You can also perform various operations on the stream using the Stream API, such as filtering, mapping, sorting, etc. The sublist is mutable, unlike the original list. This means you can add, remove, or change elements in the sublist. The disadvantage of using this is that it might not be compatible with older versions of Java. It also creates a new mutable list from the stream, which might consume more memory than creating a view of the original list.
3. Benefits of Using subList() method
The subList() method has some benefits when compared with other methods of creating a sublist of an immutable list in Java.
- One benefit is that it allows you to create a sublist in a concise and expressive way. You don’t have to write any loops or builders to copy or filter the elements. You just need to call one method with two parameters and get the result.
- Another benefit is that it returns a view of the original list, not a copy. This means that it does not create any unnecessary objects or consume any extra memory.
4. Conclusion
In this post, we have covered how to use the Guava ImmutableList.subList() method in Java, which is one of the methods that you can use to create a sublist of an immutable list. We have also explained its benefits, and compared it with other alternatives.
If you want to learn 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 :)