Java Program to Get Elements By Index from LinkedHashSet
Last Updated :
24 Mar, 2025
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 index 4 is : 7
Methods:
- A naive approach using the iteration count method
- Converting LinkedHashSet to Array
- Converting LinkedHashSet to List
Method 1: Naive approach using iteration method for index count and to get the element at the given index.
Algorithm
- Use iterator to traverse to our LinkedHashSet.
- Initiate out index pointer currentindex = 0
- Start the iteration using a while loop and if the current index becomes equal to the given index print the element.
Pseudo Code:
Iterator<Integer> it = LHS.iterator();
while(it.hasNext()) {}
Implementation:
Example 1
Java
// Java Program to Get Elements by Index from LinkedHashSet
// Using iteration count method
// Importing generic java libraries
import java.io.*;
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Adding elements to LinkedHashSet
// Custom inputs
LinkedHashSet<Integer> LHS = new LinkedHashSet<>();
LHS.add(2);
LHS.add(3);
LHS.add(4);
LHS.add(2);
LHS.add(7);
// Custom index chosen to get the element
// present at that index
int index = 4;
Iterator<Integer> it = LHS.iterator();
// Assigning initial values
int currIndex = 0;
Integer CurrentElement = null;
// Condition check using hasNext(), whick
// returns true if another token as input
while (it.hasNext()) {
// next element using iterator is
// assigned to variable
CurrentElement = it.next();
// Variable condition check
if (currIndex == index - 1) {
System.out.println("Element at index "
+ index + " is : "
+ CurrentElement);
break;
}
// If condition fails, so
// Incrementing current index
currIndex++;
}
}
}
OutputElement at index 4 is : 7
Time Complexity: O(n)
Method 2: LinkedHashSet is converted to Array by which element can be accessed at the given index.
Algorithm:
- Convert given LinkedHashSet to Array using toArray() method.
- Accessing the element on the given index in the array.
Pseudo Code:
Integer[] LHSArray = new Integer[LHS.size()];
LHSArray = LHS.toArray(LHSArray);
Example
Java
// Java Program to Get Elements by Index from LinkedHashSet
// By converting LinkedHashSet to Array
// Importing generic java libraries
import java.io.*;
import java.util.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating a LinkedHashSet
LinkedHashSet<Integer> LHS = new LinkedHashSet<>();
// Adding elements() to LinkedHashSet
LHS.add(2);
LHS.add(3);
LHS.add(4);
LHS.add(2);
LHS.add(7);
// Custom index chosen from LinkedHashSet
int index = 4;
// Converting LnkedHashMap to Array
Integer[] LHSArray = new Integer[LHS.size()];
LHSArray = LHS.toArray(LHSArray);
// Printing desired value at index in array,
// chosen above index from LinkedHashap
System.out.println("Element at index " + index
+ " is : "
+ LHSArray[index - 1]);
}
}
OutputElement at index 4 is : 7
Time Complexity: O(1)
Method 3: LinkedHashSet to be converted to List to get the desired element at the given index.
Algorithm
- Convert our LinkedHashSet to List like ArrayList.
- Using get() method to get an element in a given index.
Pseudo Code : List<Integer> LHSList =new ArrayList<>(LHS);
where LHS is name of our LinkedHashSet
Implementation:
Java
// Java Program to Get Elements by Index from LinkedHashSet
// By converting LinkedHashSet to List
// Importing java generic libraries
import java.util.*;
import java.io.*;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating a LinkedHashSet
LinkedHashSet<Integer> LHS = new LinkedHashSet<>();
// Adding elements to LinkedHashSet
LHS.add(2);
LHS.add(3);
LHS.add(4);
LHS.add(2);
LHS.add(7);
// Custom index chosen to retrieve value
int index = 4; // User-provided index (1-based)
// Step 1: Convert LinkedHashSet to List
List<Integer> LHSList = new ArrayList<>(LHS);
// Step 2: Adjust index to zero-based and check bounds
int zeroBasedIndex = index - 1; // Convert to zero-based index
if (zeroBasedIndex >= 0 && zeroBasedIndex < LHSList.size()) {
System.out.println("Element at index " + index + " is : " + LHSList.get(zeroBasedIndex));
} else {
System.out.println("Index out of bounds");
}
}
}
OutputElement at index 4 is : 7
Similar Reads
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
Java Program to Iterate LinkedHashSet Elements 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
3 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
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
Java Program to Get Elements of a LinkedList Linked List is a linear data structure, in which the elements are not stored at the contiguous memory locations. Here, the task is to get the elements of a LinkedList. 1. We can use get(int variable) method to access an element from a specific index of LinkedList: In the given example, we have used
4 min read
Java Program to Implement LinkedHashSet API 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 element
4 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
How to Find the Element Index in LinkedHashSet in Java? 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 fi
4 min read
Java Program to Get First or Last Elements from HashSet The HashSet class implements the Set interface, backed by a hash table which is actually a HashMap instance. There is no guarantee made for the iteration order of the set which means that the class does not guarantee the constant order of elements over time. This class permits the null element. The
3 min read
Java Program to Implement LinkedHashMap API 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
3 min read