You tin laissez passer on notice contrary ArrayList inwards Java past times using the reverse() method of java.util.Collections class. This is i of the many utility methods provided past times the Collections flat e.g. sort() method for sorting ArrayList. The Collections.reverse() method every bit good accepts a List, thence you lot non entirely tin laissez passer on notice contrary ArrayList precisely every bit good whatsoever other implementation of List interface e.g. LinkedList or Vector or fifty-fifty a custom implementation. This method has a fourth dimension complexity of O(n) i.e. it runs on linear fourth dimension because it uses ListIterator of given list. It reverses the guild of an chemical factor inwards specified list. By the means you lot cannot contrary an ArrayList using this method if the specified ArrayList or it's ListIterator doesn't back upwardly set() operation. It switches betwixt 2 algorithms depending upon the size of List or if List implements RandomAccess interface e.g. ArrayList.
If publish of elements inwards List is less than REVERSE_THRESHOLD, which is equal to eighteen as well as thence it uses for loop for swapping elements otherwise it uses listing iterator. If you lot desire to larn to a greater extent than nigh how reverse() method of Collections works, you lot tin laissez passer on notice meet it's code from JDK itself or inwards the side past times side section.
By the way, this is a typesafe generic method as well as you lot tin laissez passer on notice work it to contrary Integer, String, Float or whatsoever sort of List inwards Java. You tin laissez passer on notice every bit good meet the classic mass Core Java Volume 1 - Fundamentals past times Cay S. Horstmann to larn to a greater extent than nigh key classes of Java Development Kit.
You tin laissez passer on notice every bit good meet the classic book Core Java Volume 1 - Fundamentals by Cay S. Horstmann to larn to a greater extent than nigh key classes of Java Development Kit.
Logic of Collections.reverse() method
That's all nigh how to contrary ArrayList inwards Java. Though you lot tin laissez passer on notice e'er write your method to contrary an ArrayList, it won't last much dissimilar that how you lot contrary an Array inwards Java, it's e'er amend to work purpose from JDK library. Why? because they are good tested for programming bugs as well as corner cases as well as they are much to a greater extent than optimized as well as thence you lot think, because of wider audiences who direct maintain used as well as improved them already. Let us know if you lot know a faster means to contrary ArrayList inwards Java.
Further Learning
Java In-Depth: Become a Complete Java Engineer
Java Fundamentals: Collections
Data Structures as well as Algorithms: Deep Dive Using Java
Algorithms as well as Data Structures - Part 1 as well as 2
Data Structures inwards Java nine past times Heinz Kabutz
If publish of elements inwards List is less than REVERSE_THRESHOLD, which is equal to eighteen as well as thence it uses for loop for swapping elements otherwise it uses listing iterator. If you lot desire to larn to a greater extent than nigh how reverse() method of Collections works, you lot tin laissez passer on notice meet it's code from JDK itself or inwards the side past times side section.
By the way, this is a typesafe generic method as well as you lot tin laissez passer on notice work it to contrary Integer, String, Float or whatsoever sort of List inwards Java. You tin laissez passer on notice every bit good meet the classic mass Core Java Volume 1 - Fundamentals past times Cay S. Horstmann to larn to a greater extent than nigh key classes of Java Development Kit.
Java Program to contrary ArrayList inwards Java
Here is my code representative of reversing an ArrayList of Integer. You tin laissez passer on notice meet that nosotros direct maintain added numbers on increasing guild precisely afterward calling reverse() method, it prints them on decreasing order. Unlike pop misconcept, Comparable or Comparator is non used land reversing ArrayList inwards Java. Though they are used if you lot desire to sort Array inwards Java.import java.util.ArrayList; import java.util.Collections; /** * Java programme to contrary ArrayList past times using Collections.reverse() * method. This method volition locomote for whatsoever sort of ArrayList e.g. * integer listing or String list, precisely this method volition non locomote * for an ArrayList which doesn't back upwardly set() operation. * * @author WINDOWS 8 */ public class ArrayListReverseDemo { public static void main(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("Before Reversing : " + listOfInts); Collections.reverse(listOfInts); System.out.println("After Reversing : " + listOfInts); } } Output Before Reversing : [1, 2, 3, 4, 5] After Reversing : [5, 4, 3, 2, 1]
How contrary method of Collections works
Here is the code snippet from java.util.Collections flat which you lot tin laissez passer on notice work to reverse an ArrayList or whatsoever sort of List inwards Java. You tin laissez passer on notice meet that it uses set() method of List interface for swapping elements as well as that's why you lot cannot contrary a read entirely ArrayList because it doesn't back upwardly set() operation.You tin laissez passer on notice every bit good meet the classic book Core Java Volume 1 - Fundamentals by Cay S. Horstmann to larn to a greater extent than nigh key classes of Java Development Kit.
Logic of Collections.reverse() method
/** * Reverses the guild of the elements inwards the specified list.<p> * * This method runs inwards linear time. * * @param listing the listing whose elements are to last reversed. * @throws UnsupportedOperationException if the specified listing or * its list-iterator does non back upwardly the <tt>set</tt> operation. */ public static void reverse(List<?> list) { int size = list.size(); if (size < REVERSE_THRESHOLD || listing instanceof RandomAccess) { for (int i=0, mid=size>>1, j=size-1; i<mid; i++, j--) swap(list, i, j); } else { ListIterator fwd = list.listIterator(); ListIterator rev = list.listIterator(size); for (int i=0, mid=list.size()>>1; i<mid; i++) { Object tmp = fwd.next(); fwd.set(rev.previous()); rev.set(tmp); } } }
That's all nigh how to contrary ArrayList inwards Java. Though you lot tin laissez passer on notice e'er write your method to contrary an ArrayList, it won't last much dissimilar that how you lot contrary an Array inwards Java, it's e'er amend to work purpose from JDK library. Why? because they are good tested for programming bugs as well as corner cases as well as they are much to a greater extent than optimized as well as thence you lot think, because of wider audiences who direct maintain used as well as improved them already. Let us know if you lot know a faster means to contrary ArrayList inwards Java.
Further Learning
Java In-Depth: Become a Complete Java Engineer
Java Fundamentals: Collections
Data Structures as well as Algorithms: Deep Dive Using Java
Algorithms as well as Data Structures - Part 1 as well as 2
Data Structures inwards Java nine past times Heinz Kabutz
No comments:
Post a Comment