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:

 
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:

Download Code

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():

Download Code

 
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:

Download Code

 
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.