Google Guava 使用经验总结
来源:互联网 发布:经典书籍推荐知乎 编辑:程序博客网 时间:2024/05/17 04:55
Google 的guava 和 Apache 的 commons 是两个很好用的类库,里面有的包存在功能重叠,但是对我们来说择优而用显然可以极大的提高开发效率。
网上教程很多,大概写写自己用到的
Guava:
1. Optional
有效避免空指针错误。让Null快速失败来拒绝null值是一个非常好的选择。
Java 8 也引入了这个功能,并且在这个基础上还有扩展。
下面是一个简单的示例:
public static void main(String[] args) { GuavaOptional guavaOptional = new GuavaOptional(); Integer value1 = null; Integer value2 = new Integer(10); //Optional.fromNullable - allows passed parameter to be null. Optional<Integer> a = Optional.fromNullable(value1); //Optional.of - throws NullPointerException if passed parameter is null Optional<Integer> b = Optional.fromNullable(value2); System.out.println(guavaOptional.sum(a,b));}public Integer sum(Optional<Integer> a, Optional<Integer> b){ Integer value1 = a.or(0); Integer value2 = b.or(0); return value1 + value2;}
2. Preconditions 前置条件
Lombok 的@NonNull 也可以做非空的输入判断,原理是增加额外代码
public Integer sum(@NonNull Optional<Integer> a, Optional<Integer> b) { if(a == null) { throw new NullPointerException("a"); } else { Integer value1 = (Integer)a.or(Integer.valueOf(0)); Integer value2 = (Integer)b.or(Integer.valueOf(0)); return Integer.valueOf(value1.intValue() + value2.intValue()); }}
Preconditions 不仅可以做非空检查,还有:checkArgument(), checkNotNull(), checkState(), checkElementIndex(), checkPositionIndex() 等等,相比自由度更高。 而且还不会降低UT coverage率。当发生错误的时候自动抛出运行时异常。
3. Objects:
Object.equals 在使用之前必须要进行null检查。Objects.equals省去了这个过程。
Objects.hashCode(field1, field2, ......):在override一个bean的hashCode()函数的时候,可以选择Lombok @Data自动生成,也可以选择自己手动选择需要的fileds进行哈西散列设计,Obeject.hashCode()则会对传入的字段序列计算合理的哈西值。
Objects.toString帮助编写有用的toString()方法。
为了实现比较器,类要继承Comparable接口,也可以通过ComparisonChain来实现其中的compareTo()方法。(同理Comparator)
4. Ordering(Comparator的实现)
可以生成各种复杂的排序器。用例: Collections.sort(list, ordering);
5. Collections(集合):
不可变集合:ImmutableSet, ImmutableList, ImmutableMap 等等
优点:作为常量来安全使用,多线程安全,节省时间空间,更好的内存利用率
JDK也提供了相似的方法: Collections.unmodifiableXXX 来把集合包装为不可变形式,但是相比之下:笨重,不安全,低效。
public static final ImmutableMap<String, Integer> COLOR = ImmutableMap.of( "yello", 1, "red", 2, "black", 3);新集合类型:multisets, multimaps, tables, bidirectional maps等等
强大的集合工具类:java.util.Collections 已经非常常用了,Guava沿用这些路线提供了更多的工具方法。这是Guava最流行和成熟的部分之一。
比如在UT 中常用的List生成代码:
List<String> testList = Lists.newArrayList("a", "b", "c", "d");
6. 字符串处理
在我们Team已经非常流行了。
Joiner: 字符串连接,传统的字符串连接,如果含有null,操作会出现问题。
Joiner.on("-").skipNulls().join("a", "b", "c", "d", null)Splitter: 字符串拆分
之前的String.split()功能比较单一。
Splitter.on(",") .trimResults() .omitEmptyStrings() .split("a,b, c, , d")
可以得到结果 [a, b, c, d]
CharMatcher: 字符匹配器
Charsets: 字符集
CaseFormat: 大小写格式
CaseFormat.LOWER_HYPHEN.to(CaseFormat.LOWER_UNDERSCORE, "bob-is-good-boy")可以得到输出: bob_is_good_boy
更多的实用工具:I/O,数学运算,缓存,并发等等。
Apache.Commons
- Google Guava 使用经验总结
- google guava简单使用
- google guava使用介绍
- google Guava Cache使用
- google guava使用例子
- Google Guava Collections 使用介绍
- Google Guava Collections 使用介绍
- Google Guava Collections 使用介绍
- Google Guava Collections 使用介绍
- Google Guava Collections 使用介绍
- Google Guava Collections 使用介绍
- Google Guava Collections 使用介绍
- Google Guava Collections 使用介绍
- Google Guava Collections 使用介绍
- Google Guava Collections 使用介绍
- Google Guava Collections 使用介绍
- Google Guava Collections 使用介绍
- Google Guava Collections 使用介绍
- 来自麻省理工的信息抽取
- 用qt和seetaface制作人脸识别程序的准备工作备忘
- Linux编程预习
- Jhipster初识
- Makefile自动产生依赖
- Google Guava 使用经验总结
- Qt视图场景框架之SVG
- win下安装spring boot cli
- [arc082e]ConvexScore
- 消息队列-ActiveMQ学习笔记(二)-点对点消息实现
- mysql安装全过程
- 零基础学图形学(5) 几何知识——矩阵
- 使用正则表达式进行匹配
- 图片生成beta64