Monday, December 30, 2024
Google search engine
HomeLanguagesJavaHow to Get Elements By Index from HashSet in Java?

How to Get Elements By Index from HashSet in Java?

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
Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments