Java List indexOf() lastIndexOf() isEmpty() Methods Tutorial

In this section, we will learn what the List indexOf(), lastIndexOf(), and isEmpty() methods are and how to use them in Java.

What is Java List indexOf() Method?

The Java List indexOf() method is used to find the index number of an element in a list object.

For example, you have a list of 10 integer numbers from 0 to 9 and want to know what’s the index number of the value 8 in that list. This method can help you in a situation like this.

Note: if there are multiple same values in a list object, this method will return the index of the first occurrence.

Java List indexOf() Method Syntax:

int indexOf(Object o)

indexOf() Method Parameters:

The method takes one argument, and that is the element we want to check in a list object to find its index number.

indexOf() Method Return Value:

The return value of this method is of type integer and is equal to the index number of the element we set as the argument of this method.

Note: if the element does not exist in the target list, the return value will be -1.

indexOf() Method Exceptions:

ClassCastException: You’ll get this exception if the argument element is of a type that is incompatible with the type of the elements in the target list object.

NullPointerException: this exception will be thrown if the argument of the method is null.

Example: using List indexOf() method

import java.util.List; 
import java.util.ArrayList; 

public class Main {

    public static void main(String[] args){
        List<String> list = new ArrayList<>();
        list.add("One");
        list.add("Two");
        list.add("Three");
        list.add("Four");
        list.add("Five");
        list.add("Six");

        System.out.println("The index of the value four is: "+ list.indexOf("Four"));
    }
}

Output:

The index of the value four is: 3

What is Java List lastIndexOf() Method?

The Java List lastIndexOf() method is used to find the index number of the last occurrence of an element in a list object.

This means if in a list object there are 3 occurrences of the value “Ten” for example, using this method you get the index number of the last “Ten” value.

Another way of considering the work of this method is that it will search for the target element from the end of a list object toward its beginning.

Java List lastIndexOf() Method Syntax:

int lastIndexOf(Object o)

lastIndexOf() Method Parameters:

The method takes one argument, and that is the element we want to search for in a list object.

lastIndexOf() Method Return Value:

The return value of this method is an integer number that is equal to the index of the last occurrence of the target element in a list object.

Note: if the element wasn’t in the target list object, you’ll get the value -1 as a result.

lastIndexOf() Method Exceptions:

This method throws the same exceptions that the indexOf() method might throw.

Example: using List lastIndexOf() method

import java.util.List; 
import java.util.ArrayList; 

public class Main {

    public static void main(String[] args){
        List<String> list = new ArrayList<>();
        list.add("One");
        list.add("Two");
        list.add("Three");
        list.add("Six");
        list.add("Four");
        list.add("Five");
        list.add("Six");

        System.out.println("The index of last occurrence of the value Six is: "+ list.lastIndexOf("Six"));
    }
}

Output:

The index of last occurrence of the value Six is: 6

In this example, there are two occurrences of the value “Six” on the list. Now when we called the lastIndexOf() method on this list to get the index number of last occurrence of the value “Six”, it returned the index number 6. So basically it ignored the first occurrence and moved to the last one.

What is Java List isEmpty() Method?

The Java isEmpty() method is used to see if a list object is empty (Meaning if there are elements in it or not).

Java List isEmpty() Method Syntax:

boolean isEmpty()

isEmpty() Method Parameters:

The method does not take an argument.

isEmpty() Method Return Value:

The return value of this method is a Boolean value:

If the target list object was empty, the return value of this method is true.

Otherwise, the return value will be false.

isEmpty() Method Exceptions:

The method does not throw an exception.

Example: using List isEmpty() method

import java.util.List; 
import java.util.ArrayList; 

public class Main {

    public static void main(String[] args){
        List<String> list = new ArrayList<>();
        list.add("One");
        list.add("Two");
        list.add("Three");
        list.add("Six");
        list.add("Four");
        list.add("Five");
        list.add("Six");

        if (list.isEmpty()){
            System.out.println("Yes the list is empty");
        }else{
            System.out.println("No the list is not empty");
        }
    }
}

Output:

No the list is not empty

As you can see, because the list is not empty in this example, the return value of this method was false. Hence, the body of the else statement ran.

Leave a Reply