Rotate and shuffle a list

static void rotate(List<?> list, int distance)
Rotates the elements in the specified list by the specified distance.
static void shuffle(List<?> list)
Randomly permutes the specified list using a default source of randomness.
static void shuffle(List<?> list, Random rnd)
Randomly permute the specified list using the specified source of randomness.

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class Main {

  public static void main(String args[]) {
    List<Character> list = new LinkedList<Character>();

    for (char n = 'A'; n <= 'Z'; n++)
      list.add(n);

    Collections.shuffle(list);

    System.out.print(list);

  }
}
  

The output:


[L, B, P, U, K, C, O, W, Y, T, G, R, Z, J, I, N, S, A, Q, D, H, X, F, M, E, V]

Shuffle elements in ArrayList


import java.util.ArrayList;
import java.util.Collections;

public class Main {
  public static void main(String[] args) {
    ArrayList arrayList = new ArrayList();
    arrayList.add("A");
    arrayList.add("B");
    arrayList.add("C");
    arrayList.add("D");
    arrayList.add("java2s.com");
    System.out.println("Before shuffling: " + arrayList);
    Collections.shuffle(arrayList);
    System.out.println("After shuffling: " + arrayList);
  }
}

The output:


Before shuffling: [A, B, C, D, java2s.com]
After shuffling: [java2s.com, B, C, A, D]

Shuffle elements of Vector


import java.util.Collections;
import java.util.Vector;

public class Main {
  public static void main(String[] args) {
    Vector v = new Vector();
    v.add("1");
    v.add("2");
    v.add("3");
    v.add("4");
    v.add("java2s.com");
    System.out.println("Before shuffling: " + v);
    Collections.shuffle(v);
    System.out.println("After shuffling: " + v);
  }
}

The output:


Before shuffling: [1, 2, 3, 4, java2s.com]
After shuffling: [3, 4, 2, java2s.com, 1]

Rotate a list


import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class Main {
  public static void main(String args[]) {
    List<Character> list = new LinkedList<Character>();

    for (char n = 'A'; n <= 'F'; n++)
      list.add(n);

    System.out.println("Here is the original list: ");
    System.out.println(list);
    Collections.rotate(list, 2);

    System.out.println(list);

  }
}
  

The output:


Here is the original list: 
[A, B, C, D, E, F]
[E, F, A, B, C, D]
Home 
  Java Book 
    Collection  

Collections:
  1. Collections
  2. Get empty collection from Collections
  3. Do binary search
  4. Copy value to a List
  5. Get Enumeration from collection, create list from Enumeration
  6. Fill a list
  7. Get the max and min value from a Collection
  8. Fill n Copy object to a list
  9. Replace value in a list
  10. Reverse a list
  11. Get comparator in reverse order
  12. Rotate and shuffle a list
  13. Create singleton
  14. Sort a list
  15. Swap element in a list
  16. Get a synchronized collection
  17. Return an unmodifiable view of collections