Java Collections rotate() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
java.util.Collections

Definition

In Java, Collections.rotate(list, distance) shifts every element of a List by distance positions, wrapping elements that fall off one end around to the other. After the call, the element that was at index i is at index (i + distance) % size.

A positive distance moves elements toward the end (to the right); a negative distance moves them toward the front (to the left). For example, rotating [A, B, C, D, E] by 2 gives [D, E, A, B, C].

A static method of the java.util.Collections utility class, available since Java 1.4. Call it on the class name, as in Collections.rotate(...), after import java.util.Collections;. You never create a Collections object.

Syntax

Pass the list and how far to shift it.

Java
List<String> letters = new ArrayList<>(Arrays.asList("A", "B", "C", "D", "E"));
Collections.rotate(letters, 2);    // [D, E, A, B, C]
Collections.rotate(letters, -2);   // back to [A, B, C, D, E]
CallWhat it does
Collections.rotate(list, 1)Last element moves to the front.
Collections.rotate(list, -1)First element moves to the back.
Collections.rotate(list, list.size())A full turn: no visible change.
Collections.rotate(list.subList(j, k + 1), -1)Moves the element at j forward to k.

The distance may be zero, negative, or larger than the list size; only distance % size matters. Rotating an empty list does nothing.

Library declarations
Java
// java.util.Collections
public static void rotate(List<?> list, int distance)

Parameters

ParameterDescription
listThe list to rotate. It must support set.
distanceHow many positions to shift. Positive shifts right, negative shifts left.

Return Value

Return ValueDescription
voidNothing. Every element has moved by the distance, wrapping around.

Compare with Similar Methods

Methods that move elements to new positions:

MethodHow it differs
swap(list, i, j)Moves just two elements.
reverse(list)Mirrors the list instead of shifting it.
shuffle(list)Random positions instead of a fixed shift.
pollFirst() + addLastOn an ArrayDeque, a quick one-step left rotation.

Java Examples

Example 1 — Rotate Right and Rotate Left

Rotate two copies of the same five letters: one by 2, the other by -1.

Java
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

  1. 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.
  2. Left by 1. A negative distance shifts toward the front: B becomes first and A wraps around to the last position.
  3. Separate copies. Each rotation starts from its own copy of A to E, so the two results do not affect each other.

Example 2 — Distances Larger Than the List

Rotate a week by 7, then by 10, then by -10, and see that only the remainder matters.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CollectionsRotateModulo {
    public static void main(String[] args) {
        List<String> week = new ArrayList<>(Arrays.asList("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"));

        Collections.rotate(week, 7);
        System.out.println("by 7:   " + week);

        Collections.rotate(week, 10);
        System.out.println("by 10:  " + week);

        Collections.rotate(week, -10);
        System.out.println("by -10: " + week);
    }
}

How It Works

  1. A full lap. Seven is the list size, so every day travels all the way around and lands where it started.
  2. 10 acts like 3. 10 % 7 is 3, so Mon moves from index 0 to 3 and Fri, Sat, Sun wrap to the front.
  3. Undo with the negative. Rotating by -10 is a left shift of 3, which exactly cancels the previous step.

Example 3 — Move One Song Using rotate on a subList

Move the song at index 3 up to index 1 without touching the intro and outro.

Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CollectionsRotateMove {
    public static void main(String[] args) {
        List<String> playlist = new ArrayList<>(Arrays.asList("intro", "song1", "song2", "song3", "outro"));

        Collections.rotate(playlist.subList(1, 4), 1);
        System.out.println(playlist);
    }
}

How It Works

  1. Pick the range. subList(1, 4) is a live window over song1, song2, song3.
  2. Rotate the window by 1. Inside the window, song3 wraps from the last slot to the first, and song1 and song2 each move one step right.
  3. Outside stays fixed. intro and outro are not in the window, so they keep indexes 0 and 4. A distance of -1 would move an element the other way, from the front of the window to its end.

Frequently Asked Questions

It shifts every element of a List by a distance, wrapping elements around the end. The element at index i moves to index (i + distance) mod size.
It rotates to the left: elements move toward the front, and the first elements wrap around to the end.
Only the remainder counts. Rotating a 7-element list by 10 is the same as rotating it by 3.
Rotate a subList that starts or ends at that element. For example, Collections.rotate(list.subList(j, k + 1), -1) moves the element at j forward to k.
No. It returns void and rearranges the list you pass in.

Next: shuffle()

Put the elements of a list in a random order.

shuffle() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful