Googgle guava ImmutableCollections
Googgle guava ImmutableCollections
参考文档
Google Guava ImmutableCollections官方文档
Immutable objects have many advantages(不可变类有很多的好处)
- Safe for use by untrusted libraries(安全的使用不受信任的库),我记得Effective Java中看到过,由于传递的是集合或者对象,这会导致很多的情况,外部可能会改变这个传入的对象,或者当前类返回出去的集合或者对象也可能会使用者破坏当前返回的引用的信息,所以要保护好,得到的和出去的不会被外人破坏。
- Thread-safe: can be used by many threads with no risk of race conditions.(线程安全:可以被许多线程使用,没有竞争条件的风险) 每个线程都是获取到了一个不可变类的一个副本。
- 可以用作常数,期望它将保持固定。
- 制作不可变的对象副本是一个很好的防御编程技术a good defensive programming technique
- 当您不希望修改集合,或期望集合保持不变时,防御性地将其复制到不可变集合中是一个很好的做法。
怎么创建不可变类
- copyOf()
public static void usingCopyOf(){Set<Integer> set = new HashSet<Integer>();set.add(1);set.add(2);ImmutableSet<Integer> immutableSet = ImmutableSet.copyOf(set);//这里是不支持添加元素的/*immutableSet.add(4);Set<Integer> addOne = new HashSet<>();addOne.add(5);immutableSet.addAll(addOne);*/}
- of()
of 方法重载了很多的个数,最后一个是可以随意的扩展参数。
public static void usingOf(){ImmutableSet<Integer> ofSet = ImmutableSet.of(1,2,3,4,5,6,7);System.out.println(ofSet);
}
- 使用建筑者模式创建
public static void usingBuilder(){ImmutableSet<Integer> builderSet = ImmutableSet.<Integer>builder().add(1).add(2).build();System.out.println(builderSet);
}/*** Returns a new builder. The generated builder is equivalent to the builder* created by the {@link Builder} constructor.*/public static <E> Builder<E> builder() {return new Builder<E>();}
- asList()
所有不可变的集合提供了一个ImmutableList视图asList()即使你有数据存储为一个ImmutableSortedSet,你可以得到k最小的元素sortedSet.asList().get(k)。
主要的集合
可变集合类型 | 可变集合源:JDK or Guava | Guava不可变集合 |
---|---|---|
Collection | JDK | ImmutableCollection |
List | JDK | ImmutableList |
Set | JDK | ImmutableSet |
SortedSet/NavigableSet | JDK | ImmutableSortedSet |
Map | JDK | ImmutableMap |
SortedMap | JDK | ImmutableSortedMap |
Multiset | Guava | ImmutableMultiset |
SortedMultiset | Guava | ImmutableSortedMultiset |
Multimap | Guava | ImmutableMultimap |
ListMultimap | Guava | ImmutableListMultimap |
SetMultimap | Guava | ImmutableSetMultimap |
BiMap | Guava | ImmutableBiMap |
ClassToInstanceMap | Guava | ImmutableClassToInstanceMap |
Table | Guava | ImmutableTable |
接下来就是了解这些集合啦
Googgle guava ImmutableCollections相关推荐
- Guava源码分析——Immutable Collections(4)
Immutable的集合体系,还有中很重要的集合没有介绍,就是ImmutableMap,通过UML图,可以看出ImmutableMap的结构体系. 首先来看一下ImmutableBiMap,因为普通I ...
- Guava的介绍与使用示例
Guava是什么 Guava,翻译是番石榴.这里指的是Google Core Libraries for Java, 即Google开发的Java核心库的扩展. 其提供新的集合类型(比如multima ...
- Error:Could not download guava.jar (com.google.guava:guava:19.0): No cached version available for of
今天从git导入demo 报错 Error:Could not download guava.jar (com.google.guava:guava:19.0): No cached version ...
- Guava Cache本地缓存在 Spring Boot应用中的实践
概述 在如今高并发的互联网应用中,缓存的地位举足轻重,对提升程序性能帮助不小.而 3.x开始的 Spring也引入了对 Cache的支持,那对于如今发展得如火如荼的 Spring Boot来说自然也是 ...
- Guava Cache缓存的移除与读取
2019独角兽企业重金招聘Python工程师标准>>> 1.通过put或putAll手动向Cache中添加数据,guava不缓存value是null的key.我们可以在系统启动的时候 ...
- 为什么我不建议你用阿里巴巴Java规范,而使用 Google Guava 编程?
点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 来自 | 张丰哲 链接 | www.jianshu.com ...
- 震惊 Guava 竟然有坑
点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 转自:Java布道 最近,团队里边一个兄弟突然叫我:快来看 ...
- Guava 中的 Stopwatch 是个什么鬼?
点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! Stopwatch 解释为计时器,又称秒表.停表,很明显它 ...
- RateLimiter 源码分析(Guava 和 Sentinel 实现)
点击上方"方志朋",选择"设为星标" 做积极的人,而不是积极废人 作者javadoop,资深Java工程师 原文链接https://www.javadoop.c ...
- 为什么推荐 Java 程序员使用 Google Guava 编程
点击上方"方志朋",选择"设为星标" 做积极的人,而不是积极废人 来自 | 张丰哲 链接 | www.jianshu.com/p/97778b21bd00 前言 ...
最新文章
- 从Hive导出数据到Oracle数据库--Sqoop
- (C++)数组作为函数参数
- java aes php_php和java的aes默认加密算法有点区别及解决方法。
- mysql order by newid_order by newid() 随机查询
- HttpServletRequest说明
- bzoj千题计划141:bzoj3532: [Sdoi2014]Lis
- TimesTen Classic 18c 卸载 (uninstall)全过程
- Python图像处理丨认识图像锐化和边缘提取的4个算子
- 深度学习——卷积神经网络(CNN)
- 手算逆元及手动模拟扩展欧几里得算法及思路推导
- 数据中台POC演示流程
- 给大家推荐一款超低功耗的4G低功耗摄像头方案强大的AI功能
- Android内存泄漏leakcanary2.7
- 弹性盒子flex布局
- QCC514x-QCC304x(headset)系列(实战篇)之3.2 如何使用按键开机和关机
- lock与unlock
- android 监听短信并获取验证码
- 头脑风暴-移动搜索和传统搜索的不同之处
- Python+Selenium自动化脚本实现列表信息的多列内容获取
- ZMQ/ZeroMQ简介