Collections public static List singletonList(T o) Method Example Program


Returns an immutable list containing only the specified object.

Program

package com.candidjava;

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

/**
 * 
 * @author karthikeyan.T
 * @description the following code shows how to use Collections.singletonList(T
 *              o) method
 */
public class CollectionsSingleTonList {
	public static void main(String args[]) {
		String init[] = { "One", "Two", "Three", "One", "Two", "Three"};
		List<String> list = new ArrayList<String>(Arrays.asList(init));
		System.out.println("List value before: " + list);
		list = Collections.singletonList("One");
		System.out.println("List value after: " + list);
	}
}

Output

List value before: [One, Two, Three, One, Two, Three]
List value after: [One]

Explanation

public static <T> List<T> singletonList(T o)
Returns an immutable list containing only the specified object. The returned list is serializable.
Parameters:
o - the sole object to be stored in the returned list.
Returns:
an immutable list containing only the specified object.
Since:
1.3


Related Post

Comments


©candidjava.com