Google Guava常用类库
2015-03-20 18:41
288 查看
什么是Guava
Guava是一个增强Java功能的类库。Guava工程包含了若干被Google的 Java项目广泛依赖的核心库,例如:集合 [collections] 、缓存 [caching] 、原生类型支持 [primitives support] 、并发库 [concurrency libraries] 、通用注解 [common annotations] 、字符串处理 [string processing] 、I/O 等等。 所有这些工具每天都在被Google的工程师应用在产品服务中。
Guava常用方法
GuavaIO
Guava的Files.readLines()有点脚本语言的意思,但是处理大文件就不方便了import java.io.File; import java.io.IOException; import java.net.URL; import java.util.List; import com.google.common.base.Charsets; import com.google.common.io.Files; import com.google.common.io.Resources; /** * @author wangchao * @date 15-3-23 下午4:06 */ public class Test { public static void main(String[] args) throws IOException { // 一次性读取文件到List List<String> fileList = Files.readLines(new File(""), Charsets.UTF_8); // 一次性读取URL到List List<String> urlList = Resources.readLines(new URL(""), Charsets.UTF_8); String buffer = ""; // 写入文件 Files.write(buffer, new File(""), Charsets.UTF_8); // 追加文件 Files.append(buffer, new File(""), Charsets.UTF_8); } }
Guava String
对字符串操作的封装,包括判断字符串是否为空,连接字符串,拆分字符串等常用的字符串的操作。import java.io.IOException; import java.util.Map; import com.google.common.base.Joiner; import com.google.common.base.Splitter; import com.google.common.base.Strings; /** * @author wangchao * @date 15-3-23 下午2:48 */ public class Test { public static void main(String[] args) throws IOException { String str = "a=1, b=2 ; c=3"; // 检查字符串是否为空或者null System.out.println(Strings.isNullOrEmpty(str)); // false // Splitter拆分字符串(可去掉空格),比较蛋疼的是返回类型是Iterable。 Iterable<String> splitResults = Splitter.onPattern("[,;]").trimResults().omitEmptyStrings().split(str); System.out.println(splitResults.toString()); // [a=1, b=2, c=3] // 同样可以用Joiner拼接 String joinResult = Joiner.on(",").join(splitResults); System.out.println(joinResult); // a=1,b=2,c=3 // 二次拆分,返回Map类型 Map<String, String> kvs = Splitter.onPattern("[,;]").trimResults().omitEmptyStrings() .withKeyValueSeparator('=').split(str); System.out.println(kvs.toString()); // {a=1, b=2, c=3} // 使用Joiner拼接 String kvsJoinResult = Joiner.on(",").withKeyValueSeparator("=").join(kvs); System.out.println(kvsJoinResult); // a=1,b=2,c=3 } }
Guava 新集合类
类名 | 特点 |
---|---|
Multiset | 对key进行计数 |
Multimap | 允许一个key对应多个value |
BiMap | 允许key和value进行反转 |
Table | 允许两个key对应一个value |
RangeSet | 描述了一组不相连的、非空的区间 |
RangeMap | 描述了一组不相连的、非空的区间,对应value |
Guava学习网站
内存.溢出 http://outofmemory.cn/java/guava/并发编程网 http://ifeve.com/google-guava/
在线文档 http://tool.oschina.net/apidocs/apidoc?api=guava
相关文章推荐
- Google的Java常用类库 Guava资料
- Google的Java常用类库 Guava资料
- Google的Java常用类库 Guava
- Guava介绍 – Google的Java常用类库
- Guava学习笔记:Google Guava 类库简介
- Guava学习笔记:Google Guava 类库简介
- Guava学习笔记:Google Guava 类库简介
- Guava 14.0 发布,Java 常用类库
- Guava学习笔记:Google Guava 类库简介
- Google Guava 类库简介
- Guava学习笔记:Google Guava 类库简介
- Java -- common基础类库Google Guava和Guice
- Google类库 Guava
- Guava学习笔记:Google Guava 类库简介
- Google Guava 类库 笔记
- Google Guava 常用功能
- Guava学习笔记:Google Guava 类库简介
- Guava学习笔记:Google Guava 类库简介
- Google的Java经常使用类库 Guava
- 最近公司引用了Google的一个基于java1.6的类库Guava,详细介绍查看下面的博客