Thursday, December 12, 2013

Reverse list using Collections.reverse()

To re-order list elements in reversed, we can call the static Collections.reverse(list) method.

package java_reverselist;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @web java-buddy.blogspot.com
 */
public class Java_ReverseList {

    public static void main(String[] args) {
        List<String> list = new ArrayList<String>();
        
        list.add("A");
        list.add("B");
        list.add("C");
        list.add("D");
        list.add("E");
        
        System.out.println("original list");
        for(String s: list){
            System.out.print(s);
        }
        
        Collections.reverse(list);
        System.out.println("\nreversed list");
        for(String s: list){
            System.out.print(s);
        }
        System.out.println("\n");
    }
    
}

Reverse list
Reverse list
more: Reverse, Shuffle and Sort List

No comments:

Post a Comment