Collections public static void reverse(List list) Method Example Program


Reverses the order of the elements in the specified list.

Program

package com.candidjava;


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

/**
 * @author : karthikeyan.T
 * @description : the following code shows how to use
 *              Collections.reverse(List<?> list)
 *              method.
 * */
public class CollectionsReverse {
	public static void main(String[] args) {
		List<String> al = new ArrayList<String>();
		al.add("mohan");
		al.add("karthik");
		al.add("hari");
		al.add("kamal");
		al.add("anand");
		al.add("vinodh");
		System.out.println(al);
		Collections.reverse(al);
		System.out.println(al);
	}
}

Output

[mohan, karthik, hari, kamal, anand, vinodh]
[vinodh, anand, kamal, hari, karthik, mohan]

Explanation

public static void reverse(List<?> list)
Reverses the order of the elements in the specified list.
This method runs in linear time.

Parameters:
list - the list whose elements are to be reversed.
Throws:
UnsupportedOperationException - if the specified list or its list-iterator does not support the set operation.


Related Post

Comments


©candidjava.com