java 并发编程实战书籍学习 第四章 unmodifiableMap
2017-11-08 17:30
288 查看
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 + "]";
}
}
}
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 + "]";
}
}
}
相关文章推荐
- [置顶] java 并发编程实战书籍学习 第五章,CountDownLatch,FutureTask,CyclicBarrier,Semaphore学习
- 学习java并发编程实战的一些心得体会(一)
- Java 并发编程实战学习笔记——寻找可强化的并行性
- Java 并发编程实战学习笔记
- Java并发编程实战-学习总结-第一篇(概括)
- Java 并发编程实战学习笔记——CountDownLatch的使用
- java并发编程实战学习(3)--基础构建模块
- 学习java并发实战编程(一)线程安全性
- Java 并发编程实战学习笔记——串行任务转并行任务
- Java 并发编程实战学习笔记——路径查找类型并行任务的终止
- java学习之路--java数组--java开发实战经典第四章
- Java多线程编程--(10)学习Java5.0 并发编程包--线程工具类
- Java多线程编程--(6)学习Java5.0 并发编程包--原子操作的一些类型
- JAVA并发编程学习笔记之synchronized
- Java 并发编程基础学习
- Java 并发编程学习笔记 理解CLH队列锁算法
- java并发编程学习总结(基础篇)
- Java多线程编程--(9)学习Java5.0 并发编程包--线程工具类
- JAVA并发编程学习笔记之synchronized
- Java 编程学习书籍推荐(附下载地址)