devxlogo

Understanding Collections.reverse() in Java

Understanding Collections.reverse() in Java

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 ReverseACollectionInitial List : [First, Second, Third]Reversed List: [Third, Second, First]*/ 

devx-admin

Share the Post: