Guava collections -- Immutable Collections
发布日期:2021-06-29 12:52:00
浏览次数:2
分类:技术文章
本文共 3142 字,大约阅读时间需要 10 分钟。
Guava全文介绍地址:Google Guava
这次主要介绍是的是com.google.common.collect.Immutable Collections.一个集合的内容永远不会改变,下面详细的提供了一些额外的担保。警告
:避免直接使用ImmutableCollection做为一个type(正如集合本身)。更好使用它的子类型如ImmutableSet或ImmutableList,很好的定义了超类Object.equals(java . lang . object)语义,从而避免常见错误和混乱。
Immutablecollections的特性:
1.Shallow immutability
(浅不变性)。元素不能被添加,删除或替换在这个集合。这是一个更强的保证比Collections.unmodifiableCollection(java.util.Collection),它只要包装集合被修改的它的内容也会变化。 2. Null-hostility
(禁止null值)。这个collection永远不会包含null值元素。 3. Deterministic iteration
(确定的迭代)。迭代顺序总是定义良好的,这取决于创建集合(有关详细信息,请参阅相应的工厂方法)。视图集合如Multiset.elementSet()遍历的顺序一样父类,除了被特殊标记的。 4. Thread safety
(线程安全)。从多个线程同时访问此集合是安全的。 5. Integrity
(完整性)。此类型不能被这个包外被继承(这将允许这些担保被违反)。 性能说明:
1. 实现一般认为优先考虑内存效率,然后访问速度,最后创建的速度。 2. copyOf方法有时会认为真正的复制操作是不必要的;例如,copyOf(copyOf(anArrayList))应该只有一次复制数据。这降低了习惯性地在API创建防御副本的费用代价。然而,跳过复制操作的精确条件是未定义的。 3. 警告:ImmutableMap.keySet或ImmutableList.subList(int,int)可能保留整个数据集一个引用,防止它被垃圾收集。如果一些数据可以通过其他手段不再可访问,这就构成了一个内存泄漏。可以通过视图收集的copyOf方法获得正确size的副本。 4. 使用相关的Builder类的性能可以认为是非常糟糕的,甚至更好,但是相比创建一个可变集合和复制它可能更好。 5. 实现一般不会缓存哈希码。如果你的元素或key类型有一个slow的hashCode方法实现,它应该缓存它本身。下面就介绍一个Immutable Collections的主要用法:
1、创建ImmutableMap
@Test public void testCreateImmutableMap() { ImmutableMultimapmultimap = new ImmutableMultimap.Builder ().put(1, "Foo") .putAll(2, "Foo", "Bar", "Baz") .putAll(4, "Huey", "Duey", "Luey") .put(3, "Single").build(); Collection list = Lists.newArrayList("Single"); assertThat(multimap.get(3), is(list)); list = Lists.newArrayList("Huey", "Duey", "Luey"); assertThat(multimap.get(4), is(list)); }
2、Shallow immutability
1、不能添加对象
@Test(expected = UnsupportedOperationException.class) public void testAddImmutableCollection() { ImmutableListimmutableList = new ImmutableList.Builder ().add("foo").add("bar").build(); immutableList.add("baz"); }
2、不能删除对象
@Test(expected = UnsupportedOperationException.class) public void testRemoveImmuableCollection() { ImmutableListimmutableList = new ImmutableList.Builder ().add("foo").add("bar").build(); immutableList.remove("bar"); }
3、Change Object
@Test public void testChangeObjectImmutableCollection() { MutableObject mutableObject = new MutableObject("initialState"); ImmutableListimmutableList = new ImmutableList.Builder ().add(mutableObject).build(); assertThat(immutableList.get(0).getMutableProperty(), is("initialState")); immutableList.get(0).setMutableProperty("changedState"); assertThat(immutableList.get(0).getMutableProperty(), is("changedState")); } private class MutableObject { private String mutableProperty; private MutableObject(String mutableProperty) { this.mutableProperty = mutableProperty; } private String getMutableProperty() { return mutableProperty; } private void setMutableProperty(String mutableProperty) { this.mutableProperty = mutableProperty; } }
更多功能等待你的发现。
转载地址:https://carlzone.blog.csdn.net/article/details/51865879 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2024年04月30日 03时47分06秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
excel文本函数
2019-04-29
excel时间日期函数
2019-04-29
excel查找引用函数
2019-04-29
棱镜门事件
2019-04-29
拼多多远程删除用户照片事件
2019-04-29
power BI desktop
2019-04-29
中国信息化发展八大趋势(一)
2019-04-29
中国信息化发展八大趋势(二)
2019-04-29
中国信息化发展八大趋势(三)
2019-04-29
中国信息化发展八大趋势(四)
2019-04-29
电商大战二十年
2019-04-29
神奇的麦考林
2019-04-29
centos7使用rpm安装mysql遇到的error: Failed dependencies: mysql-community-common(x86-64) 5.7.9 is needed
2019-04-29
spark报错:warn util.utils::service ‘sparkUI‘ can not bind on part 4040.Attempting port 4041.4042等错误
2019-04-29
Error getting authority: Error initializing authority: Could not connect: No such file or directory
2019-04-29
编程程软件测试思维方式:如何科学制定测试计划
2019-04-29
BLE蓝牙4.0串口调试助手
2019-04-29
树莓派WIFI设置
2019-04-29
用vcgencmd获取树莓派硬件状态数据
2019-04-29