Collections.reverse(List) in Java

Collections.reverse(List): This method is available in java.util.Collections class of Java.

Syntax:

void java.util.Collections.reverse(List<?> list)

This method takes one argument. This method reverses the order of the elements in the specified list.

Note: This method runs in linear time.

Parameters: One parameter is required for this method.

list: the list whose elements are to be reversed.

Throws:

1. UnsupportedOperationException - if the specified list or its list-iterator does not support the set operation.

Approach

Java

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

public class Collectionsreverse {
    public static void main(String[] args) {

        List<Integer> arrlist = new ArrayList<Integer>();

        arrlist.add(12);
        arrlist.add(67);

        arrlist.add(5);
        arrlist.add(8);

        Collections.reverse(arrlist);

        System.out.println("Reverse list is: " + arrlist);

    }
}

Output:

Reverse list is: [8, 5, 67, 12]


No comments:

Post a Comment