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)
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));
}
}
OutputLinkedHashSet: [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;
}
}
OutputLinkedHashSet: [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);
}
}
OutputLinkedHashSet: [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
Similar Reads
How to Get the Last Element from LinkedHashSet in Java?
The LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. When the iteration order is needed to be maintained this class is used. When iterating through a HashSet the order is unpredictable, while a LinkedHashSet lets us iterate through the elements
2 min read
How to Print LinkedHashSet Elements in Java?
LinkedHashSet is a child class of HashSet in which duplicates are not allowed but the insertion order is preserved. The elements are printed in the same order in which they were inserted. There are several ways to print LinkedHashSet elements: By simply printing the elementsBy using enhanced for loo
4 min read
Java Program to Get Elements By Index from LinkedHashSet
LinkedHashSet is a pre-defined class in Java that is similar to HashSet. Unlike HashSet In LinkedHashSet insertion order is preserved. In order to get element by Index from LinkedHashSet in Java, we have multiple ways.Illustration:Input : 2, 3, 4, 2, 7;Processing : index = 4;Output : Element at inde
4 min read
How to Get Random Elements from LinkedHashSet in Java?
LinkedHashSet is used to maintain the insertion order and for generating random elements from LinkedHashSet we will use Random Class to generate a random number between 0 and the LinkedHashSet size. That random number will act as the index of LinkedHashSet. We can get a random element in three ways:
3 min read
How to Find the Minimum or Maximum Element from LinkedHashSet in Java?
Given a LinkedHashSet the task is to find the minimum and maximum element from this LinkedHashSet in Java. There are two ways to find the minimum or maximum element from LinkedHashSet in Java as follows Using the min and max method of the Collections classBy Iterating the LinkedHashSetExamples: Inpu
2 min read
How to Sort LinkedHashSet Elements in Descending Order in Java?
The LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. When the iteration order is needed to be maintained this class is used. When iterating through a HashSet the order is unpredictable, while a LinkedHashSet iteration is through the elements in
2 min read
How to Remove Elements from a LinkedHashMap in Java?
A LinkedHashMap is a part of the Collection Framework from java.util package Java and is similar to a HashMap, except that a LinkedHashMap preserves the insertion order among the keys/entries. In this article, we will look at how to remove the elements from a LinkedHashMap in Java. Program to Remove
2 min read
Checking if Element Exists in LinkedHashSet in Java
The Java.util.LinkedHashSet.contains() method is used to check whether a specific element is present in the LinkedHashSet or not. So basically it is used to check if a Set contains any particular element. The contains a method of the LinkedHashSet class returns true if the specified element is found
1 min read
How to Convert ArrayList to LinkedHashSet in Java?
ArrayList is a data structure that overcomes the shortcomings of the common array in Java wherein the size has to be explicitly specified beforehand. The length of the array data structure cannot be modified which is taken care of the ArrayList data structure. This data structure is also known as th
8 min read
How to Check if LinkedHashMap is Empty in Java?
The LinkedHashMap is just like HashMap with an additional feature of maintaining an order of elements inserted into it. HashMap provided the advantage of quick insertion, search, and deletion but it never maintained the track and order of insertion which the LinkedHashMap provides where the elements
2 min read