CopyOnWriteArrayList
is a thread-safe implementation of ArrayList
without synchronization.add()
or delete()
, 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.CopyOnWriteArrayList
isn’t the appropriate data structure to use because the extra copies will almost always result in poor performance.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.
public int lastIndexOf(Object o)
This method takes the parameter, Object o
, which represents the object we want to find.
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.
public int lastIndexOf(E e, int index)
E e
: The element to find.int index
: The index to start searching backward from.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.
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);}}
CopyOnWriteArrayList
class.lastIndexOfBackwards
, which uses lastIndexOf(E e, int index)
variation to find the last occurring index of the given object searching backward from the given index.lastIndexOfObject
that uses lastIndexOf(Object o)
variation to find the last occurring index of the given object.CopyOnWriteArrayList
class called copyOnWriteArrayListObject
.add()
method to insert elements to copyOnWriteArrayListObject
.