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]*/ 
See also  Java Developer Jobs: Your Guide to Landing the Perfect Position in 2024

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist