Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the CopyOnWriteArrayList.lastIndexOf method in Java?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

  • The CopyOnWriteArrayList is a thread-safe implementation of ArrayList without synchronization.
  • When we use any of the altering methods, such as 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.
  • 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, CopyOnWriteArrayList isn’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;
Importing 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)
lastIndexOf(Object o) syntax

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)
lastIndexOf(E e, int index) syntax

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);
}
}
lastIndexOf() method

Explanation

  • Line 1: We import the CopyOnWriteArrayList class.
  • Lines 5–10: We define a method called 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.
  • Lines 12–17: We define a method called lastIndexOfObject that uses lastIndexOf(Object o) variation to find the last occurring index of the given object.
  • Line 20: We create an instance of the CopyOnWriteArrayList class called copyOnWriteArrayListObject.
  • Lines 22–27: We use the add() method to insert elements to copyOnWriteArrayListObject.

RELATED TAGS

java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring