Javatpoint Logo
Javatpoint Logo

Rotate List in Java

A list is an ordered collection of elements that can be stored the duplicate values. List store elements in the same order in which we insert them, so it maintains the insertion order of elements and allows us to access and insertion of elements based on position.

In this section, we will understand how we can rotate the elements of the list in Java. Suppose we have the following list to rotate:

1 -> 2-> 3-> 4-> 5 -> 6 -> 7

We can perform rotation in two ways, i.e., Right Rotation and Left Rotation. After performing 4 right rotations, list contains elements in the following order:

4 -> 5-> 6-> 7-> 1 -> 2 -> 3

Right Rotation

We can implement right rotation with or without using built-in methods. Let's understand both of the ways one by one:

Right Rotation without using a built-in method

Right Rotation is done by shifting elements to the right side of the list. We use the following steps to perform the right Rotation in a list:

  1. In the first step, we will create a variable temp and store the last element of the list in it.
  2. Next, we will move the elements in one position towards the right.
  3. Now, we replace the value present in the first index in the list with the value available in the temp
  4. Now, the last element of the updated list will be stored in the temp variable.
  5. We will repeat the above steps required a number of rotations.

Let's implement the code for the above theory to perform right rotation without using the built-in method:

RightRotationExample1.java

Output:

Rotate List in Java

Right Rotation by Using a built-in Method

We use the rotate() method of the Collections class to perform the right Rotation in the list. The method accepts two parameters, i.e., a list and a positive integer.

List parameter defines a list in which we need to perform Rotation. The distance parameter is a positive integer that defines the number of Rotations.

Let's take an example to understand how we can use the built-in method to perform the right Rotation.

RightRotationExample2.java

Output:

Rotate List in Java

Left Rotation of the List

We can implement rotation in a Java program with/without using built-in methods. Let's understand both of the ways one by one:

Left Rotation Without using a built-in Method

Left Rotation is done by shifting elements to the left side of the list. We use the following steps to perform the right Rotation in a list:

  1. In the first step, we will create a variable temp and store the first element of the list in it.
  2. Next, we will move the element in one position towards the left.
  3. Now, we replace the value present in the last index in the list with the value available in the temp
  4. Now, the first element of the updated list will be stored in the temp variable.
  5. We will repeat the above steps required a number of rotations.

Let's implement the code for the above theory to perform left rotation without using the built-in method:

LeftRotationExample1.java

Output:

Rotate List in Java

Left Rotation by Using a built-in Method

Just like right Rotation, we also use the rotate() method of the Collections class to perform left Rotation. The only change between right and left rotation is that in the right Rotation, the value of the distance parameter is positive, whereas, in the left Rotation, its value is negative.

Let's take an example to understand how we can use the built-in method to perform left Rotation.

LeftRotationExample2.java

Output:

Rotate List in Java





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA