- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to reverse an ArrayList in Java?
The Collections class provides a method named reverse() this method accepts a list and reverses the order of elements in it.
Example:
import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashSet; import java.util.Set; public class Sample { public static void main(String[] args){ ArrayList<String> list = new ArrayList<String>(); list.add("JavaFx"); list.add("Java"); list.add("WebGL"); list.add("OpenCV"); Set<String> set = new LinkedHashSet<String>(list); System.out.println(set); Collections.reverse(list); System.out.println(list); } }
Output:
[JavaFx, Java, WebGL, OpenCV] [OpenCV, WebGL, Java, JavaFx]
- Related Articles
- Use ListIterator to traverse an ArrayList in the reverse direction in Java
- How to synchronize an ArrayList in Java?
- How to create an ArrayList from an Array in Java?
- How to replace an element of an ArrayList in Java?
- How to remove duplicates from an ArrayList in Java?
- How to remove an element from ArrayList in Java?
- How to make an ArrayList read only in Java?
- Java Program to Empty an ArrayList in Java
- How do you convert an ArrayList to an array in Java?
- How to sort an ArrayList in Java in ascending order?
- How to sort an ArrayList in Java in descending order?
- How to sort an ArrayList in Ascending Order in Java
- How to sort an ArrayList in Descending Order in Java
- Convert an ArrayList to HashSet in Java
- How to remove a SubList from an ArrayList in Java?

Advertisements