The Artima Developer Community
Sponsored Link

Java Buzz Forum
How to reverse ArrayList in Java with Example

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Javin Paul

Posts: 1090
Nickname: javinpaul
Registered: Jan, 2012

Javin Paul is Java Programmer working on Finance domain.
How to reverse ArrayList in Java with Example Posted: Jan 9, 2015 8:14 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Javin Paul.
Original Post: How to reverse ArrayList in Java with Example
Feed Title: Java67
Feed URL: http://www.java67.com/feeds/posts/default?alt=rss
Feed Description: Java and technology tutorials, tips, questions for all programmers.
Latest Java Buzz Posts
Latest Java Buzz Posts by Javin Paul
Latest Posts From Java67

Advertisement
You can reverse ArrayList in Java by using reverse() method of java.util.Collections class. This is one of the many utility method provided by Collections class e.g. sort() method for sorting ArrayList. The Collections.reverse() method also accepts a List, so you not only can reverse ArrayList but also any other implementation of List interface e.g. LinkedList or Vector or even a custom implementation. This method has time complexity of O(n) i.e. it runs on linear time, because it uses ListIterator of given list.  It reverse the order of element in specified list. By the way you cannot reverse an ArrayList using this method if the specified ArrayList or it's ListIterator doesn't support set() operation. It switches between two algorithm depending upon the size of List or if List implements RandomAccess interface e.g. ArrayList. If number of elements in List is less than REVERSE_THRESHOLD, which is equal to 18 then it uses for loop for swapping elements otherwise it uses list iterator. If you want to learn more about how reverse() method of Collections works, you can see it's code from JDK itself or in the next section. By the way, this is a typesafe generic method and you can use it to reverse Integer, String, Float or any kind of List in Java.
Read more »

Read: How to reverse ArrayList in Java with Example

Topic: Caching Strategy Reminder for Maven-Based Docker Builds Previous Topic   Next Topic Topic: JDBC Transaction Management Example

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use