Java ArrayList reverse example shows how to reverse ArrayList in Java. The example also shows various ways to do that in detail.
How to reverse ArrayList in Java?
There are a couple of ways using which you can reverse ArrayList in Java.
1) Reverse ArrayList using for loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
package com.javacodeexamples.collectionexamples; import java.util.ArrayList; public class ArrayListReverseExample { public static void main(String[] args){ //create new ArrayList object ArrayList<String> aListMonths = new ArrayList<String>(); aListMonths.add("January"); aListMonths.add("February"); aListMonths.add("March"); System.out.println("Before Reversing: " + aListMonths); //get the last index of ArrayList int lastIndex = aListMonths.size() - 1; String temp = null; for( int i = 0 ; i < lastIndex ; i++){ //remove the last element and store it temp = aListMonths.remove(lastIndex); /* * add the removed element back to the ArrayList * at i position. This will push all subsequent elements to * the right. * * We are removing an element from bottom and adding it * from top one by one. */ aListMonths.add(i , temp); } System.out.println("After Reversing: " + aListMonths); } } |
Output
1 2 |
Before Reversing: [January, February, March] After Reversing: [March, February, January] |
2) Reverse ArrayList using Collections class
The simplest way is to use reverse
method of Collections class as given in the below example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package com.javacodeexamples.collectionexamples; import java.util.ArrayList; import java.util.Collections; public class ArrayListReverseExample { public static void main(String[] args){ //create new ArrayList object ArrayList<String> aListMonths = new ArrayList<String>(); aListMonths.add("January"); aListMonths.add("February"); aListMonths.add("March"); System.out.println("Before Reversing: " + aListMonths); /* * To reverse ArrayList, use reverse method of * Collections class. */ Collections.reverse(aListMonths); System.out.println("After Reversing: " + aListMonths); } } |
Output
1 2 |
Before Reversing: [January, February, March] After Reversing: [March, February, January] |
Please note that reverse
method throws UnsupportedOperationException
if the specified List does not support the Set operation.
How does the reverse method of the Collections class work?
Let’s have a look at the source code of the Collections class. This snippet is directly taken from the source code of OpenJDK version 6.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public static void reverse(List<?> list) { int size = list.size(); if (size < REVERSE_THRESHOLD || list instanceof RandomAccess) { for (int i=0, mid=size>>1, j=size-1; i<mid; i++, j--) swap(list, i, j); } else { ListIterator fwd = list.listIterator(); ListIterator rev = list.listIterator(size); for (int i=0, mid=list.size()>>1; i<mid; i++) { Object tmp = fwd.next(); fwd.set(rev.previous()); rev.set(tmp); } } } |
As you can see, it uses REVERSE_THRESHOLD and List’s size along with the type of List to determine the best performing approach to reverse the List. REVERSE_THRESHOLD is defined as,
1 |
private static final int REVERSE_THRESHOLD = 18; |
So if the argument List’s size is less than 18 or if the list is inherited from RandomAccess, it uses for loop to reverse the list by swapping the elements. If the list size is equal to or more than 18, it uses ListIterator to swap the elements and reverse the list.
Note: If you just want to iterate the list in reverse order and do not want to change the original list by reversing it, have a look at how to iterate the ArrayList in reverse order example.
This example is a part of the ArrayList in Java tutorial.
Please let me know your views in the comments section below.