[Guava学习笔记]Basic Utilities: Null, 前置条件, Object方法, 排序, 异常
我的技术博客经常被流氓网站恶意爬取转载。请移步原文:http://www.cnblogs.com/hamhog/p/3842433.html,享受整齐的排版、有效的链接、正确的代码缩进、更好的阅读体验。
Null
Guava用Optional表示可能为null的T类型引用。
创建:Optional.of(T)不接受null Optional.fromNullable(T)接受null Optional.absent()
查询:isPresent() get() or(T)如果为null则为T orNull()
其他:Objects.firstNonNull(T, T)用于给null指定默认值,如果两个都是null会抛NullPointerException
emptyToNull(String),nullToEmpty(String)
前置条件
静态import:
import static com.google.common.base.Preconditions.checkNotNull;
用法:
checkArgument(i >= 0, "Argument was %s but expected nonnegative", i);
checkArgument(i < j, "Expected i < j, but %s > %s", i, j);
checkArgument(boolean) checkNotNull(T) checkState(boolean)
checkElementIndex(int index, int size) checkPositionIndexes(int start, int end, int size)
可内联使用:this.field = checkNotNull(field)
object方法
Objects.equals(a,b)可null的equals Objects.hash(Object...)
toString():
// Returns "ClassName{x=1}"
Objects.toStringHelper(this).add("x", 1).toString();
// Returns "MyObject{x=1}"
Objects.toStringHelper("MyObject").add("x", 1).toString();
compare():
public int compareTo(Foo that) {
return ComparisonChain.start()
.compare(this.aString, that.aString)
.compare(this.anInt, that.anInt)
.compare(this.anEnum, that.anEnum, Ordering.natural().nullsLast())
.result();
}
排序
Ordering:Comparator的实现
构造:natural()自然序 usingToString()字典序 from(Comparator)
链式调用:reverse()逆序 nullsFirst() nullsLast() lexicographical() onResultOf(Function)
Ordering<Foo> ordering = Ordering.natural().nullsFirst().onResultOf(new Function<Foo, String>() {
public String apply(Foo foo) {
return foo.sortedBy;
}
});
集合和元素:greatestOf(Iterable iterable, int k) min(Iterable) min(E, E, E, E...)
isOrdered(Iterable) sortedCopy(Iterable)
[Guava学习笔记]Basic Utilities: Null, 前置条件, Object方法, 排序, 异常的更多相关文章
- Guava学习笔记(3):复写的Object常用方法
转自:http://www.cnblogs.com/peida/p/Guava_Objects.html 在Java中Object类是所有类的父类,其中有几个需要override的方法比如equals ...
- Google Guava学习笔记——基础工具类针对Object类的使用
Guava 提供了一系列针对Object操作的方法. 1. toString方法 为了方便调试重写toString()方法是很有必要的,但写起来比较无聊,不管如何,Objects类提供了toStrin ...
- 《C#并发编程经典实例》学习笔记—2.9 处理 async void 方法的异常
问题 需要处理从 async void 方法传递出来的异常. 解决方案 书中建议尽量不写 async void 这样的方法,如果非写不可,建议在方法内部 try catch 所有的代码,即在方法内部处 ...
- 《C#并发编程经典实例》学习笔记—2.8 处理 async Task 方法的异常
异常处理一直是所有编程语言不可避免需要考虑的问题,C#的异步方法的异常处理和同步方法并无差别,同样要借助 try catch 语句捕获异常. 首先编写一个抛出异常的方法 static async Ta ...
- guava 学习笔记(二) 瓜娃(guava)的API快速熟悉使用
guava 学习笔记(二) 瓜娃(guava)的API快速熟悉使用 1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: Introduction Guava Collection ...
- Guava学习笔记目录
Guava 是一个 Google 的基于java1.6的类库集合的扩展项目,包括 collections, caching, primitives support, concurrency libra ...
- guava 学习笔记 使用瓜娃(guava)的选择和预判断使代码变得简洁
guava 学习笔记 使用瓜娃(guava)的选择和预判断使代码变得简洁 1,本文翻译自 http://eclipsesource.com/blogs/2012/06/06/cleaner-code- ...
- SQL反模式学习笔记14 关于Null值的使用
目标:辨别并使用Null值 反模式:将Null值作为普通的值,反之亦然 1.在表达式中使用Null: Null值与空字符串是不一样的,Null值参与任何的加.减.乘.除等其他运算,结果都是Null: ...
- Guava学习笔记(1):Optional优雅的使用null
转自:http://www.cnblogs.com/peida/archive/2013/06/14/Guava_Optional.html 参考:[Google Guava] 1.1-使用和避免nu ...
随机推荐
- Codeforces GYM 100114 C. Sequence 打表
C. Sequence Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Description ...
- GLSL实现Fresnel And Chromatic aberration 【转】
http://blog.csdn.net/a3070173/archive/2008/11/13/3290091.aspx 使用Shader实现菲涅尔和颜色色散效果很简单,在Cg教程和OpenGL S ...
- [Oracle] Group By 语句的扩展 - Rollup、Cube和Grouping Sets
常常写SQL语句的人应该知道Group by语句的主要使用方法是进行分类汇总,以下是一种它最常见的使用方法(依据部门.职位分别统计业绩): SELECT a.dname,b.job,SUM(b.sal ...
- inux2.6.xx内核代码分析( 72节)
http://blog.csdn.net/ustc_dylan/article/category/469214
- C语言的ANSI/ISO标准
摘自:http://see.xidian.edu.cn/cpp/html/1658.html 从技术上讲有两种C语言标准,一种来自ANSI(American National Standard Ins ...
- linux后端运行
程序命令 & :将命令放入后台运行. Ctrl + z : 把一个正在运行的前端命令转移到后台运行,它等效于:程序命令 & :这样虽然把程序放在了后端运行,但是此时程序状态为暂停状态, ...
- Helpers\Document
Helpers\Document The document class is a collection of useful methods for working with files. To get ...
- 命令行中使用adb安装apk
转载:http://blog.sina.com.cn/s/blog_8324d8e80101b8dn.html 在你的android—IDE中找到D:\Softwave_Ghost\技术软件\IDE\ ...
- 如何在VS C++中高亮用户自定义关键字
这篇文章主要参考一篇英文博客,具体步骤如下: 1.在VS中找到msdev.exe所在的目录,一般在...\Microsoft Visual Studio.NET\Common7\IDE\devnev. ...
- html笔记01:顺序和无序列表
<!DOCTYPE html> <html> <body> <li>Yellow <ul><li>Wet soil</li ...