Close

Java Collections - Collections.rotate() Examples

Java Collections Java Java API 


Class:

java.util.Collections

java.lang.Objectjava.lang.Objectjava.util.Collectionsjava.util.CollectionsLogicBig

Method:

public static void rotate(List<?> list,
                          int distance)

Moves each element in the specified list by the specified distance, the elements whose index becomes more than the list length are moved to the start of the list.


Examples


package com.logicbig.example.collections;

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

public class RotateExample {

public static void main(String... args) {
List<String> list = Arrays.asList("one", "two", "three", "four");
System.out.println(list);
Collections.rotate(list, 2);
System.out.println(list);
}
}

Output

[one, two, three, four]
[three, four, one, two]




See Also