subList trong Java

In this tutorial we will see how to get a sublist from an existing ArrayList. We will be doing it using the subList method of ArrayList class.

List subList[int fromIndex, int toIndex]

Here fromIndex is inclusive and toIndex is exclusive. There are few important points regarding this method which I have shared at the end of this post.

Example of getting sub-list from an ArrayList

Points to Note in the below example:
The subList method returns a list therefore to store the sublist in another ArrayList we must need to type cast the returned value in same way as I did in the below example. On the other side if we are storing the returned sublist into a list then there is no need to type cast [Refer the example].

package beginnersbook.com; import java.util.ArrayList; import java.util.List; public class SublistExample { public static void main[String a[]]{ ArrayList al = new ArrayList[]; //Addition of elements in ArrayList al.add["Steve"]; al.add["Justin"]; al.add["Ajeet"]; al.add["John"]; al.add["Arnold"]; al.add["Chaitanya"]; System.out.println["Original ArrayList Content: "+al]; //Sublist to ArrayList ArrayList al2 = new ArrayList[al.subList[1, 4]]; System.out.println["SubList stored in ArrayList: "+al2]; //Sublist to List List list = al.subList[1, 4]; System.out.println["SubList stored in List: "+list]; } }

Output:

Original ArrayList Content: [Steve, Justin, Ajeet, John, Arnold, Chaitanya] SubList stored in ArrayList: [Justin, Ajeet, John] SubList stored in List: [Justin, Ajeet, John]

Note:
The subList method throws IndexOutOfBoundsException – if the specified indexes are out of the range of ArrayList [fromIndex < 0 || toIndex > size].
IllegalArgumentException – if the starting index is greater than the end point index [fromIndex > toIndex].

List là một interface nằm trong nền tảng tập hợp của Java [Java Collection Framework], nó là một interface con của Collection vì vậy nó có đầy đủ các tính năng của một Collection, ngoài ra nó cung cấp cơ sở để duy trì một Collection có thứ tự [ordered Collection]. Nó bao gồm các phương thức cho phép chèn, cập nhập, xoá và tìm kiếm một phần tử theo chỉ số. List cho phép các phần tử trùng lặp, và các phần tử null.

public interface List extends Collection

Hệ thống phân cấp các lớp thi hành [implement] interface List:

LinkedList là một lớp đặc biệt, nó vừa thuộc nhóm List vừa thuộc nhóm Queue:

List cung cấp phương thức listIterator[] trả về một đối tượng ListIterator cho phép lặp [iterate] trên các phần từ theo hướng tiến hoặc lùi. Các lớp thi hành interface List có thể kể tới là ArrayList, LinkedList, Stack, Vector. Trong đó ArrayListLinkedList được sử dụng rộng rãi hơn cả.

Các phương thức được định nghĩa trong List:

public interface List extends Collection { boolean isEmpty[]; boolean contains[Object o]; Object[] toArray[]; T[] toArray[T[] a]; boolean containsAll[Collection c]; boolean addAll[Collection c]; boolean retainAll[Collection c]; int size[]; boolean equals[Object o]; int hashCode[]; E get[int index]; E set[int index, E element]; boolean add[E e]; void add[int index, E element]; E remove[int index]; boolean remove[Object o]; void clear[]; int indexOf[Object o]; int lastIndexOf[Object o]; Iterator iterator[]; ListIterator listIterator[]; ListIterator listIterator[int index]; List subList[int fromIndex, int toIndex]; default Spliterator spliterator[] default void replaceAll[UnaryOperator operator] default void sort[Comparator

Chủ Đề