Java Collection Tutorial - Java Collections.rotate(List <?> list, int distance)








Syntax

Collections.rotate(List <?> list, int distance) has the following syntax.

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

Example

In the following code shows how to use Collections.rotate(List <?> list, int distance) method.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
// w ww .j a  va 2  s.  co  m
public class Main {
   public static void main(String[] args) {
      // create array list object
      List<Integer> numbers = new ArrayList<Integer>();
      
      // populate the list
      for (int i = 0; i < 15; i++) {
         numbers.add(i);
      }

      System.out.println("Before : "+Arrays.toString(numbers.toArray()));
      
      // rotate the list at distance 10
      Collections.rotate(numbers, 10);

      System.out.println("After : "+Arrays.toString(numbers.toArray()));
   }
}

The code above generates the following result.