Example usage for java.util Collections shuffle

List of usage examples for java.util Collections shuffle

Introduction

In this page you can find the example usage for java.util Collections shuffle.

Prototype

public static void shuffle(List<?> list) 

Source Link

Document

Randomly permutes the specified list using a default source of randomness.

Usage

From source file:Main.java

public static void main(String[] args) {
    ArrayList<String> arrayList = new ArrayList<String>();

    arrayList.add("A");
    arrayList.add("B");
    arrayList.add("C");
    arrayList.add("D");
    arrayList.add("E");

    System.out.println(arrayList);

    Collections.shuffle(arrayList);

    System.out.println(arrayList);

}

From source file:AlgorithmsDemo.java

public static void main(String args[]) {

    LinkedList<Integer> ll = new LinkedList<Integer>();
    ll.add(-8);//w  ww  . j ava  2s.c o m
    ll.add(20);
    ll.add(-20);
    ll.add(8);

    Comparator<Integer> r = Collections.reverseOrder();

    Collections.sort(ll, r);

    for (int i : ll)
        System.out.print(i + " ");

    Collections.shuffle(ll);

    System.out.print("List shuffled: ");
    for (int i : ll)
        System.out.print(i + " ");

    System.out.println("Minimum: " + Collections.min(ll));
    System.out.println("Maximum: " + Collections.max(ll));
}

From source file:Main.java

public static void main(String args[]) {

    List<Character> ll = new LinkedList<Character>();

    for (char n = 'A'; n <= 'F'; n++)
        ll.add(n);//  ww  w. j av a 2 s. c  o  m

    System.out.println(ll);
    Collections.reverse(ll);

    System.out.println(ll);
    Collections.rotate(ll, 2);

    System.out.println(ll);

    Collections.shuffle(ll);

    System.out.println(ll);
}

From source file:Main.java

public static void main(String args[]) {
    // create array list object       
    List<String> arrlist = new ArrayList<String>();

    // populate the list
    arrlist.add("A");
    arrlist.add("B");
    arrlist.add("from java2s.com");

    System.out.println("Initial collection: " + arrlist);

    // shuffle the list
    Collections.shuffle(arrlist);

    System.out.println("Final collection after shuffle: " + arrlist);
}

From source file:Main.java

public static void main(String args[]) {

    List<Character> ll = new LinkedList<Character>();

    for (char n = 'A'; n <= 'F'; n++)
        ll.add(n);//from  w ww. j  a  va  2s  .c  om

    System.out.println("Here is the original list: ");
    for (Character x : ll)
        System.out.print(x + " ");
    Collections.reverse(ll);

    System.out.println("Here is the reversed list: ");
    for (Character x : ll)
        System.out.print(x + " ");
    Collections.rotate(ll, 2);

    for (Character x : ll)
        System.out.print(x + " ");

    Collections.shuffle(ll);

    System.out.println("Here is the randomized list:");
    for (Character x : ll)
        System.out.print(x + " ");
}

From source file:MainClass.java

public static void main(String args[]) {
    LinkedList<Integer> ll = new LinkedList<Integer>();
    ll.add(-8);//from   w w w . j av  a 2 s  . co  m
    ll.add(20);
    ll.add(-20);
    ll.add(8);

    Comparator<Integer> r = Collections.reverseOrder();

    Collections.sort(ll, r);

    System.out.print("List sorted in reverse: ");
    for (int i : ll) {
        System.out.print(i + " ");
    }

    System.out.println();

    Collections.shuffle(ll);

    System.out.print("List shuffled: ");
    for (int i : ll)
        System.out.print(i + " ");

    System.out.println();

    System.out.println("Minimum: " + Collections.min(ll));
    System.out.println("Maximum: " + Collections.max(ll));
}

From source file:ShuffleTest.java

public static void main(String[] args) {
    List<Integer> numbers = new ArrayList<Integer>();
    for (int i = 1; i <= 49; i++)
        numbers.add(i);//from w ww . java 2  s  .c o m
    Collections.shuffle(numbers);
    List<Integer> winningCombination = numbers.subList(0, 6);
    Collections.sort(winningCombination);
    System.out.println(winningCombination);
}

From source file:Shuffle.java

public static void main(String[] args) {
    List<String> list = Arrays.asList(args);
    Collections.shuffle(list);
    System.out.println(list);//from w  w w .ja  va2  s . c  om
}

From source file:Ran.java

public static void main(String[] args) {

    // Get and shuffle the list of arguments
    List<String> argList = Arrays.asList(args);
    Collections.shuffle(argList);

    // Print out the elements using JDK 8 Streams
    argList.stream().forEach(e -> System.out.format("%s ", e));

    // Print out the elements using for-each
    for (String arg : argList) {
        System.out.format("%s ", arg);
    }//from w ww.ja  va2  s. c  o  m

    System.out.println();
}

From source file:Deal.java

public static void main(String[] args) {
    if (args.length < 2) {
        System.out.println("Usage: Deal hands cards");
        return;/*  w  w w . j a  va  2  s  .  c  om*/
    }
    int numHands = Integer.parseInt(args[0]);
    int cardsPerHand = Integer.parseInt(args[1]);
    List<Card> deck = Deck.newDeck();
    Collections.shuffle(deck);
    if (numHands * cardsPerHand > deck.size()) {
        System.out.println("Not enough cards.");
        return;
    }

    for (int i = 0; i < numHands; i++)
        System.out.println(dealHand(deck, cardsPerHand));
}