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:
- Using an array
- Using a for loop
- Using ArrayList
Method 1: Using Array
- Import the required Java package java.util
- Declare the HashSet using Set Interface
- Add elements into the HashSet using the add() method
- Display the HashSet to determine order of elements
- Convert HashSet into Array using toArray() method
- 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 ]); } } |
HashSet contains: [Geek, Geeks, For, Welcome, To] Element at index 3 is: Welcome
Method 2: Using a for loop
- 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
- Determine desired index
- 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++; } } } |
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 )); } } |
HashSet contains: [Geek, Geeks, For, Welcome, To] Element at index 3 is: Welcome