Java Program to Get Elements By Index from LinkedHashSet - GeeksforGeeks

28 Jul, 2021
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 methodConverting LinkedHashSet to ArrayConverting LinkedHashSet to List
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 = 0Start the iteration using a while loop and if the current index becomes equal to the given index print the element.
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 librariesimport java.io.*;import java.util.*; // Main classclass 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++;        }    }}
Element 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.
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 librariesimport java.io.*;import java.util.*; // Main classclass 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]);    }}
Element 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 LinkedHashMap to List like ArrayList.Using get() method to get an element in a given index.
Convert our LinkedHashMap 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 librariesimport java.util.*;import java.io.*; // Main classclass 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;         // Converting LinkedHashSet to List        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()) {            CurrentElement = it.next();             if (currIndex == index - 1) {                 // Printing desired value at index in array,                // chosen above index from LinkedHashap                System.out.println("Element at index "                                   + index + " is : "                                   + CurrentElement);                break;            }             // Incrementing the current index            currIndex++;        }    }}
Element at index 4 is : 7
saurabh1990aror
skhasanj300
java-LinkedHashSet
Picked
Technical Scripter 2020
Java
Java Programs
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Different ways of Reading a text file in Java
Constructors in Java
Stream In Java
Exceptions in Java
StringBuilder Class in Java with Examples
Convert a String to Character array in Java
Java Programming Examples
Implementing a Linked List in Java using Class
Convert Double to Integer in Java
How to Iterate HashMap in Java?