Example usage for java.util Collections rotate

List of usage examples for java.util Collections rotate

Introduction

In this page you can find the example usage for java.util Collections rotate.

Prototype

public static void rotate(List<?> list, int distance) 

Source Link

Document

Rotates the elements in the specified list by the specified distance.

Usage

From source file:Main.java

public static void main(String[] args) {
    List numbers = new ArrayList();

    for (int i = 0; i < 25; i++) {
        numbers.add(i);/*from   w  ww .j a v  a 2 s.c o m*/
    }

    System.out.println(Arrays.toString(numbers.toArray()));

    Collections.rotate(numbers, 10);

    System.out.println(Arrays.toString(numbers.toArray()));
}

From source file:Main.java

public static void main(String args[]) {

    List<Character> ll = new LinkedList<Character>();

    for (char n = 'A'; n <= 'F'; n++)
        ll.add(n);//from w  w  w . ja  va 2  s. c o  m

    System.out.println(ll);
    Collections.reverse(ll);

    System.out.println(ll);
    Collections.rotate(ll, 2);

    System.out.println(ll);

    Collections.shuffle(ll);

    System.out.println(ll);
}

From source file:Main.java

public static void main(String[] args) {
    List<String> list = new ArrayList<>();
    list.add("Java");
    list.add("R");
    list.add("CSS");
    list.add("XML");

    Collections.sort(list);/*from   www .  j  a v  a2s .c  o m*/
    System.out.println("List: " + list);

    // Rotate elements by 2
    Collections.rotate(list, 2);
    System.out.println("After  Rotating by  2: " + list);

}

From source file:Main.java

public static void main(String[] args) {
    // create array list object
    List<Integer> numbers = new ArrayList<Integer>();

    // populate the list
    for (int i = 0; i < 15; i++) {
        numbers.add(i);/*w  w w  . j  a va2s . c o  m*/
    }

    System.out.println("Before : " + Arrays.toString(numbers.toArray()));

    // rotate the list at distance 10
    Collections.rotate(numbers, 10);

    System.out.println("After : " + Arrays.toString(numbers.toArray()));
}

From source file:Main.java

public static void main(String args[]) {

    List<Character> ll = new LinkedList<Character>();

    for (char n = 'A'; n <= 'F'; n++)
        ll.add(n);/*from   w ww  .j  ava 2  s .c o m*/

    System.out.println("Here is the original list: ");
    for (Character x : ll)
        System.out.print(x + " ");
    Collections.reverse(ll);

    System.out.println("Here is the reversed list: ");
    for (Character x : ll)
        System.out.print(x + " ");
    Collections.rotate(ll, 2);

    for (Character x : ll)
        System.out.print(x + " ");

    Collections.shuffle(ll);

    System.out.println("Here is the randomized list:");
    for (Character x : ll)
        System.out.print(x + " ");
}

From source file:Utilities.java

public static void main(String[] args) {
    List list = Arrays.asList("one Two three Four five six one".split(" "));
    System.out.println(list);/*from  w ww .j a  v  a2s .c o  m*/
    System.out.println("max: " + Collections.max(list));
    System.out.println("min: " + Collections.min(list));
    AlphabeticComparator comp = new AlphabeticComparator();
    System.out.println("max w/ comparator: " + Collections.max(list, comp));
    System.out.println("min w/ comparator: " + Collections.min(list, comp));
    List sublist = Arrays.asList("Four five six".split(" "));
    System.out.println("indexOfSubList: " + Collections.indexOfSubList(list, sublist));
    System.out.println("lastIndexOfSubList: " + Collections.lastIndexOfSubList(list, sublist));
    Collections.replaceAll(list, "one", "Yo");
    System.out.println("replaceAll: " + list);
    Collections.reverse(list);
    System.out.println("reverse: " + list);
    Collections.rotate(list, 3);
    System.out.println("rotate: " + list);
    List source = Arrays.asList("in the matrix".split(" "));
    Collections.copy(list, source);
    System.out.println("copy: " + list);
    Collections.swap(list, 0, list.size() - 1);
    System.out.println("swap: " + list);
    Collections.fill(list, "pop");
    System.out.println("fill: " + list);
    List dups = Collections.nCopies(3, "snap");
    System.out.println("dups: " + dups);
    // Getting an old-style Enumeration:
    Enumeration e = Collections.enumeration(dups);
    Vector v = new Vector();
    while (e.hasMoreElements())
        v.addElement(e.nextElement());
    // Converting an old-style Vector
    // to a List via an Enumeration:
    ArrayList arrayList = Collections.list(v.elements());
    System.out.println("arrayList: " + arrayList);

}

From source file:Main.java

/**
 * Shift object to the top of list/*from  www.j  a  v  a  2s . co m*/
 *
 * @param list  list
 * @param index object index
 */
public static void shiftItemToFront(List<?> list, int index) {
    if (index >= 0) {
        Collections.rotate(list.subList(index, list.size()), -1);
    }
}

From source file:Main.java

/**
 * Shift object to the bottom of list// w  w w  .j a  v  a 2s. co m
 *
 * @param list  list
 * @param index object index
 */
public static void shiftItemToBack(List<?> list, int index) {
    if (index >= 0) {
        Collections.rotate(list.subList(0, index + 1), 1);
    }
}

From source file:Main.java

public static void move(List<?> collection, int indexToMoveFrom, int indexToMoveAt) {
    if (indexToMoveAt >= indexToMoveFrom) {
        Collections.rotate(collection.subList(indexToMoveFrom, indexToMoveAt + 1), -1);
    } else {//from www .  j a  v  a  2  s. c om
        Collections.rotate(collection.subList(indexToMoveAt, indexToMoveFrom + 1), 1);
    }
}

From source file:Main.java

/**
 * Shift object in the list//from  w  w w.j ava  2  s .c  o  m
 *
 * @param list   list
 * @param index  object index
 * @param offset offset value can be positive or negative.
 * @return new index of the object
 */
public static int shiftItem(List<?> list, int index, int offset) {
    if (offset == 0 || index < 0) {
        return 0;
    }
    if (offset > 0) {
        int end = index + offset + 1;
        if (end > list.size()) {
            end = list.size();
        }
        Collections.rotate(list.subList(index, end), -1);
        return end - 1;
    } else {
        int start = index + offset;
        if (start < 0) {
            start = 0;
        }
        Collections.rotate(list.subList(start, index + 1), 1);
        return start;
    }
}