📌  相关文章
📜  如何从Java中的 LinkedHashSet 中获取随机元素?

📅  最后修改于: 2022-05-13 01:54:22.338000             🧑  作者: Mango

如何从Java中的 LinkedHashSet 中获取随机元素?

LinkedHashSet用于维护插入顺序,为了从 LinkedHashSet 生成随机元素,我们将使用Random Class生成一个介于 0 和 LinkedHashSet 大小之间的随机数。该随机数将作为 LinkedHashSet 的索引。

我们可以通过三种方式获取随机元素:

  1. 通过使用 for 循环遍历 LinkedHashSet 并获取随机元素。
  2. 通过将 LinkedHashSet 存储在数组中并获取随机数索引处的元素。
  3. 通过将 LinkedHashSet 存储在 ArrayList 中并在随机数位置获取元素。

示例 1:

  • 我们将生成从 0 到 LinkedHashSet 大小的随机元素。
  • 然后我们将使用 for 循环遍历 LinkedHashSet 并检查第 i索引是否等于随机数,然后打印该元素。
Java
// Java program to get the randome element
// from LinkedHashSet
  
import java.util.LinkedHashSet;
import java.util.Random;
  
class GFG {
    public static void main(String[] args)
    {
        LinkedHashSet 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("Restaurent Style Chilli Paneer");
        cset.add("Restaurent 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++;
        }
    }
}


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 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("Restaurent Style Chilli Paneer");
        cset.add("Restaurent 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]);
    }
}


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 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("Restaurent Style Chilli Paneer");
        cset.add("Restaurent 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));
    }
}


输出
Paneer Butter Masala

示例 2:

  • 在这种方法中,我们将 LinkedHashSet 存储在数组中并返回随机元素索引处的元素。

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 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("Restaurent Style Chilli Paneer");
        cset.add("Restaurent 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]);
    }
}
输出
Achari Paneer Gravy

示例 3:

  • 在这种方法中,我们将 LinkedHashSet 存储在 ArrayList 中并返回出现在随机元素位置的元素。

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 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("Restaurent Style Chilli Paneer");
        cset.add("Restaurent 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));
    }
}
输出
Malai Kofta Curry