We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 30 May 2015

How to rotate element of List

Following example shows how to rotate the element of List using Collections.rotate(list, distance); method in Java.

void java.util.Collections.rotate(List<?> list, int distance)
rotate
public static void rotate(List<?> list,int distance) 
Rotates the elements in the specified list by the specified distance. 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. (This method has no effect on the size of the list.) For example, suppose list comprises [t, a, n, k, s]. After invoking Collections.rotate(list, 1) (or Collections.rotate(list, -4)), list will comprise [s, t, a, n, k].

Note that this method can usefully be applied to sublists to move one or more elements within a list while preserving the order of the remaining elements. For example, the following idiom moves the element at index j forward to position k (which must be greater than or equal to j):
Collections.rotate(list.subList(j, k+1), -1);
To make this concrete, suppose list comprises [a, b, c, d, e]. To move the element at index 1 (b) forward two positions, perform the following invocation:
Collections.rotate(l.subList(1, 4), -1);The resulting list is [a, c, d, b, e].

To move more than one element forward, increase the absolute value of the rotation distance. To move elements backward, use a positive shift distance.
If the specified list is small or implements the RandomAccess interface, this implementation exchanges the first element into the location it should go, and then repeatedly exchanges the displaced element into the location it should go until a displaced element is swapped into the first element. If necessary, the process is repeated on the second and successive elements, until the rotation is complete. If the specified list is large and doesn't implement the RandomAccess interface, this implementation breaks the list into two sublist views around index -distance mod size. Then the reverse(List) method is invoked on each sublist view, and finally it is invoked on the entire list. For a more complete description of both algorithms, see Section 2.3 of Jon Bentley's Programming Pearls (Addison-Wesley, 1986).
Parameters:
list - the list to be rotated.
distance - the distance to rotate the list. There are no constraints on this value; it may be zero, negative, or greater than list.size().
Throws:
    UnsupportedOperationException - if the specified list or its list-iterator does not support the set operation.

Sample Program
RotateCollection.java
package com.javatutorialcorner.collection;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class RotateCollection {

 public static void main(String[] args) {
  String[] nameArray = new String[] { "Spring", "Struts", "Hibernate",
    "Webservice", "SOAP Webservice", "RESTful Webservice" };
  List<String> nameList = Arrays.asList(nameArray);

  System.out.println("*********** Before Rotate ************");
  for (String name : nameList) {
   System.out.println(name);
  }
  
  Collections.rotate(nameList,3);
  System.out.println("*********** After Rotate ************");
  
  for (String name : nameList) {
   System.out.println(name);
  }

 }

}


Result
The above code will produce the following output.
*********** Before Rotate ************
Spring
Struts
Hibernate
Webservice
SOAP Webservice
RESTful Webservice
*********** After Rotate ************
Webservice
SOAP Webservice
RESTful Webservice
Spring
Struts
Hibernate

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to rotate element of List Rating: 5 Reviewed By: eHowToNow