参见英文答案 >
How to create a deep unmodifiable collection? 6个
java.util.Collections类允许我使集合实例不可修改.以下方法
protected Map> getCacheData() {
return Collections.unmodifiableMap(tableColumnCache);
}
返回一个umodifiable Map,因此尝试更改map实例会导致UnsupportedOperationException.
@Test(expected = UnsupportedOperationException.class)
public void checkGetCacheData_Unmodifiable() {
Map> cacheData = cache.getCacheData();
cacheData.remove("SomeItem");
}
不幸的是所有List< String>孩子不是不可修改的.所以我想知道是否有办法对子值List< String>强制执行无法模仿的行为.太?
当然,或者我可以迭代映射键值对,使列表不可修改并重新组合映射.