通过Map.of()创建的对象,不可以修改。
/**
* Returns an unmodifiable map containing a single mapping.
* See <a href="#unmodifiable">Unmodifiable Maps</a> for details.
*
* @param <K> the {@code Map}'s key type
* @param <V> the {@code Map}'s value type
* @param k1 the mapping's key
* @param v1 the mapping's value
* @return a {@code Map} containing the specified mapping
* @throws NullPointerException if the key or the value is {@code null}
*
* @since 9
*/
static <K, V> Map<K, V> of(K k1, V v1) {
return new ImmutableCollections.Map1<>(k1, v1);
}
在实际的应用中:
_temp.setYearSalesMap(Map.of(_item.getSaleMonth(),_item.getNetAmt()));
java.lang.UnsupportedOperationException
OR
Map<String,BigDecimal> _tempMap = new HashMap();
_tempMap.put(_item.getSaleMonth(), _item.getNetAmt());
_temp.setYearSalesMap(_tempMap);
可以修改
当取出之前的Map再添加时
_tempMap.put(_item.getSaleMonth(), _item.getNetAmt());