import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class CreateUnmodifiableMap {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(map());
}
/**
* @param entries the <i>final</i> set of entries to add to the newly created <i>unmodifiable</i> map
* @return an <i>unmodifiable</i> map with all given entries
*/
public static <K, V> Map<K, V> map(final Entry<K, V>... entries) {
final HashMap<K, V> map = new HashMap<K, V>(entries.length);
for (final Entry<K, V> entry : entries) {
map.put(entry.getKey(), entry.getValue());
}
return Collections.unmodifiableMap(map);
}
public static <K, V> Map<K, V> map() {
return new HashMap<K, V>();
}
/**
* @return an <b>UNMODIFIABLE</b> Map<K, V>
*/
public static <K, V> Map<K, V> unmodifiableMap(
final Map<? extends K, ? extends V> m) {
return (m == null) ? Collections.<K, V> emptyMap() : Collections
.unmodifiableMap(m);
}
}
Console:
{}