dcsimg
 

Submitted by: M S Sridhar(sridhar.ms@gmail.com)

Monday Oct 15th 2018 by M S Sridhar

Language: Java, Expertise: Intermediate - See how to reverse a list with a set of values in Java with the reverse() method.

A list with a set of values can be reversed using the reverse() method.

import java.util.*;
 
public class ReverseACollection
{
    public static void main(String[] args)
    {
        List dataList = new ArrayList();
        dataList.add("First");
        dataList.add("Second");
        dataList.add("Third");
 
        System.out.println("Initial List : " + dataList);
        Collections.reverse(dataList);
        System.out.println("Reversed List: " + dataList);
    }
}

/*

Expected output:

[root@mypc]# java ReverseACollection
Initial List : [First, Second, Third]
Reversed List: [Third, Second, First]

*/ 
Home
Mobile Site | Full Site