Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to Find the Element Index in LinkedHashSet in Java?

  • Last Updated : 15 Dec, 2020

LinkedHashSet is used to store distinct items and get the items in which order they were inserted in Java. LinkedHashSet does not store values based on the index. But there are some methods to find the element index in LinkedHashSet in Java.

Method 1: (By converting LinkedHashSet to ArrayList)

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

 To find the element index in LinkedHashSet in Java by converting LinkedHashSet to ArrayList, the process divided into two parts:

1. Convert LinkedHashSet to ArrayList using the constructor.



// Convert LinkedHashSet to ArrayList using constructor
ArrayList<Integer> elements = new ArrayList<>(set);

2. And get the element index using the indexOf() method in Java.

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 the first occurrence of the given element in the list and returns “-1” if the element is not in the list.

Example:

Java




// Java program to find the element
// index in LinkedHashSet
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // New empty HashSet
        LinkedHashSet<Integer> set = new LinkedHashSet<>();
  
        // Add elements to set
        set.add(10);
        set.add(20);
        set.add(10);
        set.add(50);
        set.add(30);
        set.add(20);
        set.add(50);
  
        // Convert LinkedHashSet to ArrayList
        ArrayList<Integer> elements = new ArrayList<>(set);
  
        // Print the LinkedHashSet
        System.out.println("LinkedHashSet: " + set);
  
        // Print index of all the elements
        for (Integer x : elements) {
            System.out.println("Index of " + x + ": "
                               + elements.indexOf(x));
        }
  
        // It returns -1 becouce 60 not present in
        // LinkedHashSet
        System.out.println("Index of " + 60 + ": "
                           + elements.indexOf(60));
    }
}
Output
LinkedHashSet: [10, 20, 50, 30]
Index of 10: 0
Index of 20: 1
Index of 50: 2
Index of 30: 3
Index of 60: -1

Method 2: (Using Iterator or enhanced for loop)

To find the element index in Java, we can create a new user-defined function (indexOf) that returns the given element index. Our function iterate LinkedHashSet and returns the index of the given element.



Note: If the element is not present in the LinkedHashSet, it returns -1.

Example:

Java




// Java program to find the 
// element index in LinkedHashSet
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // New empty HashSet
        LinkedHashSet<Integer> set = new LinkedHashSet<>();
  
        // Add elements to set
        set.add(10);
        set.add(20);
        set.add(10);
        set.add(50);
        set.add(30);
        set.add(20);
        set.add(50);
  
        // Print the LinkedHashSet
        System.out.println("LinkedHashSet: " + set);
  
        // Print index of elements
        for (Integer x : set) {
            System.out.println("Index of " + x + ": "
                               + indexOf(set, x));
        }
  
        // It returns -1 becouce it is not present in
        // LinkedHashSet
        System.out.println("Index of " + 60 + ": "
                           + indexOf(set, 60));
    }
  
    public static int indexOf(LinkedHashSet<Integer> set,
                              int element)
    {
  
        // If element not present in the LinkedHashSet it
        // returns -1
        int index = -1;
  
        // get an iterator
        Iterator<Integer> iterator = set.iterator();
  
        int currentIndex = 0;
        while (iterator.hasNext()) {
  
            // If element present in the LinkedHashSet
            if (iterator.next().equals(element)) {
                index = currentIndex;
                break;
            }
  
            currentIndex++;
        }
  
        // Return index of the element
        return index;
    }
}
Output
LinkedHashSet: [10, 20, 50, 30]
Index of 10: 0
Index of 20: 1
Index of 50: 2
Index of 30: 3
Index of 60: -1

Method 3: (Using an Array) To find the element index in LinkedHashSet in Java using an Array, the process divided into two parts:

1. Convert LinkedHashSet to an Array using toArray() method.

// New Array

Integer[] array = new Integer[set.size()];

// Convert set to Array using toArray
        
array = set.toArray(array);

2. Find the element using an index.

Syntax:

Object[] arr = LinkedHashSet.toArray()

Parameters: The method does not take any parameters.

Return Value: The method returns an array containing the elements similar to the LinkedHashSet.

Example:

Java




// Java program to find the 
// element index in LinkedHashSet
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
    {
  
        // New empty HashSet
        LinkedHashSet<Integer> set = new LinkedHashSet<>();
  
        // Add elements to set
        set.add(10);
        set.add(20);
        set.add(10);
        set.add(50);
        set.add(30);
        set.add(20);
        set.add(50);
  
        // Print the LinkedHashSet
        System.out.println("LinkedHashSet: " + set);
  
        // New Array
        Integer[] array = new Integer[set.size()];
  
        // Convert LinkedHashSet to Array
        array = set.toArray(array);
  
        // Print index of elements
        for (int i = 0; i < array.length; i++) {
            System.out.println("Index of " + array[i] + ":"
                               + i);
        }
  
        int element = 50;
  
        int index = -1;
  
        for (int i = 0; i < array.length; i++) {
  
            // if element is equal to array value
            // store index and come out
            if (array[i] == element) {
                index = i;
                break;
            }
        }
  
        // print index
        System.out.println("Index of " + element
                           + " is : " + index);
    }
}
Output
LinkedHashSet: [10, 20, 50, 30]
Index of 10:0
Index of 20:1
Index of 50:2
Index of 30:3
Index of 50 is : 2



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!