Collections.unmodifiableMap
来源:互联网 发布:国外免费网络视频聊天 编辑:程序博客网 时间:2024/06/07 00:07
对于不可变的Collections.unmodifiableMap 是否真的不可以变?
博客:http://blog.csdn.net/l2tp1012/article/details/39338209
Java Code
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.example.demo;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class SeeminglyUnmodifiable
{
private Map<String, Point> startingLocations = new HashMap<String, Point>(3);
public SeeminglyUnmodifiable()
{
startingLocations.put("LeftRook", new Point(1, 1));
startingLocations.put("LeftKnight", new Point(1, 2));
startingLocations.put("LeftCamel", new Point(1, 3));
//..more locations..
}
public Map<String, Point> getStartingLocations()
{
return Collections.unmodifiableMap(startingLocations);
}
public static void main(String [] args)
{
SeeminglyUnmodifiable pieceLocations = new SeeminglyUnmodifiable();
Map<String, Point> locations = pieceLocations.getStartingLocations();
Point camelLoc = locations.get("LeftCamel");
System.out.println("The LeftCamel's start is at [ " + camelLoc.getX() + ", " + camelLoc.getY() + " ]");
//Try 1. update elicits Exception
try
{
locations.put("LeftCamel", new Point(0, 0));
}
catch (java.lang.UnsupportedOperationException e)
{
System.out.println("Try 1 - Could not update the map!");
}
//Try 2. Now let's try changing the contents of the object from the unmodifiable map!
camelLoc.setLocation(0, 0);
//Now see whether we were able to update the actual map
Point newCamelLoc = pieceLocations.getStartingLocations().get("LeftCamel");
System.out.println("Try 2 - Map updated! The LeftCamel's start is now at [ " + newCamelLoc.getX() + ", " + newCamelLoc.getY() + " ]");
}
}
class Point
{
public float x;
public float y;
public Point(float x, float y)
{
setLocation(x, y);
}
public void setLocation(float x, float y)
{
this.x = x;
this.y = y;
}
public float getX()
{
return x;
}
public float getY()
{
return y;
}
}
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class SeeminglyUnmodifiable
{
private Map<String, Point> startingLocations = new HashMap<String, Point>(3);
public SeeminglyUnmodifiable()
{
startingLocations.put("LeftRook", new Point(1, 1));
startingLocations.put("LeftKnight", new Point(1, 2));
startingLocations.put("LeftCamel", new Point(1, 3));
//..more locations..
}
public Map<String, Point> getStartingLocations()
{
return Collections.unmodifiableMap(startingLocations);
}
public static void main(String [] args)
{
SeeminglyUnmodifiable pieceLocations = new SeeminglyUnmodifiable();
Map<String, Point> locations = pieceLocations.getStartingLocations();
Point camelLoc = locations.get("LeftCamel");
System.out.println("The LeftCamel's start is at [ " + camelLoc.getX() + ", " + camelLoc.getY() + " ]");
//Try 1. update elicits Exception
try
{
locations.put("LeftCamel", new Point(0, 0));
}
catch (java.lang.UnsupportedOperationException e)
{
System.out.println("Try 1 - Could not update the map!");
}
//Try 2. Now let's try changing the contents of the object from the unmodifiable map!
camelLoc.setLocation(0, 0);
//Now see whether we were able to update the actual map
Point newCamelLoc = pieceLocations.getStartingLocations().get("LeftCamel");
System.out.println("Try 2 - Map updated! The LeftCamel's start is now at [ " + newCamelLoc.getX() + ", " + newCamelLoc.getY() + " ]");
}
}
class Point
{
public float x;
public float y;
public Point(float x, float y)
{
setLocation(x, y);
}
public void setLocation(float x, float y)
{
this.x = x;
this.y = y;
}
public float getX()
{
return x;
}
public float getY()
{
return y;
}
}
从这个代码可以看出:Collections.unmodifiableMap不是真正的不可以改变,其原理可以从Collections.unmodifiableMap的源代码看出来:
源代码如下:
Java Code
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public V put(K key, V value)
{
throw new UnsupportedOperationException();
}
public V remove(Object key)
{
throw new UnsupportedOperationException();
}
public void putAll(Map<? extends K, ? extends V> m)
{
throw new UnsupportedOperationException();
}
public void clear()
{
throw new UnsupportedOperationException();
}
{
throw new UnsupportedOperationException();
}
public V remove(Object key)
{
throw new UnsupportedOperationException();
}
public void putAll(Map<? extends K, ? extends V> m)
{
throw new UnsupportedOperationException();
}
public void clear()
{
throw new UnsupportedOperationException();
}
结论:Collections.unmodifiableMap是可以对map的值进行修改的,不可以采用put方式进行, 正确的姿势是对Value 采用Set方法进行修改
但是真实的使用场景Collections.unmodifiableMap是不需要对值进行修改的
阅读全文
0 0
- Collections.unmodifiableMap
- Collections.unmodifiableMap
- Collections.unmodifiableMap 用法解析
- Collections.unmodifiableMap()的理解
- Java-Collections.unmodifiableMap()方法
- Collections.unmodifiableMap()分析
- Collections.unmodifiableMap()的简单解释
- 关于Collections.unmodifiableMap的一些理解
- java.util.Collections.unmodifiableMap()方法实例
- 关于collections.unmodifiablemap 的一点理解
- 升级hbase-client报错 java.lang.NoClassDefFoundError: org/apache/commons/collections/map/UnmodifiableMap
- Collections
- Collections
- Collections
- collections
- Collections
- COLLECTIONS
- Collections
- 软件测试之BUG分析定位概述(转载)
- Qt下载地址
- Azkaban入门篇
- 高并发重复插入数据的场景之一
- 推荐一款自助式BI工具
- Collections.unmodifiableMap
- Codeforces Round #371 (Div. 2) A. Meeting of Old Friends【思维,求区间交集】
- Handling truncated messages
- Sphinx 安装
- HDU 【1241】Oil Deposits(DPS)
- HDFS原理综述篇
- JDBC连接数据库oracle的代码
- Java中Socket的用法--Spring MVC
- 宏函数和函数的区别及其优缺点