Open In App

Java Program to Get Elements By Index from LinkedHashSet

Last Updated : 28 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 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++;
        }
    }
}


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);

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]);
    }
}


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.*;
 
// 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;
 
        // 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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads