The ArrayList.subList()
method in Java is used to create a view of a portion of the ArrayList
. This guide will cover the method’s usage, explain how it works, and provide examples to demonstrate its functionality. Additionally, we will cover a real-world use case to illustrate its application.
Table of Contents
- Introduction
subList
Method Syntax- How It Works
- Examples
- Creating a SubList
- Modifying the SubList
- Real-World Use Case
- Conclusion
Introduction
The ArrayList.subList()
method is part of the ArrayList
class in Java. It allows you to create a view of a specific range of elements within the list. This view is backed by the original list, meaning changes to the sublist are reflected in the original list and vice versa.
subList Method Syntax
The syntax for the subList
method is as follows:
public List<E> subList(int fromIndex, int toIndex)
- fromIndex: The starting index (inclusive) of the sublist.
- toIndex: The ending index (exclusive) of the sublist.
- The method returns a view of the specified range within the list.
How It Works
When you use the subList(int fromIndex, int toIndex)
method, the ArrayList
creates a view of the elements between the specified fromIndex
(inclusive) and toIndex
(exclusive). This view is backed by the original list, meaning that any changes made to the sublist are reflected in the original list and vice versa. If the specified range is invalid, the method throws an IndexOutOfBoundsException
or IllegalArgumentException
.
Examples
Creating a SubList
The subList
method can be used to create a view of a specific range of elements within the ArrayList
.
Example
import java.util.ArrayList;
import java.util.List;
public class SubListExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Orange");
list.add("Grapes");
list.add("Mango");
// Create a sublist from index 1 (inclusive) to 3 (exclusive)
List<String> sublist = list.subList(1, 3);
System.out.println("Original list: " + list);
System.out.println("Sublist: " + sublist);
}
}
Output:
Original list: [Apple, Banana, Orange, Grapes, Mango]
Sublist: [Banana, Orange]
Modifying the SubList
Changes made to the sublist are reflected in the original list and vice versa.
Example
import java.util.ArrayList;
import java.util.List;
public class ModifySubListExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Orange");
list.add("Grapes");
list.add("Mango");
// Create a sublist from index 1 (inclusive) to 3 (exclusive)
List<String> sublist = list.subList(1, 3);
// Modify the sublist
sublist.set(1, "Peach");
System.out.println("Original list after modification: " + list);
System.out.println("Sublist after modification: " + sublist);
}
}
Output:
Original list after modification: [Apple, Banana, Peach, Grapes, Mango]
Sublist after modification: [Banana, Peach]
Handling Invalid Range
If the specified range is invalid, the method will throw an IndexOutOfBoundsException
or IllegalArgumentException
.
Example
import java.util.ArrayList;
import java.util.List;
public class SubListInvalidRangeExample {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
list.add("Orange");
try {
// Attempt to create a sublist with an invalid range
List<String> sublist = list.subList(2, 5);
} catch (IndexOutOfBoundsException | IllegalArgumentException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Output:
Error: toIndex = 5
Real-World Use Case
Paginating a List of Users
In an application where you manage a large list of users, you might want to display users in pages. The subList()
method can be used to create sublists for each page.
Example
import java.util.ArrayList;
import java.util.List;
class User {
String name;
User(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
public class UserPagination {
public static void main(String[] args) {
List<User> users = new ArrayList<>();
for (int i = 1; i <= 20; i++) {
users.add(new User("User " + i));
}
// Paginate the list of users, displaying 5 users per page
int pageSize = 5;
for (int i = 0; i < users.size(); i += pageSize) {
int toIndex = Math.min(i + pageSize, users.size());
List<User> page = users.subList(i, toIndex);
System.out.println("Page " + (i / pageSize + 1) + ": " + page);
}
}
}
Output:
Page 1: [User 1, User 2, User 3, User 4, User 5]
Page 2: [User 6, User 7, User 8, User 9, User 10]
Page 3: [User 11, User 12, User 13, User 14, User 15]
Page 4: [User 16, User 17, User 18, User 19, User 20]
Conclusion
The ArrayList.subList()
method in Java provides a way to create a view of a specific range of elements within an ArrayList
. By understanding how to use this method, you can efficiently manage and manipulate subsets of data in your Java applications. This method is particularly useful in real-world applications such as paginating lists, managing parts of a collection, and performing operations on specific ranges of elements.