Map goodsStandMap = new HashMap<>(); goodsStandMap.put("key1", "value1"); goodsStandMap.put("key2", "value2"); Set unitKeySet = goodsStandMap.keySet();
报错:"exceptionTypeName":"java.lang.UnsupportedOperationException","message":null}}} 原因: map.keySet方法返回的是 keySet不是HashSet KeySet extends AbstractSet implements Set
keySet中没有add方法,也好理解,如果map的keyset中添加新的元素,那map中也添加key,那么value呢?
public Set keySet() { Set ks = keySet; if (ks == null) { ks = new KeySet(); keySet = ks; } return ks; } final class KeySet extends AbstractSet {}
public abstract class AbstractSet extends AbstractCollection implements Set {}