Collections public static Map singletonMap(K key, V value) Method Example Program


Returns an immutable map, mapping only the specified key to the specified value.

Program

package com.candidjava;

import java.util.Collections;
import java.util.Map;

/**
 * 
 * @author karthikeyan.T
 * @description the following code shows how to use Collections.singletonMap(K
 *              key, V value) method.
 */
public class CollectionssingletonMap {
	public static void main(String[] a) {
		Map<String, String> map = Collections.singletonMap("key", "Value");
		System.out.println("Singleton map is: " + map);
	}
}

Output

Singleton map is: {key=Value}

Explanation

public static <K,V> Map<K,V> singletonMap(K key, V value)
Returns an immutable map, mapping only the specified key to the specified value. The returned map is serializable.
Parameters:
key - the sole key to be stored in the returned map.
value - the value to which the returned map maps key.
Returns:
an immutable map containing only the specified key-value mapping.
Since:
1.3


Related Post

Comments


©candidjava.com