How to Use Collections.reverse in Java: Simple Guide
Use
Collections.reverse(List<T> list) to reverse the order of elements in a list in Java. This method modifies the original list in place, so no new list is created.Syntax
The method Collections.reverse takes a single argument, which is a List of any type. It reverses the order of elements inside the list directly.
Collections.reverse(list): Reverses the elements oflistin place.list: AListobject whose elements you want to reverse.
java
Collections.reverse(list);Example
This example shows how to reverse a list of strings using Collections.reverse. The original list is changed after calling the method.
java
import java.util.*; public class ReverseExample { public static void main(String[] args) { List<String> fruits = new ArrayList<>(Arrays.asList("Apple", "Banana", "Cherry", "Date")); System.out.println("Original list: " + fruits); Collections.reverse(fruits); System.out.println("Reversed list: " + fruits); } }
Output
Original list: [Apple, Banana, Cherry, Date]
Reversed list: [Date, Cherry, Banana, Apple]
Common Pitfalls
Common mistakes when using Collections.reverse include:
- Trying to reverse an immutable or fixed-size list, which causes
UnsupportedOperationExceptionif you try to add or remove elements. - Expecting
Collections.reverseto return a new list instead of modifying the original. - Passing
nullas the list, which causesNullPointerException.
Always ensure the list is mutable and not null.
java
import java.util.*; public class ReversePitfall { public static void main(String[] args) { // Arrays.asList returns a fixed-size list List<String> fixedList = Arrays.asList("A", "B", "C"); try { Collections.reverse(fixedList); // This works because reverse swaps elements, but adding/removing fails System.out.println("Reversed fixedList: " + fixedList); } catch (UnsupportedOperationException e) { System.out.println("Cannot modify fixed-size list"); } // Use ArrayList for mutable list List<String> mutableList = new ArrayList<>(fixedList); Collections.reverse(mutableList); System.out.println("Reversed mutableList: " + mutableList); } }
Output
Reversed fixedList: [C, B, A]
Reversed mutableList: [C, B, A]
Quick Reference
Collections.reverse modifies the list in place and does not return a value.
- Input:
List<T> - Output:
void(list reversed in place) - Throws:
NullPointerExceptionif list is null - Works only on mutable lists
| Method | Description |
|---|---|
| Collections.reverse(list) | Reverses the order of elements in the given list in place. |
| Throws NullPointerException | If the list passed is null. |
| Works on mutable lists | The list must support element modification. |
Key Takeaways
Collections.reverse reverses the elements of a mutable list in place without creating a new list.
Always pass a mutable and non-null list to avoid exceptions.
The method does not return a new list; it changes the original list directly.
Arrays.asList returns a fixed-size list; wrap it with ArrayList to safely reverse.
NullPointerException occurs if you pass null instead of a list.