73
Java Collections rotate() Method
The rotate() method of Java Collections class is used to rotate the elements in the specified list by a given distance.
Syntax
Following is the declaration of rotate() method:
Parameter
Parameter | Description | Required/Optional |
---|---|---|
list | It is the list which will be rotated. | Required |
distance | It is the distance which will rotate the list. It may be zero, negative, or greater than size of list. | Required |
Returns
The rotate() method does not return anything.
Exceptions
UnsupportedOperationException– It throws this type of exception if the specified list or its list-iterator does not support the set operation.
Compatibility Version
Java 1.4 and above
Example 1
Output:
Original List : [Java, Python, Cobol, Perl, Android] Rotated List: [Cobol, Perl, Android, Java, Python]
Example 2
Output:
Value Before Rotation: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Value After Rotation: [6, 7, 8, 9, 10, 1, 2, 3, 4, 5]
Example 3
Output:
Original Array: [10, 20, 30, 40, 50] Modified Array: [30, 40, 50, 10, 20]
Next TopicJava Collections Class