java random string from list code example

Example 1: get random element from string array java

String random = (fruits[new Random().nextInt(fruits.length)]);

Example 2: java random word from list

private static String[] names = { "Terminator", "Slicer","Ninja", "cow", "Robot", "littlegirl" };
name = names[(int) (Math.random() * names.length)];
System.out.println(name);

Example 3: get random String from array list

Random r = new Random();

    int randomitem = r.nextInt(myList.size());
    String randomElement = myList.get(randomitem);

Example 4: get random element from string array java

String[] fruits = {"Apple","Mango","Peach","Banana","Orange","Grapes","Watermelon","Tomato"};