Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.ArrayList;

import java.util.Collection;

import java.util.List;

import java.util.Random;

public class Main {
    private static final Random random = new Random();

    public static <O> O getRandom(final Collection<O> pObjects) {
        if (pObjects != null && !pObjects.isEmpty()) {
            int r = Math.abs(random.nextInt() % pObjects.size());
            List<O> objects = !(pObjects instanceof List) ? new ArrayList<O>(pObjects) : (List<O>) pObjects;

            return objects.get(r);
        }
        return null;
    }
}