Java SE API
Collections.rotate
Rotate positions by the supplied distance, wrapping around the list. Requires suitable mutation support.
Formal Syntax & Structure
java syntaxjava
static void rotate(List<?> list, int distance)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| list | List<?> | List to rearrange. |
| distance | int | Positive moves right; negative moves left. |
Working Example
Example Snippetjava
import java.util.*;public class Main {
public static void main(String[] args) { List<String> stops = new ArrayList<>(List.of("B", "A", "B", "C")); System.out.println(Collections.frequency(stops, "B")); System.out.println(Collections.min(stops)); System.out.println(Collections.max(stops)); Collections.swap(stops, 0, 1); Collections.reverse(stops); System.out.println(stops); Collections.rotate(stops, 1); System.out.println(stops); }}