Java CopyOnWriteArrayList lastIndexOf() Method

The CopyOnWriteArrayList.lastIndexOf() method in Java is used to find the index of the last occurrence of a specified element in the CopyOnWriteArrayList.

Table of Contents

  1. Introduction
  2. lastIndexOf Method Syntax
  3. Examples
    • Finding the Last Index of an Element
    • Handling Element Not Found
  4. Real-World Use Case
    • Example: Finding Last Occurrence of User Names in a Thread-Safe List
  5. Conclusion

Introduction

The CopyOnWriteArrayList is a thread-safe variant of ArrayList in Java. It is part of the java.util.concurrent package and is designed for scenarios where read operations are more frequent than write operations. The lastIndexOf method allows you to find the position of the last occurrence of a specified element in the list. The CopyOnWriteArrayList achieves thread safety by creating a new copy of the array whenever it is modified.

lastIndexOf() Method Syntax

The syntax for the lastIndexOf method is as follows:

public int lastIndexOf(Object o)
  • The method takes one parameter:
    • o of type Object, which represents the element whose last index is to be found.
  • The method returns the index of the last occurrence of the specified element in the list, or -1 if the element is not found.

Examples

Finding the Last Index of an Element

The lastIndexOf method can be used to find the index of the last occurrence of a specified element in a CopyOnWriteArrayList.

Example

import java.util.concurrent.CopyOnWriteArrayList;

public class LastIndexOfExample {
    public static void main(String[] args) {
        // Creating a CopyOnWriteArrayList with String elements
        CopyOnWriteArrayList<String> names = new CopyOnWriteArrayList<>();

        // Adding elements to the CopyOnWriteArrayList
        names.add("Ravi");
        names.add("Priya");
        names.add("Vijay");
        names.add("Priya");

        // Finding the last index of an element
        int lastIndex = names.lastIndexOf("Priya");

        // Printing the last index
        System.out.println("Last index of 'Priya': " + lastIndex);
    }
}

Output:

Last index of 'Priya': 3

Handling Element Not Found

The lastIndexOf method returns -1 if the specified element is not found in the list.

Example

import java.util.concurrent.CopyOnWriteArrayList;

public class ElementNotFoundExample {
    public static void main(String[] args) {
        // Creating a CopyOnWriteArrayList with String elements
        CopyOnWriteArrayList<String> names = new CopyOnWriteArrayList<>();

        // Adding elements to the CopyOnWriteArrayList
        names.add("Ravi");
        names.add("Priya");
        names.add("Vijay");

        // Trying to find the last index of an element that does not exist
        int lastIndex = names.lastIndexOf("Anita");

        // Printing the last index
        System.out.println("Last index of 'Anita': " + lastIndex);
    }
}

Output:

Last index of 'Anita': -1

Real-World Use Case

Example: Finding Last Occurrence of User Names in a Thread-Safe List

A common real-world use case for CopyOnWriteArrayList is managing a thread-safe list of users and finding the positions of specific users in the list.

Example

import java.util.concurrent.CopyOnWriteArrayList;

public class UserListManager {
    public static void main(String[] args) {
        // Creating a CopyOnWriteArrayList to manage user names
        CopyOnWriteArrayList<String> userList = new CopyOnWriteArrayList<>();

        // Adding user names to the CopyOnWriteArrayList
        userList.add("Ravi");
        userList.add("Priya");
        userList.add("Vijay");
        userList.add("Priya");

        // Finding the last occurrence of a user
        Thread findUserThread = new Thread(() -> {
            int lastIndex = userList.lastIndexOf("Priya");
            System.out.println("Last index of 'Priya': " + lastIndex);
        });

        // Starting the thread
        findUserThread.start();

        // Waiting for the thread to finish
        try {
            findUserThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // Finding the last occurrence of a user that does not exist
        System.out.println("Last index of 'Anita': " + userList.lastIndexOf("Anita"));
    }
}

Output:

Last index of 'Priya': 3
Last index of 'Anita': -1

In this example, CopyOnWriteArrayList is used to manage a thread-safe list of user names, allowing concurrent operations while finding the last occurrence of specific users.

Conclusion

The CopyOnWriteArrayList.lastIndexOf() method in Java provides a way to find the index of the last occurrence of a specified element in a CopyOnWriteArrayList in a thread-safe manner. By understanding how to use this method, you can efficiently manage collections of elements in your Java applications, especially in concurrent environments. The method allows you to locate the last occurrence of elements within the list, making it a versatile tool for data management in multi-threaded scenarios.

Comments