Example 1 — Swap the First and Last Player
Move the last player to the front and the first to the back in one call.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class CollectionsSwapEnds {
public static void main(String[] args) {
List<String> order = new ArrayList<>(Arrays.asList("Ann", "Bob", "Cat", "Dan"));
System.out.println("Before: " + order);
Collections.swap(order, 0, order.size() - 1);
System.out.println("After: " + order);
}
} How It Works
- Computed last index.
order.size() - 1is 3, the position of Dan. - One exchange. Ann (index 0) and Dan (index 3) trade places.
- Middle untouched. Bob and Cat keep indexes 1 and 2.

