The java.io library provides various classes for performing input and output operations in Java. One of the classes in this package is the List interface, which is used to store a collection of elements. The subList method in the List interface is used to return a view of a portion of the original list, that is, a list that contains a specified range of elements from the original list.
Example 1:
List list = new ArrayList<>(); list.add("apple"); list.add("banana"); list.add("orange"); list.add("grape"); List subList = list.subList(1, 3); System.out.println(subList);
Output: [banana, orange]
In this example, we first create a list of strings and add four elements to it. We then use the subList method to create a new list that contains the elements from index 1 (inclusive) to index 3 (exclusive) of the original list. Finally, we print the subList to the console.
Package library: java.util
Example 2:
List list = Arrays.asList(1, 2, 3, 4, 5, 6); List subList = list.subList(2, 5); System.out.println(subList);
Output: [3, 4, 5]
In this example, we first create a list of integers using the Arrays.asList method. We then use the subList method to create a new list that contains the elements from index 2 (inclusive) to index 5 (exclusive) of the original list. Finally, we print the subList to the console.
Package library: java.util
Java List.subList - 26 examples found. These are the top rated real world Java examples of java.io.List.subList extracted from open source projects. You can rate examples to help us improve the quality of examples.