dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
Oct 15, 2018

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


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 ReverseACollection
Initial List : [First, Second, Third]
Reversed List: [Third, Second, First]

*/ 
M S Sridhar
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date