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:
- By traversing through the LinkedHashSet using for loop and getting the random element.
- By storing the LinkedHashSet in array and getting the element present at random number index.
- By storing the LinkedHashSet in ArrayList and getting the element at a random number position.
Example 1:
- We will generate the random element from 0 to size of LinkedHashSet size.
- Then we will traverse the LinkedHashSet using for loop and check if the ith index is equal to the random number then print that element.
Java
// Java program to get the random element // from LinkedHashSet import java.util.LinkedHashSet;
import java.util.Random;
class GFG {
public static void main(String[] args)
{
LinkedHashSet<String> cset = new LinkedHashSet<>();
// Adding elements to LinkedHashSet
cset.add( "Paneer Butter Masala" );
cset.add( "Paneer Lababdar" );
cset.add( "Kadai Paneer Gravy" );
cset.add( "Malai Kofta Curry" );
cset.add( "Palak Paneer" );
cset.add( "Mughlai Shahi Paneer" );
cset.add( "Restaurant Style Chilli Paneer" );
cset.add( "Restaurant Style Matar Paneer" );
cset.add( "Paneer Tikka Masala" );
cset.add( "Paneer Makhani" );
cset.add( "Achari Paneer Gravy" );
// This will generate a random number
// between 0 and LinkedHashSet size()
Random random = new Random();
int randomNumber = random.nextInt(cset.size());
// maintaining the index
int count = 0 ;
for (String s : cset) {
// when ever our index counter variable "count"
// will be equal to the random number it will
// print the element at that position in the
// LinkedHashSet and after printing we will break
// the loop
if (count == randomNumber) {
System.out.println(s);
break ;
}
count++;
}
}
} |
Output
Paneer Butter Masala
Example 2:
- In this approach, we would store the LinkedHashSet in the array and return the element present at random element index.
Java
// Java program to get the random element // from LinkedHashSet import java.util.LinkedHashSet;
import java.util.Random;
class GFG {
public static void main(String[] args)
{
LinkedHashSet<String> cset = new LinkedHashSet<>();
// Adding elements to the LinkedHashSet
cset.add( "Paneer Butter Masala" );
cset.add( "Paneer Lababdar" );
cset.add( "Kadai Paneer Gravy" );
cset.add( "Malai Kofta Curry" );
cset.add( "Palak Paneer" );
cset.add( "Mughlai Shahi Paneer" );
cset.add( "Restaurant Style Chilli Paneer" );
cset.add( "Restaurant Style Matar Paneer" );
cset.add( "Paneer Tikka Masala" );
cset.add( "Paneer Makhani" );
cset.add( "Achari Paneer Gravy" );
Random random = new Random();
int randomNumber = random.nextInt(cset.size());
// creating an array so that we can store
// the content of LinkedHashSet
String[] csetArray = new String[cset.size()];
csetArray = cset.toArray(csetArray);
// printing the element present at position
// randoNumber in array csetArray
System.out.println(csetArray[randomNumber]);
}
} |
Output
Achari Paneer Gravy
Example 3:
- In this approach, we would store the LinkedHashSet in the ArrayList and return the element present at a random element position.
Java
// Java program to get the random element // from LinkedHashSet import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.Random;
class GFG {
public static void main(String[] args)
{
LinkedHashSet<String> cset = new LinkedHashSet<>();
// Adding elements to the ArrayList
cset.add( "Paneer Butter Masala" );
cset.add( "Paneer Lababdar" );
cset.add( "Kadai Paneer Gravy" );
cset.add( "Malai Kofta Curry" );
cset.add( "Palak Paneer" );
cset.add( "Mughlai Shahi Paneer" );
cset.add( "Restaurant Style Chilli Paneer" );
cset.add( "Restaurant Style Matar Paneer" );
cset.add( "Paneer Tikka Masala" );
cset.add( "Paneer Makhani" );
cset.add( "Achari Paneer Gravy" );
Random random = new Random();
int randomNumber = random.nextInt(cset.size());
// Creating an ArrayList and storing all
// element of LinkedHashSet into ArrayList
ArrayList clist = new ArrayList<>(cset);
// Printing the content of arraylist which is
// present at index random number
System.out.println(clist.get(randomNumber));
}
} |
Output
Malai Kofta Curry