Open In App

Java Program to Rotate Elements of the List

List is an ordered collection of objects in which duplicate values can be stored. Since List preserves the insertion order, it allows positional access and insertion of elements. In this article, we are going to see how to rotate elements of a list. Let’s consider the following list.

ORIGINAL LIST BEFORE ROTATION

There are two types of rotations in a list. They are right rotation and left rotation. After Four Right Rotations the list becomes as shown below:



LIST AFTER  ROTATING FOUR POSITIONS

Method 1: (Without Using in-built methods)

Working For Right Rotation



Example:




// Java Program to Rotate Elements of the List
import java.io.*;
import java.util.*;
class GFG {
    
    public static void main(String[] args)
    {
        // creating ArrayList
        List<Integer> my_list = new ArrayList<>();
        my_list.add(10);
        my_list.add(20);
        my_list.add(30);
        my_list.add(40);
        my_list.add(50);
        my_list.add(60);
        my_list.add(70);
  
        // Printing list before rotation
        System.out.println(
            "List Before Rotation : "
            + Arrays.toString(my_list.toArray()));
  
        // Loop according to the number of rotations
        for (int i = 0; i < 4; i++) {
            
            // storing the last element in the list
            int temp = my_list.get(6);
            
            // traverse the list and move elements to right
            for (int j = 6; j > 0; j--) {
                my_list.set(j, my_list.get(j - 1));
            }
            my_list.set(0, temp);
        }
  
        // Printing list after rotation
        System.out.println(
            "List After Rotation :  "
            + Arrays.toString(my_list.toArray()));
    }
}

Output
List Before Rotation : [10, 20, 30, 40, 50, 60, 70]
List After Rotation :  [40, 50, 60, 70, 10, 20, 30]

After Four Left Rotations the list becomes as shown below:

LIST AFTER ROTATING FOUR POSITIONS

Working For Left Rotation

Example




// Java Program to Rotate Elements of the List
  
import java.io.*;
import java.util.*;
class GFG {
    public static void main(String[] args)
    {
        // creating array list
        List<Integer> my_list = new ArrayList<>();
        my_list.add(10);
        my_list.add(20);
        my_list.add(30);
        my_list.add(40);
        my_list.add(50);
        my_list.add(60);
        my_list.add(70);
  
        // Printing list before rotation
        System.out.println(
            "List Before Rotation : "
            + Arrays.toString(my_list.toArray()));
  
        // Loop according to the number of rotations
        for (int i = 0; i < 4; i++) {
            // storing the first element in the list
            int temp = my_list.get(0);
            // traverse the list and move elements to left
            for (int j = 0; j < 6; j++) {
                my_list.set(j, my_list.get(j + 1));
            }
            my_list.set(6, temp);
        }
  
        // Printing list after rotation
        System.out.println(
            "List After Rotation :  "
            + Arrays.toString(my_list.toArray()));
    }
}

Output
List Before Rotation : [10, 20, 30, 40, 50, 60, 70]
List After Rotation :  [50, 60, 70, 10, 20, 30, 40]

Method 2: (Rotation Using Collections.rotate(list, distance) method)

Both left and right rotations can be performed directly using Java Collections.

Syntax

Collections.rotate(list_name , distance)

Parameters: 

Returns: It returns a rotated list.

Note: Negative Distance gives Left Rotation while Positive gives Right Rotation.

Example: Using Positive distance to get the Right Rotation.




// Java Program to Rotate Elements of the List
  
import java.io.*;
import java.util.*;
class GFG {
    public static void main(String[] args)
    {
        // creating array list
        List<Integer> my_list = new ArrayList<>();
        my_list.add(10);
        my_list.add(20);
        my_list.add(30);
        my_list.add(40);
        my_list.add(50);
        my_list.add(60);
        my_list.add(70);
  
        // Printing list before rotation
        System.out.println(
            "List Before Rotation : "
            + Arrays.toString(my_list.toArray()));
  
        // Rotating the list at distance 4
  
        Collections.rotate(my_list, 4);
  
        // Printing list after rotation
        System.out.println(
            "List After Rotation :  "
            + Arrays.toString(my_list.toArray()));
    }
}

Output
List Before Rotation : [10, 20, 30, 40, 50, 60, 70]
List After Rotation :  [40, 50, 60, 70, 10, 20, 30]

Example: Using Negative distance to get the Left Rotation.




// Java Program to Rotate Elements of the List
  
import java.io.*;
import java.util.*;
class GFG {
    public static void main(String[] args)
    {
        // creating array list
        List<Integer> my_list = new ArrayList<>();
        my_list.add(10);
        my_list.add(20);
        my_list.add(30);
        my_list.add(40);
        my_list.add(50);
        my_list.add(60);
        my_list.add(70);
  
        // Printing list before rotation
        System.out.println(
            "List Before Rotation : "
            + Arrays.toString(my_list.toArray()));
  
        // Rotating the list at distance -3
        Collections.rotate(my_list, -4);
  
        // Printing list after rotation
        System.out.println(
            "List After Rotation :  "
            + Arrays.toString(my_list.toArray()));
    }
}

Output
List Before Rotation : [10, 20, 30, 40, 50, 60, 70]
List After Rotation :  [50, 60, 70, 10, 20, 30, 40]

Article Tags :