Java Array Shuffle shuffle(Collection objects)

Here you can find the source of shuffle(Collection objects)

Description

Creates a new shuffled list containing the objects in the given collection.

License

Open Source License

Parameter

Parameter Description
T the type of objects being shuffled
objects the objects

Return

the new shuffled list

Declaration

public static <T> List<T> shuffle(Collection<T> objects) 

Method Source Code


//package com.java2s;
/*/*from w w  w. j  a va 2 s . co m*/
 * Copyright 2012 Michael Roberts
 * All rights reserved.
 *
 *
 * This file is part of xutil.
 *
 * xutil is free software: you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as published by the 
 * Free Software Foundation, either version 3 of the License, or (at your 
 * option) any later version.
 *
 * xutil is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License 
 * along with xutil.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

import java.util.List;

import java.util.Random;

public class Main {
    /** The Constant RANDOM. */
    private static final Random RANDOM = new Random();

    /**
     * Creates a new shuffled list containing the objects in the given
     * collection.
     *
     * @param <T> the type of objects being shuffled
     * @param objects the objects
     * @return the new shuffled list
     */
    public static <T> List<T> shuffle(Collection<T> objects) {
        return shuffle(objects, Collections.<T>emptyList());
    }

    /**
     * Creates a new shuffled list containing the objects in the given
     * collection, minus those in the ignores.
     *
     * @param <T> the type of objects being shuffled
     * @param objects the objects
     * @param ignores the ignores
     * @return the new shuffled list
     */
    public static <T> List<T> shuffle(Collection<T> objects, T... ignores) {
        return shuffle(objects, Arrays.asList(ignores));
    }

    /**
     * Creates a new shuffled list containing the objects in the given
     * collection, minus those in the ignores.
     *
     * @param <T> the type of objects being shuffled
     * @param objects the objects
     * @param ignores the ignores
     * @return the new shuffled list
     */
    public static <T> List<T> shuffle(Collection<T> objects, Collection<T> ignores) {
        List<T> ok = new ArrayList<T>(objects);
        ok.removeAll(ignores);
        Collections.shuffle(ok, RANDOM);
        return ok;
    }
}

Related

  1. randomize(final T[] array)
  2. shuffle()
  3. shuffle(byte[] array)
  4. shuffle(byte[] values, Random rnd)
  5. shuffle(Collection items, Random random)
  6. shuffle(double[] arr, Random rnd)
  7. Shuffle(double[] array)
  8. shuffle(double[] array, Random r)
  9. shuffle(double[][] kernel, long seed)