How to find the last occurrence of an element in a list with Java

1 Answer

0 votes
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MyClass {
    public static void main(String args[]) {
      List<Integer> list = new ArrayList<>(Arrays.asList(2, 6, 8, 1, 6, 5, 1, 9, 4, 4, 4));
      
      System.out.println(list.lastIndexOf(Integer.valueOf(6)));
      
      System.out.println(list.lastIndexOf(Integer.valueOf(18)));
    }
}




/*
run:

4
-1

*/

 



answered Sep 7, 2022 by avibootz
...