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.
Syntax:
public static List singletonList(T o)
Parameters: This method takes the object o as a parameter to be stored in the returned list.
Return Value: This method returns an immutable list containing only the specified object.
Below are the examples to illustrate the singletonList() method
Example 1:
// Java program to demonstrate // singletonList() method // for <String> Value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // create singleton list // using method singletonList() method List<String> list = Collections.singletonList("E"); // print the list System.out.println("singletonList : " + list); } catch (IllegalArgumentException e) { System.out.println("Exception thrown : " + e); } } } |
singletonList : [E]
Example 2:
// Java program to demonstrate // singletonList() method // for <Integer> Value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // create singleton list // using method singletonList() method List<Integer> list = Collections.singletonList(20); // print the list System.out.println("singletonList : " + list); } catch (IllegalArgumentException e) { System.out.println("Exception thrown : " + e); } } } |
singletonList : [20]
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Difference between Traditional Collections and Concurrent Collections in java
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.Collections.disjoint() Method in java with Examples
- Java.util.Collections.frequency() in Java with Examples
- Collections checkedMap() method in Java with Examples
- Collections singletonMap() method in Java with Examples
- Collections min() method in Java with Examples
- Collections max() method in Java with Examples
- Collections addAll() method in Java with Examples
- Collections asLifoQueue() method in Java with Examples
- Collections unmodifiableCollection() method in Java with Examples
- Collections unmodifiableSortedMap() method in Java with Examples
- Collections unmodifiableSet() method in Java with Examples
- Collections unmodifiableMap() method in Java with Examples
- Collections unmodifiableList() method in Java with Examples
- Collections checkedCollection() method in Java with Examples
- Collections checkedList() method in Java with Examples
- Collections checkedSet() method in Java with Examples
- Collections checkedSortedMap() method in Java with Examples
- Collections checkedSortedSet() method in Java with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

