Home  >  Core Java

Example of Collections.rotate in Java

By Arvind Rai, May 31, 2013
On this page we will provide example of Collections.rotate in java. Collections.rotate() method rotates the elements of a java list for the given distance. We can also rotate the sub list for the given distance within a list. According to java doc, the method syntax is as follows.
public static void rotate(List<?> list,  int distance) 
The distance will vary from 0 to list.size() -1. For the example, suppose we have a list {A B C} and if we are rotating the list for the distance -1 and 1 respectively, the result will be as follows.
1. {A   B  C} after rotate for distance= -1
   {B  C  A}

 2. {A   B  C} after rotate for distance= 1
   {C  A   B} 
Now find the example.
CollectionsRotate
package com.concretepage.util;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CollectionsRotate {
    public static void main(String[] args) {
        User u1 = new User("A");
        User u2 = new User("B");
        User u3 = new User("C");
        List<User> list = new ArrayList<User>();
        list.add(u1);
        list.add(u2);
        list.add(u3);
        System.out.println("before rotate");
        for(User user: list){
            System.out.print(user.getName()+" ");
        }
        System.out.println("\nafter rotate by distnace 2");
        Collections.rotate(list, 2);
        for(User user: list){
            System.out.print(user.getName()+" ");
        }
        System.out.println("\nafter rotate by distnace -1");
        Collections.rotate(list, -1);
        for(User user: list){
            System.out.print(user.getName()+" ");
        }        
    }
}
User.java
package com.concretepage.util;
public class User {
   private String name;
   public User(String name) {
       this.name = name;
   }
   public String getName() {
       return name;
   }
}
Find the output.
before rotate
A B C 
after rotate by distnace 2
B C A 
after rotate by distnace -1
C A B 

Collections.rotate with Sub List

Now we will rotate a sub list within a list. Find the example.
RotateSubList.java
package com.concretepage.util;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class RotateSubList {
    public static void main(String[] args) {
    	List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8);
        System.out.println("List before rotate");
        for(Integer i: list){
            System.out.print(i+" ");
        }
        System.out.println("\nSublist list.subList(2, 5)");
        for(Integer i: list.subList(2, 5)){
            System.out.print(i+" ");
        }        
        System.out.println("\nList after rotating sublist by distnace 1");
        Collections.rotate(list.subList(2, 5), 1);
        for(Integer i: list){
            System.out.print(i+" ");
        }
    }
}
Find the output.
List before rotate
1 2 3 4 5 6 7 8 
Sublist list.subList(2, 5)
3 4 5 
List after rotating sublist by distnace 1
1 2 5 3 4 6 7 8 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us