Take n random elements from a List<E>?
问题 How can I take n random elements from an ArrayList<E> ? Ideally, I\'d like to be able to make successive calls to the take() method to get another x elements, without replacement. 回答1: Two main ways. Use Random#nextInt(int): List<Foo> list = createItSomehow(); Random random = new Random(); Foo foo = list.get(random.nextInt(list.size())); It's however not guaranteed that successive n calls returns unique elements. Use Collections#shuffle(): List<Foo> list = createItSomehow(); Collections