Collections public static Set singleton(T o) Method Example Program


Returns an immutable set 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 In the following code shows how to use Collections.singleton(T
 *              o) method.
 */
public class CollectionsSingleton {

	public static void main(String args[]) {
		String init[] = { "One", "Two", "Three", "One", "Two", "Three" };
		List<String> list1 = new ArrayList<String>(Arrays.asList(init));
		List<String> list2 = new ArrayList<String>(Arrays.asList(init));
		list1.remove("One");
		System.out.println("List1 value: " + list1);
		list2.removeAll(Collections.singleton("One"));
		System.out.println("The SingletonList is :" + list2);
	}
}

Output

List1 value: [Two, Three, One, Two, Three]
The SingletonList is :[Two, Three, Two, Three]

Explanation

public static <T> Set<T> singleton(T o)
Returns an immutable set containing only the specified object. The returned set is serializable.
Parameters:
o - the sole object to be stored in the returned set.
Returns:
an immutable set containing only the specified object.


Related Post

Comments


©candidjava.com