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:

[[email protected]]# java ReverseACollectionInitial List : [First, Second, Third]Reversed List: [Third, Second, First]*/ 
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: