Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Java rotate List

farenda 2015-08-06 0

Problem:

How in Java rotate List elements or move an element to specified position? Java Collections Framework has nice solutions for this problem.

Solution:

In the following example we use Collections.rotate(List), from java.util package, to rotate List elements by specified number of positions:

package com.farenda.java;

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

public class CollectionsRotate {

    public static void main(String[] args) {
        List<String> items = Arrays.asList("a", "b", "c", "d", "e");
        System.out.println("Items: " + items);

        forwardBy(items, 1);

        forwardBy(items, 1);

        backwardBy(items, 2);

        moveXbyY(items, 1, 3);
    }

    private static void moveXbyY(List<String> items, int x, int y) {
        String e = items.get(x);
        // +1 because index 'to' is exclusive
        Collections.rotate(items.subList(x, x+y+1), -1);
        System.out.printf("Move '%s' by %d: %s%n", e, y, items);
    }

    private static void backwardBy(List<String> items, int distance) {
        Collections.rotate(items, -distance);
        System.out.printf("Backward %d: %s%n", distance, items);
    }

    private static void forwardBy(List<String> items, int distance) {
        Collections.rotate(items, distance);
        System.out.printf("Forward %d: %s%n", distance, items);
    }
}

As you can see, to move backward, all you have to do is to provide negative number of steps.

Collections.rotate(List) in conjunction with List.subList(from, to) creates a nice idiom to move an element to specified position. It works fast, because subList creates only a view backed by List, so changes in the view are reflected in the original List.

Here’s the result of running the Java code:

Items: [a, b, c, d, e]
Forward 1: [e, a, b, c, d]
Forward 1: [d, e, a, b, c]
Backward 2: [a, b, c, d, e]
Move 'b' by 3: [a, c, d, e, b]

The Java idiom rotate + subView is worth remembering.

Share with the World!
Categories Java Tags java-collections
Previous: Java reverse Comparator
Next: Java randomly shuffle

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored