What is the CopyOnWriteArrayList.subList method in Java?
Overview
- In Java, the
CopyOnWriteArrayListmethod is a thread-safe implementation ofArrayListwithout the requirement for synchronization. - The whole content of the
CopyOnWriteArrayListis duplicated into a new internal copy when we use any of the altering methods, such asadd()ordelete(). Hence, the list can be iterated in a safe way without any fear of losing data, while modifying it simultaneously. - The properties of data structures make it particularly helpful in situations when we iterate it more frequently than altering it.
- If adding items is a typical activity,
CopyOnWriteArrayListisn’t the appropriate data structure to use. This is because the extra copies will almost always result in poor performance.
subList() is an instance method of the CopyOnWriteArrayList, which is used to return a view of the list between fromIndex (inclusive) and toIndex (exclusive). This list backs up the returned list. Therefore, any changes in the returned list are mirrored in the original list.
The subList method is defined in the CopyOnWriteArrayList class. The CopyOnWriteArrayList class is defined in the java.util.concurrent package. To import the CopyOnWriteArrayList class, we use the following statement.
import java.util.concurrent.CopyOnWriteArrayList;
Syntax
public List<E> subList(int fromIndex, int toIndex)
Parameters
int fromIndex: The inclusive starting index.int toIndex: The exclusive ending index.
Return value
This method returns a list that is a view of the specified range within the original list.
Example
import java.util.List;import java.util.concurrent.CopyOnWriteArrayList;public class Main {public static void main(String[] args){CopyOnWriteArrayList<String> copyOnWriteArrayListObject = new CopyOnWriteArrayList<>();copyOnWriteArrayListObject.add("edpresso");copyOnWriteArrayListObject.add("educative");copyOnWriteArrayListObject.add("coding-interview");copyOnWriteArrayListObject.add("courses");int fromIndex = 1;int toIndex = 3;List<String> subListObject = copyOnWriteArrayListObject.subList(fromIndex, toIndex);System.out.println("Original List - " + copyOnWriteArrayListObject);System.out.printf("SubList from index %s to index %s - %s\n",fromIndex, toIndex, subListObject);subListObject.add("new-element");System.out.println("Original List after modification of the sublist - " + copyOnWriteArrayListObject);}}
Explanation
- Lines 1 and 2: We import the relevant classes.
- Line 7: We create an instance of the
CopyOnWriteArrayListclass calledcopyOnWriteArrayListObject. - Lines 9–12: We use the
add()method to insert elements tocopyOnWriteArrayListObject. - Line 14: We define the starting index called
fromIndex. - Line 15: We define the ending index called
toIndex. - Line 17: We use the
subList()method to get a sublist the range,[fromIndex, toIndex), fromcopyOnWriteArrayListObject. We call the sublist assubListObject. - Line 19: We print the original list
copyOnWriteArrayListObject. - Line 20: We print
subListObject. - Line 22: We insert a new element to
subListObjectto modify it. - Line 23: We print the original list.
copyOnWriteArrayListObject. The modifications made to the sublist can be observed in the original list.