The lastIndexOf() method of CopyOnWriteArrayList in Java

Java 8Object Oriented ProgrammingProgramming

<p>The lastIndexOf() method returns the index of the last occurrence of the specified element in this list. It returns -1 if this list does not contain the element.</p><p>The syntax is as follows</p><pre class="result notranslate">public int lastIndexOf(Object ob)</pre><p>Here, ob is the element. The return value would be the last index of this element.</p><p>To work with CopyOnWriteArrayList class, you need to import the following package</p><pre class="result notranslate">import java.util.concurrent.CopyOnWriteArrayList;</pre><p>The following is an example to implement CopyOnWriteArrayList class lastIndexOf() method in Java</p><h2>Example</h2><p><a class="demo" href="" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">import java.util.concurrent.CopyOnWriteArrayList; public class Demo { &nbsp; &nbsp;public static void main(String[] args) { &nbsp; &nbsp; &nbsp; CopyOnWriteArrayList&lt;Integer&gt; &nbsp; &nbsp; &nbsp; arrList = new CopyOnWriteArrayList&lt;Integer&gt;(); &nbsp; &nbsp; &nbsp; arrList.add(50); &nbsp; &nbsp; &nbsp; arrList.add(90); &nbsp; &nbsp; &nbsp; arrList.add(500); &nbsp; &nbsp; &nbsp; arrList.add(200); &nbsp; &nbsp; &nbsp; arrList.add(350); &nbsp; &nbsp; &nbsp; arrList.add(500); &nbsp; &nbsp; &nbsp; arrList.add(650); &nbsp; &nbsp; &nbsp; System.out.println(&quot;CopyOnWriteArrayList = &quot; + arrList); &nbsp; &nbsp; &nbsp; System.out.println(&quot;The last index of element 500 = &quot;+arrList.lastIndexOf(500)); &nbsp; &nbsp;} }</pre><h2>Output</h2><pre class="result notranslate">CopyOnWriteArrayList = [50, 90, 500, 200, 350, 500, 650] The last index of element 500 = 5</pre>
Updated on 30-Jul-2019 22:30:25