How to search in a List of Java object?


The List interface extends Collection interface and represents a collection storing a sequence of elements. User of a list has quite precise control over where an element to be inserted in the List. These elements are accessible by their index and are searchable. ArrayList is the most popular implementation of the List interface among the Java developers.

Java List interface provides two methods, indexOf() method which can be used to get the location of an element in the list and lastIndexOf() method which can be used to get the last occurence of an element. In this article, we're discussing both indexOf() and lastIndexOf() method to search an element in the list.

indexOf() method.

int indexOf(Object o)

Notes

  • Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

  • Returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.

Parameters

  • o - Element to search for.

Returns

The index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

Throws

  • ClassCastException - If the type of the specified element is incompatible with this list (optional).

  • NullPointerException - If the specified element is null and this list does not permit null elements (optional).

lastIndexOf() method.

int lastIndexOf(Object o)

Notes

  • Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.

  • Returns the highest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.

Parameters

  • - Element to search for.

Returns

The index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.

Throws

  • ClassCastException - If the type of the specified element is incompatible with this list (optional).

  • NullPointerException - If the specified element is null and this list does not permit null elements (optional).

Example 1

Following is the example showing the usage of indexOf() method −

package com.tutorialspoint;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CollectionsDemo {
   public static void main(String[] args) {
      List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8,9,0));
      System.out.println("List: " + list);
      int positionOfFive = list.indexOf(Integer.valueOf(5));
     
      System.out.println("5 is present at index: " + positionOfFive);
      int positionOfTen = list.indexOf(Integer.valueOf(10));
      System.out.println("10 is present at index: " + positionOfTen);
   }
}

Output

This will produce the following result −

List: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
5 is present at index: 4
10 is present at index: -1

Example 2

Following is the example showing the usage of lastIndexOf() method −

package com.tutorialspoint;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CollectionsDemo {
   public static void main(String[] args) {
      List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8,9,0,1,2,3,4,5));
      System.out.println("List: " + list);
      int positionOfFive = list.lastIndexOf(Integer.valueOf(5));
     
      System.out.println("5 is present at index: " + positionOfFive);
      int positionOfTen = list.lastIndexOf(Integer.valueOf(10));
      System.out.println("10 is present at index: " + positionOfTen);
   }
}

Output

This will produce the following result −

List: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5]
5 is present at index: 14
10 is present at index: -1

Updated on: 26-May-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements