Thursday, July 4, 2024
HomeLanguagesJavaJava.util.Collections.rotate() Method in Java with Examples

Java.util.Collections.rotate() Method in Java with Examples

java.util.Collections.rotate() method is present in java.util.Collections class. It is used to rotate the elements present in the specified list of Collection by a given distance.

Syntax:
public static void rotate(List< type > list, int distance)
Parameters : 
list - the list to be rotated.
distance - the distance to rotate the list. 
type - Type of list to be rotated. Examples of 
       types are Integer, String, etc.
Returns :
NA
Throws:
UnsupportedOperationException - if the specified list or 
its list-iterator does not support the set operation.

There are no constraints on distance value. It may be zero, negative, or greater than list.size(). After calling this method, the element at index i will be the element previously at index (i – distance) mod list.size(), for all values of i between 0 and list.size()-1, inclusive.




// Java program to demonstrate working of 
// java.utils.Collections.rotate()
  
import java.util.*;
   
public class RotateDemo
{
    public static void main(String[] args)
    {
        // Let us create a list of strings
        List<String>  mylist = new ArrayList<String>();
        mylist.add("practice");
        mylist.add("code");
        mylist.add("quiz");
        mylist.add("neveropen");
   
        System.out.println("Original List : " + mylist);
   
        // Here we are using rotate() method
        // to rotate the element by distance 2
        Collections.rotate(mylist, 2);
   
        System.out.println("Rotated List: " + mylist);
    }
}


Output:

Original List : [practice, code, quiz, neveropen]
Rotated List: [quiz, neveropen, practice, code]

How to quickly rotate an array in Java using rotate()?

Arrays class in Java doesn’t have rotate method. We can use Collections.rotate() to quickly rotate an array also.




// Java program to demonstrate rotation of array
// with Collections.rotate()
import java.util.*;
   
public class RotateDemo
{
    public static void main(String[] args)
    {
        // Let us create an array of integers
        Integer arr[] = {10, 20, 30, 40, 50};
   
        System.out.println("Original Array : " +
                                Arrays.toString(arr));
           
        // Please refer below post for details of asList()
        // rotating an array by distance 2
        Collections.rotate(Arrays.asList(arr), 2);
           
        System.out.println("Modified Array : " +
                                Arrays.toString(arr));
    }
}


Output:

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

This article is contributed by Gaurav Miglani. If you like Lazyroar and would like to contribute, you can also write an article using contribute.neveropen.co.za or mail your article to contribute@neveropen.co.za. See your article appearing on the Lazyroar main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Nokonwaba Nkukhwana
Experience as a skilled Java developer and proven expertise in using tools and technical developments to drive improvements throughout a entire software development life cycle. I have extensive industry and full life cycle experience in a java based environment, along with exceptional analytical, design and problem solving capabilities combined with excellent communication skills and ability to work alongside teams to define and refine new functionality. Currently working in springboot projects(microservices). Considering the fact that change is good, I am always keen to new challenges and growth to sharpen my skills.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments