Guava collections -- Immutable Collections
来源:互联网 发布:网络监控软件哪个好用 编辑:程序博客网 时间:2024/06/04 19:33
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() { ImmutableMultimap<Integer, String> multimap = new ImmutableMultimap.Builder<Integer, String>().put(1, "Foo") .putAll(2, "Foo", "Bar", "Baz") .putAll(4, "Huey", "Duey", "Luey") .put(3, "Single").build(); Collection<String> 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() { ImmutableList<String> immutableList = new ImmutableList.Builder<String>().add("foo").add("bar").build(); immutableList.add("baz"); }
2、不能删除对象
@Test(expected = UnsupportedOperationException.class) public void testRemoveImmuableCollection() { ImmutableList<String> immutableList = new ImmutableList.Builder<String>().add("foo").add("bar").build(); immutableList.remove("bar"); }
3、Change Object
@Test public void testChangeObjectImmutableCollection() { MutableObject mutableObject = new MutableObject("initialState"); ImmutableList<MutableObject> immutableList = new ImmutableList.Builder<MutableObject>().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; } }
更多功能等待你的发现。
- Guava collections -- Immutable Collections
- Guava Immutable和JDK Collections.unmodifiableXXX不可变集合比较
- Google Guava Collections
- Google Guava Collections
- Guava Collections使用介绍
- Guava collections -- FluentIterable
- Guava collections -- Lists
- Guava collections -- Sets
- Guava collections -- Maps
- Guava collections -- Multimaps
- Guava collections -- BiMap
- Guava collections -- Table
- Guava collections -- Range
- Guava collections -- Ordering
- Google Guava Collections 使用介绍
- Google Guava Collections 使用介绍
- Google Guava Collections 使用介绍
- Google Guava Collections 使用介绍
- 【已解决】UnicodeEncodeError: ‘gbk’ codec can’t encode character u’\u200e’ in position 43: illegal multib
- Xcode工程目录的 folder 和 group的区别(蓝色和黄色文件夹的区别)
- HDU 3422, POJ 1305简单的本原勾股数组
- C++ 如何解决string类型无法比较大小的问题
- linux 打包分割合并,解压文件
- Guava collections -- Immutable Collections
- Python中的yield from语法
- 设计模式之简单工厂设计模式
- 为何Hadoop中的块如此之大
- ASP.NET获取<EmptyDataTemplate></EmptyDataTemplate>模板中的textBox控件值
- java语法基础
- 下载maven JAR包太慢?各种方法解决。不再为此烦恼!
- Java 程序死锁问题原理及解决方案
- 二维树狀数组模板