The Wayback Machine - https://web.archive.org/web/20240907023415/https://www.geeksforgeeks.org/list-indexof-method-in-java-with-examples/
Open In App

List indexOf() Method in Java with Examples

Last Updated : 02 Jan, 2019
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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

Syntax:

public int indexOf(Object o)

Parameters: This function has a single parameter, i.e, the element to be searched in the list.

Returns: This method returns the index of first occurrence of the given element in the list and returns “-1” if element is not in the list.

Below programs show the implementation of this method.

Program 1:




// Java code to show the implementation of
// indexOf method in list interface
import java.util.*;
public class GfG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Initializing a list of type Linkedlist
        List<Integer> l = new LinkedList<>();
        l.add(1);
        l.add(3);
        l.add(5);
        l.add(7);
        System.out.println(l);
        System.out.println(l.indexOf(5));
    }
}


Output:

[1, 3, 5, 7]
2

Program 2: Below is the code to show implementation of list.hashCode() using Linkedlist.




// Java code to show the implementation of
// indexOf method in list interface
import java.util.*;
public class GfG {
  
    // Driver code
    public static void main(String[] args)
    {
  
        // Initializing a list of type Linkedlist
        List<String> l = new LinkedList<>();
        l.add("10");
        l.add("15");
        l.add("20");
        System.out.println(l);
        System.out.println(l.indexOf("20"));
    }
}


Output:

[10, 15, 20]
2

Reference:
Oracle Docs



Previous Article
Next Article

Similar Reads

Java Guava | Bytes.indexOf(byte[] array, byte target) method with Examples
Bytes.indexOf(byte[] array, byte target) method of Guava's Bytes Class accepts two parameters array and target. If the target exists within the array, the method returns the position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(byte[] array, byte target) Parameters:
3 min read
Java Guava | Floats.indexOf(float[] array, float target) method with Examples
Floats.indexOf(float[] array, float target) method of Guava's Floats Class accepts two parameters array and target. If the target exists within the array, the method returns the position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(float[] array, float target) Param
3 min read
Java Guava | Doubles.indexOf(double[] array, double target) method with Examples
Doubles.indexOf(double[] array, double target) method of Guava's Doubles Class accepts two parameters array and target. If the target exists within the array, the method returns the position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(double[] array, double target)
3 min read
Java Guava | Shorts.indexOf(short[] array, short target) method with Examples
Shorts.indexOf(short[] array, short target) method of Guava's Shorts Class accepts two parameters array and target. If the target exists within the array, the method returns the position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(short[] array, short target) Param
3 min read
Java Guava | Booleans.indexOf(boolean[] array, boolean target) method with Examples
Booleans.indexOf(boolean[] array, boolean target) method of Guava's Booleans Class accepts two parameters array and target. If the target exists within the array, the method returns the position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(boolean[] array, boolean t
3 min read
AbstractList indexOf() method in Java with Examples
The indexOf() method of java.util.AbstractList class is used to return the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index. Syntax: public int indexOf(
3 min read
StringBuffer indexOf() method in Java with Examples
In StringBuffer class, there are two types of indexOf() method depending upon the parameters passed to it. indexOf(String str) The indexOf(String str) method of StringBuffer class is used to return the index of the String for first occurrence of passed substring as parameter from the sequence contained by this object. If substring str is not presen
4 min read
StringBuilder indexOf() method in Java with Examples
In StringBuilder class, there are two types of indexOf() method depending upon the parameters passed to it. indexOf(String str) The indexOf(String str) method of StringBuilder class is the inbuilt method used to return the index within the String for first occurrence of passed substring as parameter. If substring str is not present then -1 is retur
4 min read
Java Guava | Booleans.indexOf(boolean[] array, boolean[] target) method with Examples
Booleans.indexOf(boolean[] array, boolean[] target) method of Guava's Booleans Class accepts two parameters array and target. If the target exists within the array, the method returns the start position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(boolean[] array, b
2 min read
Java Guava | Ints.indexOf(int[] array, int[] target) method with Examples
Ints.indexOf(int[] array, int[] target) method of Guava's Ints Class accepts two parameters array and target. If the target exists within the array, the method returns the start position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(int[] array, int[] target) Paramet
3 min read
Java Guava | Longs.indexOf(long[] array, long[] target) method with Examples
Longs.indexOf(long[] array, long[] target) method of Guava's Longs Class accepts two parameters array and target. If the target exists within the array, the method returns the start position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(long[] array, long[] target) P
3 min read
Java Guava | Shorts.indexOf(short[] array, short[] target) method with Examples
Shorts.indexOf(short[] array, short[] target) method of Guava's Shorts Class accepts two parameters array and target. If the target exists within the array, the method returns the start position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(short[] array, short[] tar
3 min read
Java Guava | Floats.indexOf(float[] array, float[] target) method with Examples
Floats.indexOf(float[] array, float[] target) method of Guava's Floats Class accepts two parameters array and target. If the target exists within the array, the method returns the start position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(float[] array, float[] tar
3 min read
Java Guava | Chars.indexOf(char[] array, char[] target) method with Examples
Chars.indexOf(char[] array, char[] target) method of Guava's Chars Class accepts two parameters array and target. If the target exists within the array, the method returns the start position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(char[] array, char[] target) P
3 min read
Java Guava | Bytes.indexOf(byte[] array, byte[] target) method with Examples
Bytes.indexOf(byte[] array, byte[] target) method of Guava's Bytes Class accepts two parameters array and target. If the target exists within the array, the method returns the start position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(byte[] array, byte[] target) P
3 min read
Java Guava | Doubles.indexOf(double[] array, double[] target) method with Examples
Doubles.indexOf(double[] array, double[] target) method of Guava's Doubles Class accepts two parameters array and target. If the target exists within the array, the method returns the start position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(double[] array, double
3 min read
Java Guava | Chars.indexOf(char[] array, char target) method with Examples
Chars.indexOf(char[] array, char target) method of Guava's Chars Class accepts two parameters array and target. If the target exists within the array, the method returns the position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(char[] array, char target) Parameters:
3 min read
Java Guava | Longs.indexOf(long[] array, long target) method with Examples
Longs.indexOf(long[] array, long target) method of Guava's Longs Class accepts two parameters array and target. If the target exists within the array, the method returns the position of its first occurrence. If the target does not exist within the array, the method returns -1. Syntax: public static int indexOf(long[] array, long target) Parameters:
3 min read
Java.util.Arraylist.indexOf() in Java
The indexOf() method of ArrayList returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. Syntax : public int IndexOf(Object o) obj : The element to search for. // Java code to demonstrate the working of // indexOf in ArrayList // for ArrayList functions import java.util.Arra
2 min read
Java.util.LinkedList.indexOf(), lastIndexof() in Java
Linked list library also offers to depict the first and last index of element that has to be found using the indexOf() and lastIndexOf() functions respectively. They offer a variety as the direct access is not available in the conventionally made linked list, hence knowledge of it is useful. 1. indexOf(Object o) : This method returns the index of t
4 min read
LinkedList indexOf() method in Java
The Java.util.LinkedList.indexOf(Object element) method is used to check and find the occurrence of a particular element in the list. If the element is present then the index of the first occurrence of the element is returned otherwise -1 is returned if the list does not contain the element. Syntax: LinkedList.indexOf(Object element) Parameters: Th
2 min read
Vector indexOf() Method in Java
The java.util.vector.indexOf(Object element) method is used to check and find the occurrence of a particular element in the vector. If the element is present then the index of the first occurrence of the element is returned otherwise -1 is returned if the vector does not contain the element. Syntax: Vector.indexOf(Object element) Parameters: Elemen
3 min read
CopyOnWriteArrayList indexOf() method in Java
The indexOf(Object o) method of CopyOnWriteArrayList returns the first occurrence of the element passed in the list. It returns -1 if the element is not present in the list. Syntax: public int indexOf(Object o) Parameters: The function accepts a parameter o whose first occurrence is to be returned. Return Value: The function returns the first occur
3 min read
Stack indexOf() method in Java with Example
The Java.util.Stack.indexOf(Object element) method is used to check and find the occurrence of a particular element in the Stack. If the element is present then the index of the first occurrence of the element is returned otherwise -1 is returned if the Stack does not contain the element. Syntax: Stack.indexOf(Object element) Parameters: This metho
2 min read
Stack indexOf(Object, int) method in Java with Example
The Java.util.Stack.indexOf(Object element, int index) method is used to the index of the first occurrence of the specified element in this Stack, searching forwards from index, or returns -1 if the element is not found. More formally, returns the lowest index i such that (i &gt;= index &amp;&amp; Objects.equals(o, get(i))), or -1 if there is no su
2 min read
AbstractSequentialList indexOf() method in Java with Example
The indexOf() method of java.util.AbstractSequentialList class is used to return the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index. Syntax: public in
2 min read
Applications of String indexOf() Method in Java
There are four variants of indexOf() method. indexOf(): This method returns the index within this string of the first occurrence of the specified character or -1 if the character does not occur.indexOf(char ch, int start ): This method returns the index within this string of the first occurrence of the specified character, starting the search at th
3 min read
Java String indexOf()
In Java, String indexOf() method returns the position of the first occurrence of the specified character or string in a specified string. Variants of indexOf() Method There are four variants of the indexOf() method are mentioned below: int indexOf()int indexOf(char ch, int strt)int indexOf(String str)int indexOf(String str, int strt)1. int indexOf(
4 min read
Ints indexOf() function | Guava | Java
Guava's Ints.indexOf(int[] array, int target) method returns the index of the first appearance of the value target in array. Syntax: public static int indexOf(int[] array, int target) Parameters: This method accepts the following parameters: array: An array of int values, possibly empty. target: A primitive int value. Return Value: This method retu
2 min read
JavaTuples indexOf() method
The indexOf() method in org.javatuples is used to find the index of a value, passed as parameter, in TupleClass. This method takes the parameter of Object type, so that it can check for all type of values. It is inherited from the JavaTuple class. This method can be used to any tuple class object of javatuples library. It returns an int which is th
2 min read