- 浏览: 786053 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (360)
- Java (101)
- JPA/Hibernate (10)
- Spring (14)
- Flex/BlazeDS (37)
- Database (30)
- Lucene/Solr/Nutch (0)
- Maven/Ant (25)
- CXF/WebService (3)
- RPC/RMI/SOAP/WSDL (1)
- REST (6)
- TDD/BDD/JUnit (1)
- Servlet/JSP (2)
- AI/MachineLearning (3)
- Resource (1)
- 字符编码 (2)
- OOA/OOPS/UML (5)
- DesignPattern (8)
- 算法与数据结构 (11)
- Web&App Server (13)
- 并发&异步&无阻塞 (7)
- Entertainment (4)
- JavaScript/ExtJS (45)
- CodeStyle&Quality (1)
- svn/git/perforce (8)
- JSON (2)
- JavaScriptTesting (4)
- Others (6)
- RegularExpression (2)
- Linux/Windows (12)
- Protocal (2)
- Celebrities (1)
- Interview (1)
- 计算机语言 (1)
- English (2)
- Eclipse (5)
- TimeZone/时区 (1)
- Finance (1)
- 信息安全 (1)
- JMS/MQ (2)
- XSD/XML/DTD (3)
- Android (4)
- 投资 (3)
- Distribution (3)
- Excel (1)
最新评论
-
qdujunjie:
如果把m换成具体的数字,比如4或者5,会让读者更明白
m阶B树中“阶”的含义 -
java-admin:
不错,加油,多写点文章
关于Extjs的mixins和plugin -
xiehuaidong880827:
你好,我用sencha cmd打包完本地工程后,把app.js ...
ExtJS使用Sencha Cmd合并javascript文件为一个文件 -
KIWIFLY:
lwpan 写道inverse = "true&qu ...
Hibernate中什么时候使用inverse=true -
luedipiaofeng:
good
消除IE stop running this script弹出框
如果某个对象在被创建后其状态就不能被修改,那么这个对象就称为不可变对象。线程安全性是不可变对象的固有属性之一,它们的不变性条件是由构造函数创建的,只要它们的状态不改变,那么这些不变性条件就能得以维持。不可变对象一定是线程安全的。
当满足以下条件时,对象才是不可变的:
1) 对象创建以后其状态就不能修改。
2) 对象的所有域都是final类型(当然像不可变String类型的域并不需要声明为final)。
3) 对象是正确创建的(在对象的创建期间,this引用没有逸出)。
更多详细参考<Java Concurrency in Practice> 关于“不变性”的章节
Java实现Immutable Class要点
Java中很多class都是immutable,像String,Integer等,它们通常用来作为Map的key.
那么在实现自定义的Immutable的Class的时候,应该注意哪些要点呢?
a)Class 应该定义成final,避免被继承。
b)所有的成员变量应该被定义成final。
c)不要提供可以改变类状态(成员变量)的方法。【get 方法不要把类里的成员变量让外部客户端引用,当需要访问成员变量时,返回成员变量的copy】
d)构造函数不要引用外部可变对象。如果需要引用可以在外部改变值的变量,应该在构造函数里进行defensive copy。
Wrong way to write a constructor:
public final class MyImmutable { private final int[] myArray; public MyImmutable(int[] anArray) { this.myArray = anArray; // wrong } public String toString() { StringBuffer sb = new StringBuffer("Numbers are: "); for (int i = 0; i < myArray.length; i++) { sb.append(myArray[i] + " "); } return sb.toString(); } }
//the caller could change the array after calling the constructor.
int[] array = {1,2}; MyImmutable myImmutableRef = new MyImmutable(array) ; System.out.println("Before constructing " + myImmutableRef); array[1] = 5; // change (i.e. mutate) the element System.out.println("After constructing " + myImmutableRef);
Output:
Before constructing Numbers are: 1 2
After constructing Numbers are: 1 5
Right way to write an immutable class
Right way is to copy the array before assigning in the constructor.
public final class MyImmutable { private final int[] myArray; public MyImmutable(int[] anArray) { this.myArray = anArray.clone(); // defensive copy } public String toString() { StringBuffer sb = new StringBuffer("Numbers are: "); for (int i = 0; i < myArray.length; i++) { sb.append(myArray[i] + " "); } return sb.toString(); } }
//the caller cannot change the array after calling the constructor.
int[] array = {1,2}; MyImmutable myImmutableRef = new MyImmutable(array) ; System.out.println("Before constructing " + myImmutableRef); array[1] = 5; // change (i.e. mutate) the element System.out.println("After constructing " + myImmutableRef);
Output:
Before constructing Numbers are: 1 2
After constructing Numbers are: 1 2
Exercises:
1.What is Immutable class. How to make a Immutable class
2.If we make a class immutable and it has one List reference how do we make sure the list is not getting modified by other references.
3.Write a class that is Immutable.
4.Why do we make Immutable class final.
5.How do we declare setter and getter in immutable class, What is the need of Immutable class.
6.If we have date and timestamp class which is mutable and we want another lass which takes these two types and create a datetimestamp object how to make sure that it is immutable .
发表评论
-
sapjco3 notes
2019-03-21 14:51 1104sapjco https://support.sap.com/ ... -
使用RestTemplate发送post JSON请求
2019-01-12 17:30 4595private final String BASE_URL = ... -
使用RestTemplate发送post JSON请求
2019-01-12 17:30 3402private final String BASE_URL = ... -
Spring线程池ThreadPoolTaskExecutor
2018-08-06 09:51 1791<!-- spring thread pool ex ... -
Spring注解事物@Transactional不工作
2018-08-02 18:50 2553“In proxy mode (which is the de ... -
创建前缀索引报长度超出错误
2018-07-25 15:44 1661表结构定义如下: CREATE TABLE `sku` ( ` ... -
Mysql Varchar字符长度
2018-07-25 15:23 1310`sku_name` VARCHAR(200) NOT NUL ... -
使用 Spring RestTemplate 发送 post 请求
2018-07-23 18:49 11648注意点: 1)使用MultiValueMap设置入参,不要使 ... -
Java动态代理Dynamic Proxy
2018-07-21 16:33 738JAVA学习篇--静态代理VS动态代理 https://blo ... -
分布式实时日志分析解决方案 ELK 部署架构
2018-07-20 09:52 1149原文链接:http://www.importn ... -
为什么HashMap容量一定要为2的幂呢?
2018-07-19 10:07 1666原文链接:https://blog.csdn.net/wang ... -
为什么计算HashCode时通常选择31这个数?
2018-07-19 10:05 1343摘自http://www.importnew.com/2208 ... -
jackson自定义序列化和反序列化
2018-07-10 18:47 2188原文链接:https://blog.csdn.net/liu ... -
Pay special attention when modifying online running system
2017-06-23 10:25 0Never remove any properties, me ... -
Map中的Null key, Null Value
2017-06-14 10:52 1901ConcurrentHashMap的key和value都不能为 ... -
Java语法糖
2017-06-05 20:03 488Java语法糖之foreach http://www.imp ... -
Java集合相关
2017-05-24 17:55 0Java集合框架:ArrayList http://www. ... -
Java数据类型的转换:隐式(自动)转换与强制转换
2017-05-14 10:46 0http://blog.csdn.net/u011240877 ... -
分布式开放消息系统(RocketMQ)的原理与实践
2017-05-07 19:55 685分布式开放消息系统(RocketMQ)的原理与实践 http ... -
面试知识点复习(Interview knowledge review)
2017-05-07 18:39 0JVM,多线程相关知识 http://darrenzhu.it ...
相关推荐
主要介绍了JAVA不可变类(immutable)机制与String的不可变性(推荐)的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
immutable - Javascript不可变的持久化数据集合
ImmutableCollections-基于数组的Java不可变集合 版权所有(c)2017 Nicholas Cull 有关许可信息,请参阅LICENSE.txt。 Java 1.8的基于数组的不可变集合 排序和未排序的变体 风格类似于番石榴收集包装 空友好 Java 8...
流行的Java面试题之一是:什么是不可变对象(immutable object),不可变对象有什么好处,在什么情况下应该用,或者更具体一些,Java的String类为什么要设成immutable类型? 不可变对象,顾名思义是创建后不可以...
Immutable 是 Facebook 开发的不可变数据集合。不可变数据一旦创建就不能被修改,是的应用开发更简单,允许使用函数式编程技术,比如惰性评估。Immutable JS 提供一个惰性 Sequence,允许高效的队列方法链,类似 map...
kotlinx.collections.immutable, Kotlin的不可变集合 Prototype Kotlin的不可变集合库 Kotlin的不可变集合接口和实现 Prototype 。有关详细信息,请参阅建议列表。Prototype实现基于 pcollections ( 版权 2015的作者...
dot-prop-immutable, 点prop的不可变版本,带有一些扩展名 dot-prop-immutable 点prop的不可变版本,带有一些扩展名。npm install dot-prop-immutable这个模块的动机是在不改变普通JavaScript对象的现有状态的情况
go-immutable-radix, 在Golang中,一个不可变的基数树实现 go-immutable-radix 提供实现不可变 radix的iradix 包。 包只提供单个 Tree 实现,针对稀疏节点优化。作为一个基数树,它提供以下内容:O(k) 操作。在许多...
不可变视图-其他集合对象的不可变视图概述immutable-views包提供的集合类是其他(可变)集合对象上的不可变视图: 另一个映射对象的不可变视图。 在另一个列表(序列)对象上的变视图。 在另一个set对象上的变视图。...
Immutable详解及React中实践.pdf
Java库的不可变集合(JImmutable Collections)是一组高性能的不可变集合,用于替换或补充标准的java.util集合。 为每个最常用的集合提供功能替换: Java类 JImmutable接口 工厂方法 数组列表 JImmutableList ...
提供Java不变/持久集合类的库。 尽管集合是不可变的,但它们提供了通过创建自身的新修改副本来添加和... 2-3树和整数trie类针对性能进行了优化,并且在需要不可变性以实现安全数据共享时可以与java.util集合一起使用。
在 Python 中一切都可以看作为对象。每个对象都有各自的 id, type 和 value。 id: 当一个对象被创建后,它的 id 就不会在改变,这里...常见的不可变对象(immutable objects): Number: int, float, complex string t
imobject允许您创建深度不可变JavaScript对象。 设置属性后,将无法添加其他属性,并且无法修改初始属性。 ## Installation要使用 ,请cd进入您的项目目录,并使用npm或yarn安装imobject。 npm $ cd /to/project/...
Kotlin的不可变集合接口和实现原型。
seamless-immutable: 不可变JS数据结构向后兼容常规数组和对象
如何使用只需加载build/Immutable.min.js ... 这意味着可变对象仅在它们是完全相同的对象时才相等,但不可变对象在具有相同值时才相等。 这两个可变对象是不同的,因此它们不相等: // falseequal ( { "foo" : 1 } , {
#ImmutableObject ImmutableObject 被创建为一种简单的客户端 Javascript 方式来制作不可变对象。 它使用一组简单的方法来获取和设置项目。 仅当您为isExtensible传递true参数时才允许设置项目。 ##基本用法: 可以...
@ polyn / immutable使用本机JavaScript功能(即Object.freeze )使对象不可变。 它使用来验证您定义的架构,并且还支持自定义验证器(即,如果您更喜欢JSON架构)。 与Object.freeze不同,@ polyn / immutable递归...