1. 概述

  1. public static <K,V> Map<K,V> unmodifiableMap(Map<? extends K,? extends V> m)
返回指定映射的不可修改视图。此方法允许模块为用户提供对内部映射的“只读”访问。在返回的映射上执行的查询操作将“读完”指定的映射。
试图修改返回的映射(不管是直接修改还是通过其 collection 视图进行修改)将导致抛出 UnsupportedOperationException

如果指定映射是可序列化的,则返回的映射也将是可序列化的。

参数:
m - 将为其返回一个不可修改视图的映射。
返回:
指定映射的不可修改视图。

2. demo

2.1 code (摘自 http://stackoverflow.com/questions/3999086/when-is-the-unmodifiablemap-really-necessary)

  1. package com.rocky.catest;
  2.  
  3. import java.util.Collections;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6.  
  7. public class SeeminglyUnmodifiable {
  8. private Map<String, Point> startingLocations = new HashMap<String, Point>(3);
  9.  
  10. public SeeminglyUnmodifiable(){
  11. startingLocations.put("LeftRook", new Point(1, 1));
  12. startingLocations.put("LeftKnight", new Point(1, 2));
  13. startingLocations.put("LeftCamel", new Point(1, 3));
  14. //..more locations..
  15. }
  16.  
  17. public Map<String, Point> getStartingLocations(){
  18. return Collections.unmodifiableMap(startingLocations);
  19. }
  20.  
  21. public static void main(String [] args){
  22. SeeminglyUnmodifiable pieceLocations = new SeeminglyUnmodifiable();
  23. Map<String, Point> locations = pieceLocations.getStartingLocations();
  24.  
  25. Point camelLoc = locations.get("LeftCamel");
  26. System.out.println("The LeftCamel's start is at [ " + camelLoc.getX() + ", " + camelLoc.getY() + " ]");
  27.  
  28. //Try 1. update elicits Exception
  29. try{
  30. locations.put("LeftCamel", new Point(0,0));
  31. } catch (java.lang.UnsupportedOperationException e){
  32. System.out.println("Try 1 - Could not update the map!");
  33. }
  34.  
  35. //Try 2. Now let's try changing the contents of the object from the unmodifiable map!
  36. camelLoc.setLocation(0,0);
  37.  
  38. //Now see whether we were able to update the actual map
  39. Point newCamelLoc = pieceLocations.getStartingLocations().get("LeftCamel");
  40. System.out.println("Try 2 - Map updated! The LeftCamel's start is now at [ " + newCamelLoc.getX() + ", " + newCamelLoc.getY() + " ]"); }
  41. }
  42.  
  43. class Point{
  44. public float x;
  45. public float y;
  46. public Point(float x, float y){
  47. setLocation(x, y);
  48. }
  49. public void setLocation(float x, float y){
  50. this.x = x;
  51. this.y = y;
  52. }
  53.  
  54. public float getX(){
  55. return x;
  56. }
  57.  
  58. public float getY(){
  59. return y;
  60. }
  61. }

2.2 控制台输出

  1. The LeftCamel's start is at [ 1.0, 3.0 ]
  2. Try 1 - Could not update the map!
  3. Try 2 - Map updated! The LeftCamel's start is now at [ 0.0, 0.0 ]

3. 解释

unmodifiableMap方法返回的Map, 它本身不允许修改, 就是说其中每一个entry引用不允许修改,但是entry中的value如果是对象,value引用的对象的属性值是可以修改的

 

java.util.Collections.unmodifiableMap 示例的更多相关文章

  1. 005-guava 集合-集合工具类-java.util.Collections中未包含的集合工具[Maps,Lists,Sets],Iterables、Multisets、Multimaps、Tables

    一.概述 工具类与特定集合接口的对应关系归纳如下: 集合接口 属于JDK还是Guava 对应的Guava工具类 Collection JDK Collections2:不要和java.util.Col ...

  2. 集合-强大的集合工具类:java.util.Collections中未包含的集合工具

    任何对JDK集合框架有经验的程序员都熟悉和喜欢java.util.Collections包含的工具方法.Guava沿着这些路线提供了更多的工具方法:适用于所有集合的静态方法.这是Guava最流行和成熟 ...

  3. java.util.Collections.synchronizedSet()方法的使用

    下面的例子显示java.util.Collections.synchronizedSet()方法的使用 package com.; import java.util.*; public class C ...

  4. 类 java.util.Collections 提供了对Set、List、Map进行排序、填充、查找元素的辅助方法。

      类 java.util.Collections 提供了对Set.List.Map进行排序.填充.查找元素的辅助方法. 1. void sort(List) //对List容器内的元素排序,排序的规 ...

  5. [Google Guava] 2.3-强大的集合工具类:java.util.Collections中未包含的集合工具

    原文链接 译文链接 译者:沈义扬,校对:丁一 尚未完成: Queues, Tables工具类 任何对JDK集合框架有经验的程序员都熟悉和喜欢java.util.Collections包含的工具方法.G ...

  6. [Google Guava] 强大的集合工具类:java.util.Collections中未包含的集合工具

    转载的,有问题请联系我 原文链接 译文链接 译者:沈义扬,校对:丁一 尚未完成: Queues, Tables工具类 任何对JDK集合框架有经验的程序员都熟悉和喜欢java.util.Collecti ...

  7. java.util.Collections.copy():列表List浅拷贝

    今天同事问我怎样将一个列表(list1)拷贝到另一个列表(list2),然后修改新的列表(list2)不会影响到旧的列表(list1),想了一想,这是深拷贝啊. 可是,除了循环new还有别的办法吗,想 ...

  8. 要点Java20 java.util.Collections

    java.util.Collections 集合帮助类 演示样例程序(JUnit演示) 排序 @Test public void testSort() { List<Integer> de ...

  9. import java.util.Collections类

    Collections类提供了一些操作集合的方法  下面介绍几个方法 1.将集合变为线程安全的 三个方法分别对应了ArrayList,HashMap,HashSet: Collections.sync ...

随机推荐

  1. Python导模块问题

    我们在import一个模块的时候,有没有想过如果我重复的import会怎么样?导入时都做了些什么?让我们来看看 1.首先我们新建一个demo,py,里面只有一句输出语句 2.多次导入demo,运行之后 ...

  2. java字段中初始化的规律与如何用静态成员函数调用非静态成员

    java字段中初始化的规律: 执行以下代码,出现的结果是什么? class InitializeBlockClass{ { field=200; } public int field=100; pub ...

  3. 关于java编译

    1.不包括jar编译 javac -cp %CLASSPATH% -d ./classes/ ./src/org/csource/common/*.java --先编译基础模块,并且指定class保存 ...

  4. 如何高度自定义CollectionView的header和foot

    最近在研究CollectionView,突然发现觉得他的HeaderSection和FootSection也可以高度自定义. 国外有详细的教程:http://www.appcoda.com/ios-c ...

  5. 数据库SQL(1)

    EG1:db.LpOutputGroups.GroupBy(q => q.CalcGroupDesc).ToList().OrderByDescending(m => m.First(). ...

  6. Oracle给Select结果集加锁,Skip Locked(跳过加锁行获得可以加锁的结果集)

    1.通过select for update或select for update wait或select for update nowait给数据集加锁 具体实现参考select for update和 ...

  7. hadoop集群搭建过程中遇到的问题

    在安装配置Hadoop集群的过程中遇到了很多问题,有些是配置导致的,有些是linux系统本身的问题造成的,现在总结如下. 1. hdfs namenode -format出现错误:hdfs namen ...

  8. JavaScript Ajax 实现学习

    创建异步对象: function createXmlHttp(){ var xhobj=false; try{ xhobj=new ActiveXObject("Msxml2.XMLHTTP ...

  9. git学习笔记5

    查看保存的进度 git stash list 恢复进度 git stash pop 测试运行哪些文件会被删除 git clean -nd 强制删除 git clean -fd 保存当前的工作进度,会保 ...

  10. 3、在Shell程序中使用的参数

    学习目标位置参数内部参数 如同ls命令可以接受目录等作为它的参数一样,在Shell编程时同样可以使用参数.Shell程序中的参数分为位置参数和内部参数等. 12-3-1 位置参数由系统提供的参数称为位 ...