Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

Java offers us a range of methods for rearranging items in an ArrayList. In this tutorial, we’ll look at three of them.

2. Moving an Item

The most manual approach, and the one that gives us the most control, is moving an item directly to a new position. We can do this by firstly using ArrayList.remove(), which returns the removed item. Then we can reinsert that item at a position of our choosing with ArrayList.add():

@Test
void givenAList_whenManuallyReordering_thenOneItemMovesPosition() {
    ArrayList<String> arrayList = new ArrayList<>(Arrays.asList("one", "two", "three", "four", "five"));

    String removed = arrayList.remove(3);
    arrayList.add(2, removed);

    ArrayList<String> expectedResult = new ArrayList<>(Arrays.asList("one", "two", "four", "three", "five"));
    assertEquals(expectedResult, arrayList);
}

Under the hood, ArrayList uses an array. This means that removing and inserting items has a large overhead from shifting all the other items around. For that reason, we should look to avoid this method if possible and use one of the two below, which both keep the ArrayList at its original length.

3. Swapping Two Items

We can use Collections.swap() to swap the positions of two items in an ArrayList. The swap() method takes three arguments, firstly the ArrayList to adjust, followed by the positions of the two items to swap:

@Test
public void givenAList_whenUsingSwap_thenItemsSwapPositions() {
    ArrayList<String> arrayList = new ArrayList<>(Arrays.asList("one", "two", "three", "four", "five"));

    Collections.swap(arrayList, 1, 3);

    ArrayList<String> expectedResult = new ArrayList<>(Arrays.asList("one", "four", "three", "two", "five"));
    assertEquals(expectedResult, arrayList);
}

Here we’ve swapped the items in positions 1 and 3 and confirmed the list looks as we expected.

4. Rotating the Full List

Finally, we can also apply a rotation to a list, shifting all elements by the given distance. There’s no limit on the distance. Therefore we can loop everything around several times if we want. A positive distance will rotate items to the right or down the list depending on our perspective:

@Test
void givenAList_whenUsingRotateWithPositiveDistance_thenItemsMoveToTheRight() {
    ArrayList<String> arrayList = new ArrayList<>(Arrays.asList("one", "two", "three", "four", "five"));

    Collections.rotate(arrayList, 2);

    ArrayList<String> expectedResult = new ArrayList<>(Arrays.asList("four", "five", "one", "two", "three"));
    assertEquals(expectedResult, arrayList);
}

Here the items have all shifted two places to the right, looping back to the start once they reach the end. Alternatively, we can also rotate to the left with a negative distance if required:

@Test
void givenAList_whenUsingRotateWithNegativeDistance_thenItemsMoveToTheLeft() {
    ArrayList<String> arrayList = new ArrayList<>(Arrays.asList("one", "two", "three", "four", "five"));

    Collections.rotate(arrayList, -2);

    ArrayList<String> expectedResult = new ArrayList<>(Arrays.asList("three", "four", "five", "one", "two"));
    assertEquals(expectedResult, arrayList);
}

5. Conclusion

In this article, we learned about three of the options Java gives us for reordering an ArrayList. We should look to use either swap() or rotate() if possible for performance reasons. If we need more control or if only a single item is moving, then we learned how to manually shift an item to any position we need using remove() and add().

As always, the full code for the examples is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are closed on this article!