Rotate a List in Java
This post will discuss how to rotate a List in Java. Rotating a list in Java means changing the order of the elements in the list by shifting them to the left or right by a certain number of positions.
There are several ways to rotate a List in Java, depending on whether we want to use built-in methods or write our own logic. Here are some of the most common ways to rotate a list in Java:
1. Using Collections.rotate() method
The rotate() a built-in method from the java.util.Collections class that reorders the elements of a list by moving them to the right by the specified distance. If the distance is negative, the elements are moved to the left instead. This method modifies the original list and does not create a new one. For example, the following code left-rotate a list by 2 positions:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Collections; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { public static void main(String[] args) { List<Integer> list = IntStream.rangeClosed(1, 5) .boxed().collect(Collectors.toList()); int leftShiftDistance = 2; System.out.println("Before rotating: " + list); // [1, 2, 3, 4, 5] Collections.rotate(list, leftShiftDistance); System.out.println("After rotating: " + list); // [4, 5, 1, 2, 3] } } |
To right shift an array by specific positions, we can pass the negative distance to the Collections.rotate() method. For example, this code is the same as the previous method, but with a negative distance to rotate the elements to the right.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.util.Collections; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { public static void main(String[] args) { List<Integer> list = IntStream.rangeClosed(1, 5) .boxed().collect(Collectors.toList()); int rightShiftDistance = 2; System.out.println("Before rotating: " + list); // [1, 2, 3, 4, 5] Collections.rotate(list, -rightShiftDistance); System.out.println("After rotating: " + list); // [3, 4, 5, 1, 2] } } |
2. Using a for loop
Finally, we can implement our own custom logic for rotating a list by using a for loop and the Collections.remove() and Collections.add() methods that remove and add elements at the specified indices. We can use this method to perform either a left or right rotation by changing the direction and bounds of the loop. For example, the following code left-shifts elements in the list by specific positions:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; class Main { public static <T> List<T> rotate(List<T> list, int shift) { if (list.size() == 0) { return list; } for (int i = 0; i < shift; i++) { list.add(0, list.remove(list.size() - 1)); } return list; } public static void main(String[] args) { List<Integer> list = IntStream.rangeClosed(1, 5) .boxed().collect(Collectors.toList()); int shiftDistance = 3; System.out.println("Before rotating: " + list); // [1, 2, 3, 4, 5] List<Integer> rotatedList = rotate(list, shiftDistance); System.out.println("After rotating: " + rotatedList); // [3, 4, 5, 1, 2] } } |
That’s all about rotating a List in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)