Collections.singletonList(List) in Java

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

Syntax:

<List<E>> List<List<E>> java.util.Collections.singletonList(List<E> o)

This method takes one argument. This method returns an immutable list containing only the specified object.

Note: The returned list is serializable.

Parameters: One parameter is required for this method.

o: the sole object to be stored in the returned list.

Returns: an immutable list containing only the specified object.

Exceptions: NA

Approach

Java

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

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

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

        arrlist.add(12);
        arrlist.add(56);
        arrlist.add(899);
        arrlist.add(65);
        arrlist.add(5);

        System.out.println(Collections.singletonList(arrlist));

    }
}

Output:

[[12, 56, 899, 65, 5]]


No comments:

Post a Comment