This example shows how to find the index of an element in the LinkedList in Java. This example also shows how to find the index of the first and last occurrence of an element using indexOf and lastIndexOf methods.
How to find the index of an element in the LinkedList in Java?
Use the indexOf
method of the LinkedList class to find the index of the first occurrence of an element in the list.
1 |
public int indexOf(Object o) |
The indexOf
method returns the index of the first occurrence of the specified element object in the list. If the specified element is not found in the list. it returns -1.
Similarly, use the lastIndexOf
method of the LinkedList class to find the last occurrence of the specified element in the list.
1 |
public int lastIndexOf(Object o) |
This method also returns -1 if the element is not found in the list, otherwise, it returns the index of the last occurrence of the specified element in the list.
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 |
import java.util.LinkedList; public class FindElementIndexInLinkedListExample { public static void main(String[] args) { //create new LinkedList object LinkedList<Integer> linkedListNumbers = new LinkedList<Integer>(); //add elements linkedListNumbers.add(1); linkedListNumbers.add(2); linkedListNumbers.add(3); linkedListNumbers.add(1); linkedListNumbers.add(2); linkedListNumbers.add(3); /* * To find first occurrence of an element in LinkedList, use * the indexOf method */ int index = linkedListNumbers.indexOf(3); if(index == - 1){ System.out.println("Element not found"); }else{ System.out.println("Element found at index " + index); } /* * To find last occurrence of an element, use the * lastIndexOf method */ index = linkedListNumbers.lastIndexOf(3); if(index == - 1){ System.out.println("Element not found"); }else{ System.out.println("Last occurrence of an element found at index " + index); } //this will return -1 as the linked list does not contain 4 System.out.println( linkedListNumbers.indexOf(4) ); } } |
Output
1 2 3 |
Element found at index 2 Last occurrence of an element found at index 5 -1 |
How to find the index of a custom class object in the LinkedList?
The indexOf
and lastIndexOf
methods rely on the equals
method to do the equality comparison. In the above example, we have used the Integer class as elements of the LinkedList which has implemented the equals
method.
In the case of the custom class objects, these methods will not work properly if the custom class has not implemented the equals
method. It is because if the custom class has not implemented the equals
method, then the equals
method of the Object class will be used by these methods which only compares the object references, not the actual objects.
The below example shows how to define the equals
and hashCode
methods for the custom class so that the indexOf
and lastIndexOf
methods can be used to find the 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 51 52 53 54 55 56 57 58 59 60 61 |
import java.util.LinkedList; class Empl{ private Integer id; private String name; public Empl(Integer id, String name){ this.id = id; this.name = name; } public String toString(){ return "[" + this.id + " => " + this.name + "]"; } public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); return result; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Empl other = (Empl) obj; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; return true; } } public class FindElementIndexInLinkedListExample { public static void main(String[] args) { LinkedList<Empl> linkedListEmployees = new LinkedList<Empl>(); linkedListEmployees.add( new Empl(1, "Raj") ); linkedListEmployees.add( new Empl(2, "Maria") ); linkedListEmployees.add( new Empl(3, "John") ); linkedListEmployees.add( new Empl(4, "Emily") ); linkedListEmployees.add( new Empl(5, "May") ); linkedListEmployees.add( new Empl(1, "Raj") ); //first index of an element System.out.println( linkedListEmployees.indexOf(new Empl(1, "Raj") ) ); //last index of an element System.out.println( linkedListEmployees.lastIndexOf(new Empl(1, "Raj") ) ); } } |
Output
1 2 |
0 5 |
This example is a part of the LinkedList in Java tutorial.
Please let me know your views in the comments section below.
References:
Java 8 LinkedList