Skip to content
Related Articles

Related Articles

Improve Article

How to Get Elements By Index from HashSet in Java?

  • Difficulty Level : Easy
  • Last Updated : 28 Jan, 2021
Geek Week

HashSet extends AbstractSet and implements the Set interface. It creates a collection that uses a hash table for storage. The class does not guarantee the constant order of elements over time but permits the null element. The underlying data structure for HashSet is Hashtable. HashSet also implements Serializable and Cloneable interfaces.

Declaration of a HashSet:

public class HashSet<E> extends AbstractSet<E> implements Set<E>, Cloneable, Serializable

where E represents the type of elements to be stored in the HashSet.

The three different ways we can get elements by index in a HashSet is by:



  1. Using an array
  2. Using a for loop
  3. Using ArrayList

Method 1: Using Array

  1. Import the required Java package java.util
  2. Declare the HashSet using Set Interface
  3. Add elements into the HashSet using the add() method
  4. Display the HashSet to determine order of elements
  5. Convert HashSet into Array using toArray() method
  6. Access elements by index.

Java




// Java Program to get HashSet elements by index using Array
  
// import required package - java.util
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Declare HashSet using Set Interface
        Set<String> GFG = new HashSet<String>();
  
        // Add elements into HashSet using add()
        GFG.add("Welcome");
        GFG.add("To");
        GFG.add("Geeks");
        GFG.add("For");
        GFG.add("Geek");
  
        // Displaying HashSet
        System.out.println("HashSet contains: " + GFG);
  
        // Notice the order of elements may be different
        // than insertion
  
        // Converting HashSet to Array
        String[] Geeks = GFG.toArray(new String[GFG.size()]);
  
        // Accessing elements by index
        System.out.println("Element at index 3 is: "
                           + Geeks[3]);
    }
}
Output
HashSet contains: [Geek, Geeks, For, Welcome, To]
Element at index 3 is: Welcome

Method 2: Using a for loop

  1. Import required packages – java.util
  2. Declare HashSet using Set Interface
  3. Add elements into the HashSet using the add() method
  4. Display the HashSet to determine order of elements
  5. Determine desired index
  6. Implement a for loop to access elements by index

Java




// Java Program to get elements of HashSet by index using a
// for loop
  
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Declare HashSet using Set Interface
        Set<String> GFG = new HashSet<String>();
  
        // Add elements into HashSet using add()
        GFG.add("Welcome");
        GFG.add("To");
        GFG.add("Geeks");
        GFG.add("For");
        GFG.add("Geek");
  
        // Displaying HashSet
        System.out.println("HashSet contains: " + GFG);
  
        // Notice the order of elements may be different
        // than insertion
  
        // Iterator declaration
        int currentIndex = 0;
  
        // Desired Index
        int desiredIndex = 3;
        
        for (String element :GFG) { 
          // Implementing for loop
  
            if (currentIndex == desiredIndex)
            {
                System.out.println("Element at index 3 is: "+ element);
                break;
            }
            currentIndex++;
        }
    }
}
Output
HashSet contains: [Geek, Geeks, For, Welcome, To]
Element at index 3 is: Welcome

Method 3: Using ArrayList

  • Import required packages java.util
  • Declare HashSet using Set Interface
  • Add elements into the HashSet using the add() method
  • Display the HashSet to determine order of elements
  • Convert HashSet to ArrayList
  • Access elements by index

Java




// Java Program to get elements of HashSet by index using
// ArrayList
  
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Declare HashSet using Set Interface
        Set<String> GFG = new HashSet<String>();
  
        // Add elements into HashSet using add()
        GFG.add("Welcome");
        GFG.add("To");
        GFG.add("Geeks");
        GFG.add("For");
        GFG.add("Geek");
  
        // Displaying HashSet
        System.out.println("HashSet contains: " + GFG);
  
        // Notice the order of elements may be different
        // than insertion
  
        // Converting HashSet to ArrayList
        List<String> list = new ArrayList<String>(GFG);
  
        System.out.println("Element at index 3 is: "
                           + list.get(3));
    }
}
Output
HashSet contains: [Geek, Geeks, For, Welcome, To]
Element at index 3 is: Welcome

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 :