Collections singletonMap() method in Java with Examples Last Updated : 11 May, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 argument: key - the sole key to be stored in the returned map.value - the value to which the returned map maps key. Return Value: This method returns an immutable map containing only the specified key-value mapping. Below are the examples to illustrate the singletonMap() method Example 1: Java // Java program to demonstrate // singletonMap() method // for <String, String> Value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // create singleton map // using singletonMap() method Map<String, String> map = Collections .singletonMap("key", "Value"); // printing the singletonMap System.out.println("Singleton map is: " + map); } catch (IllegalArgumentException e) { System.out.println("Exception thrown : " + e); } } } Output: Singleton map is: {key=Value} Example 2: Java // Java program to demonstrate // singletonMap() method // for <Integer, Boolean> Value import java.util.*; public class GFG1 { public static void main(String[] argv) throws Exception { try { // create singleton map // using singletonMap() method Map<Integer, Boolean> map = Collections .singletonMap(100, true); // printing the singletonMap System.out.println("Singleton map is: " + map); } catch (IllegalArgumentException e) { System.out.println("Exception thrown : " + e); } } } Output: Singleton map is: {100=true} Comment More infoAdvertise with us Next Article Collections unmodifiableSortedMap() method in Java with Examples R rohitprasad3 Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions +1 More Practice Tags : JavaJava-CollectionsMisc Similar Reads Collections newSetFromMap() method in Java with Examples The newSetFromMap() method of java.util.Collections class is used to return a set backed by the specified map. The resulting set displays the same ordering, concurrency, and performance characteristics as the backing map. In essence, this factory method provides a Set implementation corresponding to 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 Collections unmodifiableMap() method in Java with Examples The unmodifiableMap() method of java.util.Collections class is used to return an unmodifiable view of the specified map. This method allows modules to provide users with "read-only" access to internal maps. Query operations on the returned map "read through" to the specified map, and attempts to mod 2 min read Collections unmodifiableSortedMap() method in Java with Examples The unmodifiableSortedMap() method of java.util.Collections class is used to return an unmodifiable view of the specified sorted map. This method allows modules to provide users with "read-only" access to internal sorted maps. Query operations on the returned sorted map "read through" to the specifi 2 min read Collections synchronizedSortedMap() method in Java with Examples The synchronizedSortedMap() method of java.util.Collections class is used to return a synchronized (thread-safe) sorted map backed by the specified sorted map. In order to guarantee serial access, it is critical that all access to the backing sorted map is accomplished through the returned sorted ma 2 min read Java Collections unmodifiableNavigableMapâ() Method with Examples The unmodifiableMap() in java collections is used to get the unmodifiable view for the given map. Syntax: public static <Key,Value> Map<Key,Value> unmodifiableMap(Map<? extends Key, ? extends Key> map) Parameters: Key is the key-value typeValue is the value typemap is the input map 2 min read Like