If quite a few components in Listing are lower than REVERSE_THRESHOLD, which is the same as 18 then it makes use of for loop for swapping components in any other case it makes use of record iterator. If you wish to be taught extra about how the reverse() methodology of Collections works, you’ll be able to see it is code from JDK itself or within the subsequent part.
By the way in which, it is a typesafe generic methodology and you need to use it to reverse Integer, String, Float or any type of Listing in Java. You can even see the basic e-book Core Java Quantity 1 – Fundamentals by Cay S. Horstmann to be taught extra about key courses of the Java Growth Package.
Java Program to Reverse an ArrayList in Java
Right here is my code instance of reversing an ArrayList of Integer. You’ll be able to see that we have now added numbers on rising order however after calling reverse() methodology, it prints them on lowering order. Not like common false impression, Comparable or Comparator is just not used whereas reversing ArrayList in Java. Although they’re used if you wish to type Array in Java.
import java.util.ArrayList; import java.util.Collections; /** * Java program to reverse ArrayList through the use of Collections.reverse() * methodology. This methodology will work for any type of ArrayList e.g. * integer record or String record, however this methodology is not going to work * for an ArrayList which does not help set() operation. * * @creator WINDOWS 8 */ public class ArrayListReverseDemo { public static void primary(String args[]) { ArrayList<String> listOfInts = new ArrayList<>(); listOfInts.add("1"); listOfInts.add("2"); listOfInts.add("3"); listOfInts.add("4"); listOfInts.add("5"); System.out.println("Earlier than Reversing : " + listOfInts); Collections.reverse(listOfInts); System.out.println("After Reversing : " + listOfInts); } } Output Earlier than Reversing : [1, 2, 3, 4, 5] After Reversing : [5, 4, 3, 2, 1]
How Collections.reverse() methodology works in Java
Right here is the code snippet from java.util.Collections class which you need to use to reverse an ArrayList or any type of Listing in Java. You’ll be able to see that it makes use of set() methodology of Listing interface for swapping components and that is why you can’t reverse a read-only ArrayList as a result of it does not help set() operation.
Logic of Collections.reverse() methodology
Right here is the logic and clarification of how the Collections.reverse() methodology works and the way it reverse the given assortment.
/** * Reverses the order of the weather within the specified record.<p> * * This methodology runs in linear time. * * @param record the record whose components are to be reversed. * @throws UnsupportedOperationException if the desired record or * its list-iterator doesn't help the <tt>set</tt> operation. */ public static void reverse(Listing<?> record) { int measurement = record.measurement(); if (measurement < REVERSE_THRESHOLD || record instanceof RandomAccess) { for (int i=0, mid=measurement>>1, j=measurement-1; i<mid; i++, j--) swap(record, i, j); } else { ListIterator fwd = record.listIterator(); ListIterator rev = record.listIterator(measurement); for (int i=0, mid=record.measurement()>>1; i<mid; i++) { Object tmp = fwd.subsequent(); fwd.set(rev.earlier()); rev.set(tmp); } } }
That is all about learn how to reverse ArrayList in Java. Although you’ll be able to at all times write your methodology to reverse an ArrayList, it will not be a lot totally different than the way you reverse an Array in Java, it is at all times higher to make use of a perform from the JDK library. Why? as a result of they’re nicely examined for programming bugs and nook instances and they’re much extra optimized than you suppose, due to wider audiences who’ve used and improved them already. Tell us if you realize a quicker option to reverse ArrayList in Java.