get random item from arraylist java code example

Example 1: random.choice in java

String[] available_cards = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Queen", "King", "Jack", "Ace"};  
java.util.Random random = new java.util.Random();
int random_computer_card = random.nextInt(available_cards.length);
System.out.println(available_cards[random_computer_card]);

Example 2: random item from arraylist

ArrayList<Type> arraylistName = new ArrayList<>();

// add items to arraylist
arraylistName.add(item1);
arraylistName.add(item2);
arraylistName.add(item3);

Random random = new Random();
// random index between 0 and arraylistName.size();
int randomIndex = random.nextInt(arraylistName.size());

System.out.println(arraylistName.get(randomIndex));

Example 3: get n random elements from list java

List<Foo> list = createItSomehow();
Collections.shuffle(list);
Foo foo = list.get(0);

Example 4: how to get random element from list in java

// Import all the libraries
import javax.swing.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

// Declare the Class
public class Hello
{  
    // Declare the Main Function
    public static void main(String[] args)
    {
        // Declare a List (Be sure to include java.util.ArrayList; and java.util.List;)
        List<String> l = new ArrayList<>();
        l.add("Quote of the Day \r\nIf you find it in your heart to care for somebody else, you will have succeeded.  \r\nReopen the program for another quote.");
        l.add("Love Quote of the Day \r\nIf there's delight in love, 'Tis when I see that heart, which others bleed for, bleed for me.  \r\nReopen the program for another quote.");
        l.add("Art Quote of the Day \r\nArt is the stored honey of the human soul, gathered on wings of misery and travail.  \r\nReopen the program for another quote.");
        l.add("Nature Quote of the Day \r\nThe earth has received the embrace of the sun and we shall see the results of that love.  \r\nReopen the program for another quote.");
        l.add("Funny Quote Of the Day \r\nThe first condition of understanding a foreign country is to smell it.  \r\nReopen the program for another quote.");
        l.add("Quote of the Day \r\nAnger cannot be dishonest.  \r\nReopen the program for another quote.");
        l.add("Love Quote of the Day \r\nLove is when he gives you a piece of your soul, that you never knew was missing.  \r\nReopen the program for another quote.");
        l.add("Art Quote of the Day \r\nIt is only an auctioneer who can equally and impartially admire all schools of art.  \r\nReopen the program for another quote.");
        l.add("Nature Quote of the Day \r\nNature reserves the right to inflict upon her children the most terrifying jests.  \r\nReopen the program for another quote.");
        l.add("Funny Quote Of the Day \r\nMarriage is not just spiritual communion, it is also remembering to take out the trash.  \r\nReopen the program for another quote.");
        
      
        // Object name should be same as class
        Hello obj = new Hello();
        // Display the input dialog box from JOptionPane
        String inputValue = JOptionPane.showInputDialog("Please enter your name: ");
        // Declare Random object (Be sure to include java.util.Random;)
        Random rand = new Random();
        // Get Random Index from the List
        int randomIndex = rand.nextInt(l.size());
        // Get the element on the random index in the list
        String randomElement = l.get(randomIndex);
        //Display the output dialog box from JOptionPane to display the random element
        JOptionPane.showMessageDialog(null, randomElement + "\r\n\r\nHave a nice day " + inputValue + "!");
    }
    
}