This example shows how to get random elements from LinkedHashSet in Java. This example also shows how to get random elements from LinkedHashSet using an iterator, for loop, and array.
How to get random elements from LinkedHashSet in Java?
There are various ways using which we can get the random elements from the LinkedHashSet as given below.
1. Using the for loop and Random class
We will first generate a random number between 0 and LinkedHashSet size using the nextInt
method of the Random class. Once we get the number, we will iterate the LinkedHashSet elements using the enhanced for loop (or an iterator). When we reach the index that is equal to the random number we generated, we will return the element.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
import java.util.LinkedHashSet; import java.util.Random; import java.util.Set; public class LinkedHashSetGetRandomElements { public static void main(String[] args) { LinkedHashSet<String> lhSetNumbers = new LinkedHashSet<String>(); lhSetNumbers.add("one"); lhSetNumbers.add("two"); lhSetNumbers.add("three"); lhSetNumbers.add("four"); lhSetNumbers.add("five"); lhSetNumbers.add("six"); lhSetNumbers.add("seven"); lhSetNumbers.add("eight"); lhSetNumbers.add("nine"); lhSetNumbers.add("ten"); //this will generate 5 random elements from the set for(int i = 0; i < 5; i++){ System.out.println( getRandomElement(lhSetNumbers) ); } } private static String getRandomElement(Set<String> set){ /* * Generate a random number between * 0 and LinkedHashSet size */ Random random = new Random(); int randomNumber = random.nextInt( set.size() ); //index counter int currentIndex = 0; //iterate the linked hash set for( String element : set ){ //if we are at index that is equal to a random number if(currentIndex == randomNumber){ return element; } currentIndex++; } return null; } } |
Output
1 2 3 4 5 |
five two four nine one |
2. Using an array
We can also convert the LinkedHashSet to an array and get the random element from the array using the random number generated using the Random class as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
import java.util.LinkedHashSet; import java.util.Random; import java.util.Set; public class LinkedHashSetGetRandomElements { public static void main(String[] args) { LinkedHashSet<String> lhSetNumbers = new LinkedHashSet<String>(); lhSetNumbers.add("one"); lhSetNumbers.add("two"); lhSetNumbers.add("three"); lhSetNumbers.add("four"); lhSetNumbers.add("five"); lhSetNumbers.add("six"); lhSetNumbers.add("seven"); lhSetNumbers.add("eight"); lhSetNumbers.add("nine"); lhSetNumbers.add("ten"); //this will generate 5 random elements from the set for(int i = 0; i < 5; i++){ System.out.println( getRandomElement(lhSetNumbers) ); } } private static String getRandomElement(Set<String> set){ /* * Generate a random number between * 0 and LinkedHashSet size */ Random random = new Random(); int randomNumber = random.nextInt( set.size() ); //convert LinkedHashSet to an array String[] array = new String[ set.size() ]; array = set.toArray(array); //get the element at randomNumber index return array[ randomNumber ]; } } |
Output
1 2 3 4 5 |
one seven five five eight |
3. Using a List
Instead of converting to an array, we can also convert LinkedHashSet to ArrayList (or LinkedList) and get the element at the random index using the get
method as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; import java.util.Random; import java.util.Set; public class LinkedHashSetGetRandomElements { public static void main(String[] args) { LinkedHashSet<String> lhSetNumbers = new LinkedHashSet<String>(); lhSetNumbers.add("one"); lhSetNumbers.add("two"); lhSetNumbers.add("three"); lhSetNumbers.add("four"); lhSetNumbers.add("five"); lhSetNumbers.add("six"); lhSetNumbers.add("seven"); lhSetNumbers.add("eight"); lhSetNumbers.add("nine"); lhSetNumbers.add("ten"); //this will generate 5 random elements from the set for(int i = 0; i < 5; i++){ System.out.println( getRandomElement(lhSetNumbers) ); } } private static String getRandomElement(Set<String> set){ /* * Generate a random number between * 0 and LinkedHashSet size */ Random random = new Random(); int randomNumber = random.nextInt( set.size() ); //convert to an ArrayList List<String> list = new ArrayList( set ); //get the element at random index return list.get(randomNumber); } } |
Output
1 2 3 4 5 |
five two six three seven |
Important Note:
1. If you want to select large number of random elements from the set, you can optimize the array and List approaches by selecting multiple random elements in a single conversion operation.
2. Converting LinkedHashSet to an array or a List needs to allocate resources for the new object which is a costly operation. It may not perform well especially if the LinkedHashSet has a very large number of elements.
This example is a part of the LinkedHashSet in Java Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 LinkedHashSet