Java Array Shuffle shuffle(List list)

Here you can find the source of shuffle(List list)

Description

Method to shuffle the list

License

Open Source License

Parameter

Parameter Description
list a parameter

Return

List

Declaration

public static <T> List<T> shuffle(List<T> list) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;

import java.util.List;
import java.util.Random;

public class Main {
    /**/*from   w  w  w .  ja v a2  s. c  o  m*/
     * Method to shuffle the list
     * 
     * @param list
     * @return {@link List<T>}
     */
    public static <T> List<T> shuffle(List<T> list) {
        if (list != null && list.size() > 0) {
            List<T> updatedList = new ArrayList<>();
            final int[] ints = new Random().ints(0, list.size()).distinct().limit(list.size()).toArray();
            for (int i = 0; i < ints.length; i++) {
                updatedList.add(list.get(i));
            }
            return updatedList;
        }
        return null;
    }
}

Related

  1. shuffle(List list)
  2. shuffle(List list)
  3. shuffle(List list, Random rnd)
  4. shuffle(List as)
  5. shuffle(List list)
  6. shuffle(List list, int nswaps)
  7. shuffle(Object[] a)
  8. shuffle(Object[] a, Random r)
  9. shuffle(Object[] array)