We can use Collections.reverse() method to reverse contents of arraylist in java.
Syntax:
Collections.reverse(arrayList);
Example:
package com.w3schools; import java.util.ArrayList; import java.util.Collections; public class Test { public static void main(String args[]){ ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("Jai"); arrayList.add("Mahesh"); arrayList.add("Vivek"); arrayList.add("Naren"); arrayList.add("Hemant"); arrayList.add("Vishal"); System.out.println("Actual ArrayList:"+arrayList); Collections.reverse(arrayList); System.out.println("ArrayList after reverse operation:" + arrayList); } } |
Output
Actual ArrayList:[Jai, Mahesh, Vivek, Naren, Hemant, Vishal] ArrayList after reverse operation:[Vishal, Hemant, Naren, Vivek, Mahesh, Jai] |