Java Collection How to - Rotate a List








Question

We would like to know how to rotate a List.

Answer

//from w  w w .j a va2 s .  c o  m
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class Main {

  public static void main(String args[]) {

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

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

    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);
  }
}

The code above generates the following result.