Example 1 — Rotate Right and Rotate Left
Rotate two copies of the same five letters: one by 2, the other by -1.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class CollectionsRotateBasic {
public static void main(String[] args) {
List<String> start = Arrays.asList("A", "B", "C", "D", "E");
List<String> right = new ArrayList<>(start);
Collections.rotate(right, 2);
System.out.println("rotate 2: " + right);
List<String> left = new ArrayList<>(start);
Collections.rotate(left, -1);
System.out.println("rotate -1: " + left);
}
} How It Works
- Right by 2. A moves from index 0 to 2, B to 3, C to 4; D and E fall off the end and wrap to indexes 0 and 1.
- Left by 1. A negative distance shifts toward the front: B becomes first and A wraps around to the last position.
- Separate copies. Each rotation starts from its own copy of A to E, so the two results do not affect each other.

