Home » Java Collections rotate() Method with Examples

Java Collections rotate() Method with Examples

by Online Tutorials Library

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

Test it Now

Output:

Original List : [Java, Python, Cobol, Perl, Android]  Rotated List: [Cobol, Perl, Android, Java, Python]  

Example 2

Test it Now

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

Test it Now

Output:

Original Array: [10, 20, 30, 40, 50]  Modified Array: [30, 40, 50, 10, 20]  

You may also like