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
import java.util.*;
class GFG {
public static void main(String[] args)
{
Set<String> GFG = new HashSet<String>();
GFG.add( "Welcome" );
GFG.add( "To" );
GFG.add( "Geeks" );
GFG.add( "For" );
GFG.add( "Geek" );
System.out.println( "HashSet contains: " + GFG);
String[] Geeks = GFG.toArray( new String[GFG.size()]);
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
- 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
import java.util.*;
class GFG {
public static void main(String[] args)
{
Set<String> GFG = new HashSet<String>();
GFG.add( "Welcome" );
GFG.add( "To" );
GFG.add( "Geeks" );
GFG.add( "For" );
GFG.add( "Geek" );
System.out.println( "HashSet contains: " + GFG);
int currentIndex = 0 ;
int desiredIndex = 3 ;
for (String element :GFG) {
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
import java.util.*;
class GFG {
public static void main(String[] args)
{
Set<String> GFG = new HashSet<String>();
GFG.add( "Welcome" );
GFG.add( "To" );
GFG.add( "Geeks" );
GFG.add( "For" );
GFG.add( "Geek" );
System.out.println( "HashSet contains: " + GFG);
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
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
28 Jan, 2021
Like Article
Save Article