java 并发编程实战书籍学习 第四章 unmodifiableMap

来源:互联网 发布:上海游奇网络老板是谁 编辑:程序博客网 时间:2024/06/15 02:02
public class CollectionSycnchroize {


public static void main(String[] args) {
HashMap<String, Point> locationsMap = new HashMap<String, Point>();//创建一个map
Point point = new Point(1, 1);
locationsMap.put("1", point);//放入一个 对象
Map<String, Point> unmodifiableMap = Collections.unmodifiableMap(locationsMap);//创建一个不可修改map
Point pointGeted = unmodifiableMap.get("1");//获取map中的对象
pointGeted.setX(2);
System.out.println(unmodifiableMap.get("1"));//运行正常
unmodifiableMap.put("1", new Point(2, 2));//报错

}


public static class Point{
private int x;
private int y;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public Point(int x, int y) {
super();
this.x = x;
this.y = y;
}
public Point() {
super();
}
@Override
public String toString() {
return "Point [x=" + x + ", y=" + y + "]";
}

}
}
原创粉丝点击