
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to Rotate Elements of a List
The List extends Collection and declares the behavior of a collection that stores a sequence of elements. The Collection is a framework that provides architecture to store and manipulate the group of objects. Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion.
In this article, we will learn to rotate elements of a list in Java. Rotating a list means shifting the elements of the list either to the left or to the right.
Following are the ways to rotate elements of a list in Java:
- Using Collections
- Using Loops
- Using Streams
Rotate Elements of a List Using Collections
We have a built in method in the Collections class called Collections.rotate()
that can be used to rotate the elements of a list. This method takes two parameters: the list to be rotated and the distance by which the elements should be rotated.
Example
Below is an example of rotating elements of a list using Collections. Here we will rotate the elements of a list to the right by 2 positions:
import java.util.ArrayList; import java.util.Collections; import java.util.List; public class RotateList { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("Java"); list.add("Python"); list.add("Scala"); list.add("C++"); System.out.println("Original List: " + list); Collections.rotate(list, 2); System.out.println("Rotated List: " + list); } }
Output
Following is the output of the above code:
Original List: [Java, Python, Scala, C++] Rotated List: [Scala, C++, Java, Python]
Rotate Elements of a List Using Loops
We can also rotate elements of a list using loops. We can create a new list and add elements from the original list based on the rotation distance.
Example
Below is an example of rotating elements of a list using loops. Here we will rotate the elements of a list to the left by 2 positions:
import java.util.ArrayList; import java.util.List; public class RotateListUsingLoops { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("Java"); list.add("Python"); list.add("Scala"); list.add("C++"); System.out.println("Original List: " + list); int distance = 2; List<String> rotatedList = new ArrayList<>(); for (int i = distance; i < list.size(); i++) { rotatedList.add(list.get(i)); } for (int i = 0; i < distance; i++) { rotatedList.add(list.get(i)); } System.out.println("Rotated List: " + rotatedList); } }
Output
Following is the output of the above code:
Original List: [Java, Python, Scala, C++] Rotated List: [Scala, C++, Java, Python]
Rotate Elements of a List Using Streams
Java Streams can also be used to rotate elements of a list. We can use the Stream
API to manipulate the list and make rotation of the elements.
Example
Below is an example of rotating elements of a list using Streams. Here we will rotate the elements of a list to the right by 2 positions:
import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class RotateListUsingStreams { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("Java"); list.add("Python"); list.add("Scala"); list.add("C++"); System.out.println("Original List: " + list); int distance = 2; List<String> rotatedList = new ArrayList<>(); rotatedList = list.stream() .skip(list.size() - distance) .collect(Collectors.toList()); rotatedList.addAll(list.stream() .limit(list.size() - distance) .collect(Collectors.toList())); System.out.println("Rotated List: " + rotatedList); } }
Output
Following is the output of the above code:
Original List: [Java, Python, Scala, C++] Rotated List: [Scala, C++, Java, Python]