Java Program to Get Elements By Index from LinkedHashSet
Last Updated :
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 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
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
LinkedHashSet<Integer> LHS = new LinkedHashSet<>();
LHS.add( 2 );
LHS.add( 3 );
LHS.add( 4 );
LHS.add( 2 );
LHS.add( 7 );
int index = 4 ;
Iterator<Integer> it = LHS.iterator();
int currIndex = 0 ;
Integer CurrentElement = null ;
while (it.hasNext()) {
CurrentElement = it.next();
if (currIndex == index - 1 ) {
System.out.println( "Element at index "
+ index + " is : "
+ CurrentElement);
break ;
}
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:
- 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
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
LinkedHashSet<Integer> LHS = new LinkedHashSet<>();
LHS.add( 2 );
LHS.add( 3 );
LHS.add( 4 );
LHS.add( 2 );
LHS.add( 7 );
int index = 4 ;
Integer[] LHSArray = new Integer[LHS.size()];
LHSArray = LHS.toArray(LHSArray);
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
- 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
import java.util.*;
import java.io.*;
class GFG {
public static void main(String[] args)
{
LinkedHashSet<Integer> LHS = new LinkedHashSet<>();
LHS.add( 2 );
LHS.add( 3 );
LHS.add( 4 );
LHS.add( 2 );
LHS.add( 7 );
int index = 4 ;
Iterator<Integer> it = LHS.iterator();
int currIndex = 0 ;
Integer CurrentElement = null ;
while (it.hasNext()) {
CurrentElement = it.next();
if (currIndex == index - 1 ) {
System.out.println( "Element at index "
+ index + " is : "
+ CurrentElement);
break ;
}
currIndex++;
}
}
}
|
Output
Element at index 4 is : 7
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...