java 中 返回一个空Map

原文链接:Map用法总结


Constructs an empty HashMap with the default initial capacity (16)mutable

// Constructs an empty HashMap with the default initial capacity (16) and the default load fact
// Since:1.2
Map<String, String> map = new HashMap<>();
map.put("key1", "value1");
System.out.println(map); // {key1=value1}

com.google.common.collect.Mapsmutable

// com.google.common.collect
// Creates a mutable, empty HashMap instance.
Map<String, String> map = Maps.newHashMap();
map.put("key1", "value1");
System.out.println(map); // {key1=value1}

java.util.Collectionsimmutable

// Returns an empty map (immutable). This map is serializable.
// Since: 1.5
Map<String, String> map = Collections.emptyMap();
map.put("key1", "value1");
Exception in thread "main" java.lang.UnsupportedOperationException
	at java.base/java.util.AbstractMap.put(AbstractMap.java:209)

java.util.Collectionsimmutable

// The empty map (immutable). This map is serializable.
// Since:1.3
Map map = Collections.EMPTY_MAP;
map.put("key1", "value1");
Exception in thread "main" java.lang.UnsupportedOperationException
	at java.base/java.util.AbstractMap.put(AbstractMap.java:209)

org.apache.commons.collections.MapUtilsimmutable

// An empty unmodifiable map.
// This was not provided in JDK1.2.
Map map = MapUtils.EMPTY_MAP;
map.put("key1", "value1");
Exception in thread "main" java.lang.UnsupportedOperationException
	at org.apache.commons.collections.map.UnmodifiableMap.put(UnmodifiableMap.java:108)

猜你喜欢

转载自blog.csdn.net/weixin_37646636/article/details/132757978