Java Collections rotate() Method



Description

The Java Collections rotate(List<?>, int) method is used to rotate the elements in the specified list by the specified distance.

Declaration

Following is the declaration for java.util.Collections.rotate() method.

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

Parameters

  • list − This is the list to be rotated.

  • distance − This is the distance to rotate the list.

Return Value

NA

Exception

UnsupportedOperationException − This is thrown if the specified list or its list-iterator does not support the set operation.

Rotating a List of Integers with Given Distance Example

The following example shows the usage of Java Collection rotate(List,int) method. We've created a List object with some integers, printed the original list. Using rotate(List, int) method, we've rotated elements of the list with given distance and then printed the updated list.

package com.tutorialspoint;

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

public class CollectionsDemo {

   public static void main(String[] args) {
      List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5));

      System.out.println("Initial collection value: " + list);
      // update values of this collection
      Collections.rotate(list, 2);
      System.out.println("Final collection value: "+list);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Initial collection value: [1, 2, 3, 4, 5]
Final collection value: [4, 5, 1, 2, 3]

Rotating a List of Strings with Given Distance Example

The following example shows the usage of Java Collection rotate(List,int) method. We've created a List object with some Strings, printed the original list. Using rotate(List, int) method, we've rotated elements of the list with given distance and then printed the updated list.

package com.tutorialspoint;

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

public class CollectionsDemo {

   public static void main(String[] args) {
      List<String> list = new ArrayList<>(Arrays.asList("A","B","C","D","E","F"));

      System.out.println("Initial collection value: " + list);
      // update values of this collection
      Collections.rotate(list, 2);
      System.out.println("Final collection value: "+list);
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Initial collection value: [A, B, C, D, E, F]
Final collection value: [E, F, A, B, C, D]

Rotating a List of Objects with Given Distance Example

The following example shows the usage of Java Collection rotate(List,int) method. We've created a List object with some Student objects, printed the original list. Using rotate(List, int) method, we've rotated elements of the list with given distance and then printed the updated list.

package com.tutorialspoint;

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

public class CollectionsDemo {

   public static void main(String[] args) {
      List<Student> list = new ArrayList<>(Arrays.asList(new Student(1, "Julie"),
         new Student(2, "Robert"), new Student(3, "Adam")));

      System.out.println("Initial collection value: " + list);
      // update values of this collection
      Collections.rotate(list, 2);
      System.out.println("Final collection value: "+list);
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Initial collection value: [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
Final collection value: [[ 2, Robert ], [ 3, Adam ], [ 1, Julie ]]
java_util_collections.htm
Advertisements