Skip to content
Related Articles

Related Articles

Java Program to Get Elements By Index from LinkedHashSet
  • Last Updated : 22 Dec, 2020

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:

  1. A naive approach using the iteration count method
  2. Converting LinkedHashSet to Array
  3. Converting LinkedHashSet to List

Method 1: Naive approach using iteration method for index count and to get the element at the given index.

Algorithm



  1. Use iterator to traverse to our LinkedHashSet.
  2. Initiate out index pointer currentindex = 0
  3. Start the iteration using 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:

Java




// Java Program to Get Elements 
// by Index from LinkedHashSet
// using iteration count method
  
// Imporrting generc java libraries
import java.util.*;
import java.io.*;
  
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++;
        }
    }
}
Output
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:

  1. Convert given LinkedHashSet to Array using toArray() method.
  2. Accessing the element on the given index in the array.
Pseudo Code : Integer[] LHSArray = new Integer[LHS.size()];
              LHSArray = LHS.toArray(LHSArray);

Implementation:

Java




// Java Program to Get Elements by Index from LinkedHashSet
// By converting LinkedHashSet to Array
  
// Importing generic java libraries
import java.util.*;
import java.io.*;
  
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]);
    }
}
Output
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

  1. Convert our LinkedHashMap to List like ArrayList.
  2. 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.*;
  
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 retriee 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++;
        }
    }
}
Output
Element at index 4 is : 7

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :