CopyOnWriteArrayList lastIndexOf() method in Java

  • The lastIndexOf(Object o) method of CopyOnWriteArrayList returns the last occurrence of the element passed in the list. It returns -1 if the element is not present in the list.

    Syntax:

    public int lastIndexOf(Object o)

    Parameters: The function accepts a parameter o whose last occurrence is to be returned.

    Return Value: The function returns the last occurence of the element. It returns -1 if the element is not present in the list.

    Below programs illustrate the above function:

    Program 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to illustrate the CopyOnWriteArrayList
    // lastIndexOf() method in Java
    import java.util.concurrent.CopyOnWriteArrayList;
    import java.util.*;
      
    public class GFG {
        public static void main(String[] args)
        {
      
            // create object of CopyOnWriteArrayList
            CopyOnWriteArrayList<Integer> ArrLis
                = new CopyOnWriteArrayList<Integer>();
      
            // Add elements
            ArrLis.add(32);
            ArrLis.add(67);
            ArrLis.add(67);
            ArrLis.add(100);
      
            // print CopyOnWriteArrayList
            System.out.println("CopyOnWriteArrayList: "
                                               + ArrLis);
      
            // last occurence of 67
            System.out.println("lastIndexOf value: " 
                             + ArrLis.lastIndexOf(67));
        }
    }

    chevron_right

    
    

    Output:

    CopyOnWriteArrayList: [32, 67, 67, 100]
    lastIndexOf value: 2
    

    Program 2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to illustrate the CopyOnWriteArrayList
    // lastIndexOf() method in Java
    import java.util.concurrent.CopyOnWriteArrayList;
    import java.util.*;
      
    public class GFG {
        public static void main(String[] args)
        {
      
            // create object of CopyOnWriteArrayList
            CopyOnWriteArrayList<Integer> ArrLis
                = new CopyOnWriteArrayList<Integer>();
      
            // Add elements
            ArrLis.add(32);
            ArrLis.add(67);
            ArrLis.add(67);
            ArrLis.add(100);
      
            // print CopyOnWriteArrayList
            System.out.println("CopyOnWriteArrayList: " 
                                              + ArrLis);
      
            // 200 is not present
            System.out.println("lastIndexOf value: " 
                              + ArrLis.lastIndexOf(200));
        }
    }

    chevron_right

    
    

    Output:

    CopyOnWriteArrayList: [32, 67, 67, 100]
    lastIndexOf value: -1
    
  • The lastIndexOf(E e, int index) method of CopyOnWriteArrayList returns the last occurrence of the element passed in the list after position index. It returns -1 if the element is not present in the list.

    Syntax:

    public int lastIndexOf(E e, int index)

    Parameters: The function accepts two parameters which are described below:

    • index: specifies the index from which the occurence is to be searched.
    • e: specifies the element whose last occurrence from position index is to be returned.

    Return Value: The function returns the last occurrence of the element after position index. It returns -1 if the element is not present in the list.

    Exceptions: The function throws an IndexOutOfBoundsException if the specified index is negative.

    Below programs illustrate the above function:

    Program 1:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to illustrate the CopyOnWriteArrayList
    // lastIndexOf() method in Java
    import java.util.concurrent.CopyOnWriteArrayList;
    import java.util.*;
      
    public class GFG {
        public static void main(String[] args)
        {
      
            // create object of CopyOnWriteArrayList
            CopyOnWriteArrayList<Integer> ArrLis
                = new CopyOnWriteArrayList<Integer>();
      
            // Add elements
            ArrLis.add(32);
            ArrLis.add(67);
            ArrLis.add(67);
            ArrLis.add(67);
      
            // print CopyOnWriteArrayList
            System.out.println("CopyOnWriteArrayList: " 
                                                 + ArrLis);
      
            // last occurence of 67 from 2nd index
            System.out.println("lastIndexOf value: " 
                              + ArrLis.lastIndexOf(67, 2));
        }
    }

    chevron_right

    
    

    Output:

    CopyOnWriteArrayList: [32, 67, 67, 67]
    lastIndexOf value: 2
    

    Program 2:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // Java Program to illustrate the CopyOnWriteArrayList
    // lastIndexOf() method in Java
    import java.util.concurrent.CopyOnWriteArrayList;
    import java.util.*;
      
    public class GFG {
        public static void main(String[] args)
        {
      
            // create object of CopyOnWriteArrayList
            CopyOnWriteArrayList<Integer> ArrLis
                = new CopyOnWriteArrayList<Integer>();
      
            // Add elements
            ArrLis.add(32);
            ArrLis.add(67);
            ArrLis.add(67);
            ArrLis.add(100);
      
            // print CopyOnWriteArrayList
            System.out.println("CopyOnWriteArrayList: " 
                                              + ArrLis);
      
            // -1 is out of range, hence exception
            System.out.println("lastIndexOf value: " 
                           + ArrLis.lastIndexOf(-1, 200));
        }
    }

    chevron_right

    
    

    Output:

    Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: -1
    at java.util.concurrent.CopyOnWriteArrayList.lastIndexOf(CopyOnWriteArrayList.java:198)
    at java.util.concurrent.CopyOnWriteArrayList.lastIndexOf(CopyOnWriteArrayList.java:263)
    at GFG.main(GFG.java:24)

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/CopyOnWriteArrayList.html#lastIndexOf-E-int-



My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.