How to Get Random Elements from Java HashSet? Last Updated : 07 Jan, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report Unlike List classes, the HashSet class does not provide any methods using which we can get the elements using their index. It makes it difficult to get random elements from it using the index. We need to get random elements from HashSet, which can be done by either of the two ways: By converting it to an arrayUsing an Iterator or a for loop Example: Input: hs.add(11); hs.add(24); hs.add(34); hs.add(43); hs.add(55); hs.add(66); hs.add(72); hs.add(80); hs.add(99); Output: Random element: 99 Method 1: By converting to an array. Firstly convert HashSet into an array and then access the random element from it.Then we will create an object of Random class and will call the nextInt() method of that class which will give us any random number less than or equal to the size of the HashSet.And then using an array we will simply print the element present at that index. Java // Java program to get random elements from HashSet // using an array import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { // creating the HashSet Set<Integer> hs = new HashSet<Integer>(); hs.add(11); hs.add(24); hs.add(34); hs.add(43); hs.add(55); hs.add(66); hs.add(72); hs.add(80); hs.add(99); // convert HashSet to an array Integer[] arrayNumbers = hs.toArray(new Integer[hs.size()]); // generate a random number Random rndm = new Random(); // this will generate a random number between 0 and // HashSet.size - 1 int rndmNumber = rndm.nextInt(hs.size()); // get the element at random number index System.out.println("Random element: " + arrayNumbers[rndmNumber]); } } OutputRandom element: 11 Method 2: Using an Iterator or a for loop In order to get random elements from the HashSet object, we need to generate a random number between 0 (inclusive) and the size of the HashSet (exclusive).And then iterate through the set till we reach the element located at the random number position as given below.In this approach, we will get the element at a random index using an Iterator. Java // Java program to get random elements from HashSet // using an Iterator import java.io.*; import java.util.*; class GFG { public static void main(String[] args) { Set<Integer> hs = new HashSet<Integer>(); hs.add(11); hs.add(24); hs.add(34); hs.add(43); hs.add(55); hs.add(66); hs.add(72); hs.add(80); hs.add(99); System.out.println("Random element: " + getRandomElement(hs)); } private static <E> E getRandomElement(Set<? extends E> set) { Random random = new Random(); // Generate a random number using nextInt // method of the Random class. int randomNumber = random.nextInt(set.size()); Iterator<? extends E> iterator = set.iterator(); int currentIndex = 0; E randomElement = null; // iterate the HashSet while (iterator.hasNext()) { randomElement = iterator.next(); // if current index is equal to random number if (currentIndex == randomNumber) return randomElement; // increase the current index currentIndex++; } return randomElement; } } OutputRandom element: 99 Comment More infoAdvertise with us Next Article Finding Maximum Element of Java HashSet K kaaruni1124 Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Java-Collections java-hashset +2 More Practice Tags : JavaJava-Collections Similar Reads 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 implement 3 min read Java Program to Get First or Last Elements from HashSet The HashSet class implements the Set interface, backed by a hash table which is actually a HashMap instance. There is no guarantee made for the iteration order of the set which means that the class does not guarantee the constant order of elements over time. This class permits the null element. The 3 min read How to Get Random Elements from LinkedHashSet in Java? LinkedHashSet is used to maintain the insertion order and for generating random elements from LinkedHashSet we will use Random Class to generate a random number between 0 and the LinkedHashSet size. That random number will act as the index of LinkedHashSet. We can get a random element in three ways: 3 min read Finding Maximum Element of Java HashSet Java HashSet class is used to create collection to be used by the collection that uses a hash table for storage purposes that uses the mechanism known as hashing. The implementation class of Set. It inherits the abstract class and implements et interface. The main trait is it does not allow duplicat 4 min read How to Get the Last Element from LinkedHashSet in Java? The LinkedHashSet is an ordered version of HashSet that maintains a doubly-linked List across all elements. When the iteration order is needed to be maintained this class is used. When iterating through a HashSet the order is unpredictable, while a LinkedHashSet lets us iterate through the elements 2 min read How to Convert a HashSet to JSON in Java? In Java, a HashSet is an implementation of the Set interface that uses a hash table to store elements. It allows fast lookups and does not allow duplicate elements. Elements in a HashSet are unordered and can be of any object type. In this article, we will see how to convert a HashSet to JSON in Jav 2 min read Like