Collections.singleton() method in Java with example
Last Updated :
02 Jul, 2021
java.util.Collections.singleton() method is a java.util.Collections class method. It creates a immutable set over a single specified element. An application of this method is to remove an element from Collections like List and Set.
Syntax:
public static Set singleton(T obj)
and
public static List singletonList(T obj)
Parameters: obj : the sole object to be stored in
the returned list or set.
Return: an immutable list/set containing only the
specified object.
Example:
myList : {"Geeks", "code", "Practice", " Error", "Java",
"Class", "Error", "Practice", "Java" }
To remove all "Error" elements from our list at once, we use
singleton() method
myList.removeAll(Collections.singleton("Error"));
After using singleton() and removeAll, we get following.
{"Geeks", "code", "Practice", "Java", "Class", "Practice", "Java" }
Java
// Java program to demonstrate
// working of singleton()
import java.util.*;
class GFG {
public static void main(String args[])
{
String[] geekslist = { "1", "2", "4", "2", "1", "2",
"3", "1", "3", "4", "3", "3" };
// Creating a list and removing
// elements without use of singleton()
List geekslist1 = new ArrayList(Arrays.asList(geekslist));
System.out.println("Original geeklist1: " + geekslist1);
geekslist1.remove("1");
System.out.println("geekslist1 after removal of 1 without"
+ " singleton " + geekslist1);
geekslist1.remove("1");
System.out.println("geekslist1 after removal of 1 without"
+ " singleton " + geekslist1);
geekslist1.remove("2");
System.out.println("geekslist1 after removal of 2 without"
+ " singleton " + geekslist1);
/* Creating another list and removing
its elements using singleton() method */
List geekslist2 = new ArrayList(Arrays.asList(geekslist));
System.out.println("\nOriginal geeklist2: " + geekslist2);
// Selectively delete "1" from
// all it's occurrences
geekslist2.removeAll(Collections.singleton("1"));
System.out.println("geekslist2 after removal of 1 with "
+ "singleton:" + geekslist2);
// Selectively delete "4" from
// all it's occurrences
geekslist2.removeAll(Collections.singleton("4"));
System.out.println("geekslist2 after removal of 4 with "
+ "singleton:" + geekslist2);
// Selectively delete "3" from
// all it's occurrences
geekslist2.removeAll(Collections.singleton("3"));
System.out.println("geekslist2 after removal of 3 with"
+ " singleton: " + geekslist2);
}
}
Output:
Original geeklist1 [1, 2, 4, 2, 1, 2, 3, 1, 3, 4, 3, 3]
geekslist1 after removal of 1 without singleton [2, 4, 2, 1, 2, 3, 1, 3, 4, 3, 3]
geekslist1 after removal of 1 without singleton [2, 4, 2, 2, 3, 1, 3, 4, 3, 3]
geekslist1 after removal of 2 without singleton [4, 2, 2, 3, 1, 3, 4, 3, 3]
Original geeklist2 [1, 2, 4, 2, 1, 2, 3, 1, 3, 4, 3, 3]
geekslist2 after removal of 1 with singleton [2, 4, 2, 2, 3, 3, 4, 3, 3]
geekslist2 after removal of 4 with singleton [2, 2, 2, 3, 3, 3, 3]
geekslist2 after removal of 3 with singleton [2, 2, 2]
Similar Reads
Collections singletonMap() method in Java with Examples
The singletonMap() method of java.util.Collections class is used to return an immutable map, mapping only the specified key to the specified value. The returned map is serializable. Syntax: public static Map singletonMap(K key, V value) Parameters: This method takes the following parameters as a arg
2 min read
Collections singletonList() method in Java with Examples
The singletonList() method of java.util.Collections class is used to return an immutable list containing only the specified object. The returned list is serializable. This list will always contain only one element thus the name singleton list. When we try to add/remove an element on the returned sin
2 min read
Collections min() method in Java with Examples
min(Collection<? extends T> coll) The min() method of java.util.Collections class is used to return the minimum element of the given collection, according to the natural ordering of its elements. All elements in the collection must implement the Comparable interface. Furthermore, all elements
4 min read
Collections synchronizedCollection() method in Java with Examples
The synchronizedCollection() method of java.util.Collections class is used to return a synchronized (thread-safe) collection backed by the specified collection. In order to guarantee serial access, it is critical that all access to the backing collection is accomplished through the returned collecti
2 min read
Collections synchronizedSet() method in Java with Examples
The synchronizedSet() method of java.util.Collections class is used to return a synchronized (thread-safe) set backed by the specified set. In order to guarantee serial access, it is critical that all access to the backing set is accomplished through the returned set. Syntax: public static <T>
2 min read
Collectors toList() method in Java with Examples
The toList() method of Collectors Class is a static (class) method. It returns a Collector Interface that gathers the input data onto a new list. This method never guarantees type, mutability, serializability, or thread-safety of the returned list but for more control toCollection(Supplier) method c
2 min read
Collections synchronizedMap() method in Java with Examples
The synchronizedMap() method of java.util.Collections class is used to return a synchronized (thread-safe) map backed by the specified map. In order to guarantee serial access, it is critical that all access to the backing map is accomplished through the returned map. Syntax: public static <K, V
2 min read
Collection add() Method in Java with Examples
The add(E element) of java.util.Collection interface is used to add the element 'element' to this collection. This method returns a boolean value depicting the successfulness of the operation. If the element was added, it returns true, else it returns false. Syntax: Collection.add(E element) Paramet
4 min read
Collections synchronizedList() method in Java with Examples
The synchronizedList() method of java.util.Collections class is used to return a synchronized (thread-safe) list backed by the specified list. In order to guarantee serial access, it is critical that all access to the backing list is accomplished through the returned list. Syntax: public static <
2 min read
Collectors toSet() in Java with Examples
Collectors toSet() returns a Collector that accumulates the input elements into a new Set. There are no guarantees on the type, mutability, serializability, or thread-safety of the Set returned. This is an unordered Collector i.e, the collection operation does not commit to preserving the encounter
2 min read