YouTip LogoYouTip

Collection Reverse

# Java Example - Collection Reversal [![Image 3: Java Example](#) Java Example](#) The following example demonstrates how to reverse the elements in a collection using the `listIterator()` method of the `Collection` and `ListIterator` classes, and the `Collections.reverse()` method: ## Main.java File ```java import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.ListIterator; class Main { public static void main(String[] args) { String[] coins = {"A", "B", "C", "D", "E"}; List l = new ArrayList(); for (int i = 0; i < coins.length; i++) l.add(coins); ListIterator liter = l.listIterator(); System.out.println("Before reversal"); while (liter.hasNext()) System.out.println(liter.next()); Collections.reverse(l); liter = l.listIterator(); System.out.println("After reversal"); while (liter.hasNext()) System.out.println(liter.next()); } } The output of the above code is: Before reversal A B C D E After reversal E D C B A [![Image 4: Java Example](#) Java Example](#)
← Collection ShuffleCollection Print β†’