What is the CopyOnWriteArrayList.lastIndexOf method in Java?
Overview
- The
CopyOnWriteArrayListis a thread-safe implementation ofArrayListwithout synchronization. - When we use any of the altering methods, such as
add()ordelete(), the whole content of this method is duplicated into a new internal copy. 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 where we iterate it more frequently than alter it.
- If adding items is a typical activity,
CopyOnWriteArrayListisn’t the appropriate data structure to use because the extra copies will almost always result in poor performance.
How does lastIndexOf() method work?
lastIndexOf() is an instance method of the CopyOnWriteArrayList, which is used to return the index of the last occurrence of the specified element. There are two variations of the method:
public int lastIndexOf(Object o)public int lastIndexOf(E e, int index)
The lastIndexOf 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;
public int lastIndexOf(Object o)
This variation of the method is used to return the index of the last occurrence of the specified object. We use the equals() method to compare the specified object with the elements of the list.
Syntax
public int lastIndexOf(Object o)
Parameters
This method takes the parameter, Object o, which represents the object we want to find.
Return value
This method returns the last occurring index of the given object.
It returns -1 if the list doesn’t contain the object.
public int lastIndexOf(E e, int index)
This variation of the method takes in the element to search and an index value. It is used to search the list backward, and return the index of the last occurrence of the specified element.
Syntax
public int lastIndexOf(E e, int index)
Parameters
E e: The element to find.int index: The index to start searching backward from.
Return value
This method returns the last occurring index of the given element at a position that is less than or equal to the specified index.
It returns -1 if the list doesn’t contain the object.
Example
import java.util.concurrent.CopyOnWriteArrayList;public class Main {private static void lastIndexOfBackwards(CopyOnWriteArrayList<String> copyOnWriteArrayListObject, String elementToFind, int index){int lastIndex = copyOnWriteArrayListObject.lastIndexOf(elementToFind, index);if(lastIndex != -1) System.out.printf("The last occurrence of '%s' is at index %s", elementToFind, lastIndex);else System.out.printf("'%s' not found in the list", elementToFind);System.out.println();}private static void lastIndexOfObject(CopyOnWriteArrayList<String> copyOnWriteArrayListObject, String elementToFind){int index = copyOnWriteArrayListObject.lastIndexOf(elementToFind);if(index != -1) System.out.printf("The last occurrence of '%s' is at index %s", elementToFind, index);else System.out.printf("'%s' not found in the list", elementToFind);System.out.println();}public static void main(String[] args){CopyOnWriteArrayList<String> copyOnWriteArrayListObject = new CopyOnWriteArrayList<>();copyOnWriteArrayListObject.add("edpresso");copyOnWriteArrayListObject.add("courses");copyOnWriteArrayListObject.add("educative");copyOnWriteArrayListObject.add("courses");copyOnWriteArrayListObject.add("coding-interview");copyOnWriteArrayListObject.add("courses");String elementToFind = "courses";int index = 4;lastIndexOfBackwards(copyOnWriteArrayListObject, elementToFind, index);lastIndexOfObject(copyOnWriteArrayListObject, elementToFind);elementToFind = "hello";lastIndexOfObject(copyOnWriteArrayListObject, elementToFind);lastIndexOfBackwards(copyOnWriteArrayListObject, elementToFind, index);}}
Explanation
- Line 1: We import the
CopyOnWriteArrayListclass. - Lines 5–10: We define a method called
lastIndexOfBackwards, which useslastIndexOf(E e, int index)variation to find the last occurring index of the given object searching backward from the given index. - Lines 12–17: We define a method called
lastIndexOfObjectthat useslastIndexOf(Object o)variation to find the last occurring index of the given object. - Line 20: We create an instance of the
CopyOnWriteArrayListclass calledcopyOnWriteArrayListObject. - Lines 22–27: We use the
add()method to insert elements tocopyOnWriteArrayListObject.