Java ArrayList reverse example

Java ArrayList reverse example shows how to reverse ArrayList in Java. Example also shows various ways to reverse ArrayList in Java.

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

packagecom.javacodeexamples.collectionexamples;

importjava.util.ArrayList;

publicclassArrayListReverseExample{

publicstaticvoidmain(String[]args){

//create new ArrayList object

ArrayList<String>aListMonths=newArrayList<String>();

aListMonths.add("January");

aListMonths.add("February");

aListMonths.add("March");

System.out.println("Before Reversing: "+aListMonths);

//get the last index of ArrayList

intlastIndex=aListMonths.size()-1;

Stringtemp=null;

for(inti=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 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

packagecom.javacodeexamples.collectionexamples;

importjava.util.ArrayList;

importjava.util.Collections;

publicclassArrayListReverseExample{

publicstaticvoidmain(String[]args){

//create new ArrayList object

ArrayList<String>aListMonths=newArrayList<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 reverse method of Collections class work?

Let’s have a look at source code of 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

publicstaticvoidreverse(List<?>list){

intsize=list.size();

if(size<REVERSE_THRESHOLD||list instanceofRandomAccess){

for(inti=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(inti=0,mid=list.size()>>1;i<mid;i++){

Objecttmp=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

privatestaticfinalintREVERSE_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.

About the author

rahimv

rahimv has over 15 years of experience in designing and developing Java applications. His areas of expertise are J2EE and eCommerce. If you like the website, follow him on Facebook, Twitter or Google Plus.