CollectionUtil { public static void move(List<?> collection, int indexToMoveFrom, int indexToMoveAt) { if (indexToMoveAt >= indexToMoveFrom) { Collections.rotate(collection.subList(indexToMoveFrom, indexToMoveAt + 1), -1); } else { Collections.rotate(collection.subList(indexToMoveAt, indexToMoveFrom + 1), 1); } }  static void move(List<?> collection, int indexToMoveFrom, int indexToMoveAt);  }
@Test public void testMoveDown() { List<String> list = Lists.newArrayList("one", "two", "three", "four", "five"); CollectionUtil.move(list, 1, 3); Assert.assertEquals("one", list.get(0)); Assert.assertEquals("three", list.get(1)); Assert.assertEquals("four", list.get(2)); Assert.assertEquals("two", list.get(3)); Assert.assertEquals("five", list.get(4)); list = Lists.newArrayList("one", "two", "three", "four", "five"); CollectionUtil.move(list, 0, 4); Assert.assertEquals("two", list.get(0)); Assert.assertEquals("three", list.get(1)); Assert.assertEquals("four", list.get(2)); Assert.assertEquals("five", list.get(3)); Assert.assertEquals("one", list.get(4)); }
@Test public void testMoveUp() { List<String> list = Lists.newArrayList("one", "two", "three", "four", "five"); CollectionUtil.move(list, 3, 1); Assert.assertEquals("one", list.get(0)); Assert.assertEquals("four", list.get(1)); Assert.assertEquals("two", list.get(2)); Assert.assertEquals("three", list.get(3)); Assert.assertEquals("five", list.get(4)); list = Lists.newArrayList("one", "two", "three", "four", "five"); CollectionUtil.move(list, 4, 0); Assert.assertEquals("five", list.get(0)); Assert.assertEquals("one", list.get(1)); Assert.assertEquals("two", list.get(2)); Assert.assertEquals("three", list.get(3)); Assert.assertEquals("four", list.get(4)); }