这个是什么意思 不可改变????
public static Map unmodifiableMap(Map<? extends K,? extends V> m)返回指定映射的不可修改视图。此方法允许模块为用户提供对内部映射的“只读”访问。在返回的映射上执行的查询操作将“读完”指定的映射。试图修改返回的映射(不管是直接修改还是通过其 collection 视图进行修改)将导致抛出 UnsupportedOperationException。
如果指定映射是可序列化的,则返回的映射也将是可序列化的。
参数:
m - 将为其返回一个不可修改视图的映射。
返回:
指定映射的不可修改视图。
// 实现原是是包装了下map 不支持改变大小的操作
// 仅仅返回的Map不能put remove 操作,
// 但可以对里的对象进行操作
对,就是产生一个只读的Map,当你调用此map的put方法时会抛错。
看源码就一清二楚了
[code="java"] private static class UnmodifiableMap
implements Map, Serializable
{
static class UnmodifiableEntrySet extends UnmodifiableSet
{
private static class UnmodifiableEntry
implements Map.Entry
{
public Object getKey()
{
return e.getKey();
}
public Object getValue()
{
return e.getValue();
}
public Object setValue(Object obj)
{
throw new UnsupportedOperationException();
}
public int hashCode()
{
return e.hashCode();
}
public boolean equals(Object obj)
{
if(!(obj instanceof Map.Entry))
{
return false;
} else
{
Map.Entry entry = (Map.Entry)obj;
return Collections.eq(e.getKey(), entry.getKey()) && Collections.eq(e.getValue(), entry.getValue());
}
}
public String toString()
{
return e.toString();
}
private Map.Entry e;
UnmodifiableEntry(Map.Entry entry)
{
e = entry;
}
}
public Iterator iterator()
{
return new Iterator() {
public boolean hasNext()
{
return i.hasNext();
}
public Map.Entry next()
{
return new UnmodifiableEntry((Map.Entry)i.next());
}
public void remove()
{
throw new UnsupportedOperationException();
}
public volatile Object next()
{
return next();
}
private final Iterator i;
final UnmodifiableEntrySet this$0;
{
this$0 = UnmodifiableEntrySet.this;
super();
i = c.iterator();
}
}
;
}
public Object[] toArray()
{
Object aobj[] = c.toArray();
for(int i = 0; i < aobj.length; i++)
aobj[i] = new UnmodifiableEntry((Map.Entry)aobj[i]);
return aobj;
}
public Object[] toArray(Object aobj[])
{
Object aobj1[] = c.toArray(aobj.length != 0 ? Arrays.copyOf(aobj, 0) : aobj);
for(int i = 0; i < aobj1.length; i++)
aobj1[i] = new UnmodifiableEntry((Map.Entry)aobj1[i]);
if(aobj1.length > aobj.length)
return (Object[])aobj1;
System.arraycopy(((Object) (aobj1)), 0, ((Object) (aobj)), 0, aobj1.length);
if(aobj.length > aobj1.length)
aobj[aobj1.length] = null;
return aobj;
}
public boolean contains(Object obj)
{
if(!(obj instanceof Map.Entry))
return false;
else
return c.contains(new UnmodifiableEntry((Map.Entry)obj));
}
public boolean containsAll(Collection collection)
{
for(Iterator iterator1 = collection.iterator(); iterator1.hasNext();)
{
Object obj = iterator1.next();
if(!contains(obj))
return false;
}
return true;
}
public boolean equals(Object obj)
{
if(obj == this)
return true;
if(!(obj instanceof Set))
return false;
Set set = (Set)obj;
if(set.size() != c.size())
return false;
else
return containsAll(set);
}
private static final long serialVersionUID = 7854390611657943733L;
UnmodifiableEntrySet(Set set)
{
super(set);
}
}
public int size()
{
return m.size();
}
public boolean isEmpty()
{
return m.isEmpty();
}
public boolean containsKey(Object obj)
{
return m.containsKey(obj);
}
public boolean containsValue(Object obj)
{
return m.containsValue(obj);
}
public Object get(Object obj)
{
return m.get(obj);
}
public Object put(Object obj, Object obj1)
{
throw new UnsupportedOperationException();
}
public Object remove(Object obj)
{
throw new UnsupportedOperationException();
}
public void putAll(Map map)
{
throw new UnsupportedOperationException();
}
public void clear()
{
throw new UnsupportedOperationException();
}
public Set keySet()
{
if(keySet == null)
keySet = Collections.unmodifiableSet(m.keySet());
return keySet;
}
public Set entrySet()
{
if(entrySet == null)
entrySet = new UnmodifiableEntrySet(m.entrySet());
return entrySet;
}
public Collection values()
{
if(values == null)
values = Collections.unmodifiableCollection(m.values());
return values;
}
public boolean equals(Object obj)
{
return obj == this || m.equals(obj);
}
public int hashCode()
{
return m.hashCode();
}
public String toString()
{
return m.toString();
}
private static final long serialVersionUID = -1034234728574286014L;
private final Map m;
private transient Set keySet;
private transient Set entrySet;
private transient Collection values;
UnmodifiableMap(Map map)
{
keySet = null;
entrySet = null;
values = null;
if(map == null)
{
throw new NullPointerException();
} else
{
m = map;
return;
}
}
}[/code]