Find an element in ArrayList using indexOf lastIndexOf methods example shows how to find element in ArrayList using indexOf and lastIndexOf methods in Java. It also shows how to find an object of a custom class in ArrayList by implementing equals and hashCode methods.
How to find an element in ArrayList using the indexOf lastIndexOf methods in Java?
The ArrayList indexOf
method returns the index of an element in the ArrayList object.
1 |
public int indexOf(Object o) |
This method returns the index of the first occurrence of the specified object in the ArrayList. If the specified object is not found, the indexOf
method returns -1.
Similarly, use the lastIndexOf
method of the ArrayList class to find an element’s last index.
1 |
public int lastIndexOf(Object o) |
This method returns the index of the last occurrence of the specified object in the ArrayList. If the specified object is not found in the ArrayList, this method returns -1.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; public class ArrayListFindElementExample { public static void main(String[] args) { //create new ArrayList ArrayList<String> aListColors = new ArrayList<String>(); aListColors.add("Red"); aListColors.add("Green"); aListColors.add("Blue"); aListColors.add("Red"); aListColors.add("Yellow"); /* * To find element index in ArrayList, * use indexOf method. */ System.out.println("ArrayList indexOf example"); int index = aListColors.indexOf("Blue"); System.out.println("Blue is at index " + index + " in ArrayList"); index = aListColors.indexOf("Red"); System.out.println("Red is at index " + index + " in ArrayList"); index = aListColors.lastIndexOf("Violet"); System.out.println("Violet is at index " + index + " in ArrayList"); /* * To find last occurrence of element in ArrayList * use lastIndexOf method */ System.out.println("ArrayList lastIndexOf example"); index = aListColors.lastIndexOf("Blue"); System.out.println("Blue last index is " + index + " in ArrayList"); index = aListColors.lastIndexOf("Red"); System.out.println("Red last index is " + index + " in ArrayList"); } } |
Output
1 2 3 4 5 6 7 |
ArrayList indexOf example Blue is at index 2 in ArrayList Red is at index 0 in ArrayList Violet is at index -1 in ArrayList ArrayList lastIndexOf example Blue last index is 2 in ArrayList Red last index is 3 in ArrayList |
How to find element index in ArrayList of custom class objects?
Consider below given example which uses an ArrayList of custom class objects.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; public class ArrayListFindElementExample { public static void main(String[] args) { ArrayList<Customer> aListCustomers = new ArrayList<Customer>(); aListCustomers.add(new Customer(1, "A")); aListCustomers.add(new Customer(2, "B")); aListCustomers.add(new Customer(3, "C")); aListCustomers.add(new Customer(4, "D")); //find customer object in ArrayList int index = aListCustomers.indexOf(new Customer(2, "B")); System.out.println("Object index: " + index); } } class Customer{ int id; String name; public Customer(){} public Customer(int id, String name){ this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString(){ return id + ":" + name; } } |
Output
1 |
Object index: -1 |
Even if the object was in the ArrayList, the indexOf
method could not find it and returned -1. That is because the indexOf
and lastIndexOf
methods rely on the equals
method to compare the objects and since we have not implemented the equals
method in the Customer class, it could not find it.
Here is the updated version of Customer class which implements equals
and hashCode
methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
package com.javacodeexamples.collections.arraylist; import java.util.ArrayList; public class ArrayListFindElementExample { public static void main(String[] args) { ArrayList<Customer> aListCustomers = new ArrayList<Customer>(); aListCustomers.add(new Customer(1, "A")); aListCustomers.add(new Customer(2, "B")); aListCustomers.add(new Customer(3, "C")); aListCustomers.add(new Customer(4, "D")); //find customer object in ArrayList int index = aListCustomers.indexOf(new Customer(2, "B")); System.out.println("Object index: " + index); } } class Customer{ int id; String name; public Customer(){} public Customer(int id, String name){ this.id = id; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString(){ return id + ":" + name; } public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Customer other = (Customer) obj; if (id != other.id) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } } |
Output
1 |
Object index: 1 |
This time the indexOf
method worked and returned the right index for the object.
This example is a part of the Java ArrayList tutorial.
Please let me know your views in the comments section below.
its always return -1.
“That is because indexOf and lastIndexOf method relies on equals method to compare the objects”. You need to implement equals and hashcode method for it to work in your class. Make sure that these methods are implemented properly.
Thank you sir
Glad to help. Do check out the quiz section Arun.