Find index of an element in LinkedList in Java example

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

publicintindexOf(Objecto)

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

publicintlastIndexOf(Objecto)

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

importjava.util.LinkedList;

publicclassFindElementIndexInLinkedListExample{

publicstaticvoidmain(String[]args){

//create new LinkedList object

LinkedList<Integer>linkedListNumbers=newLinkedList<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

*/

intindex=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.

About the author

RahimV

My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.