Java Collections.singleton(T o)
Syntax
Collections.singleton(T o) has the following syntax.
public static <T> Set <T> singleton(T o)
Example
In the following code shows how to use Collections.singleton(T o) method.
//from w w w .j a va 2 s. com
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String args[]) {
String init[] = { "One", "Two", "Three", "One", "Two", "Three" };
// create two lists
List<String> list1 = new ArrayList<String>(Arrays.asList(init));
List<String> list2 = new ArrayList<String>(Arrays.asList(init));
// remove from list1
list1.remove("One");
System.out.println("List1 value: "+list1);
// remove from list2 using singleton
list2.removeAll(Collections.singleton("One"));
System.out.println("The SingletonList is :"+list2);
}
}
The code above generates the following result.
Home »
Java Tutorial »
java.util »
Java Tutorial »
java.util »