`

Guava 15新特性介绍

阅读更多
原文:http://www.javacodegeeks.com/2013/10/guava-15-new-features.html
Guava 是众所周知的google出品的开源工具包,十分好用,本月退出了version 15的版本,其中主要的几个新特性有:


1 Escapers字符转义器
  其目的就是在于将特殊字符进行转移,比如有:
HtmlEscapers
XmlEscapers
UrlEscapers
  还可以自定义escaper,比如:

  // escaping HTML
HtmlEscapers.htmlEscaper().escape("echo foo > file &");
// [result] echo foo > file &

// escaping XML attributes and content
XmlEscapers.xmlAttributeEscaper().escape("foo \"bar\"");
// [result] echo "bar"

XmlEscapers.xmlContentEscaper().escape("foo \"bar\"");
// [result] foo "bar"

// Custom Escaper
// escape single quote with another single quote
// and escape ampersand with backslash
Escaper myEscaper = Escapers.builder()
                            .addEscape('\'', "''")
                            .addEscape('&', "\&")
                            .build();

2 StandardSystemProperty
  这个是用来方便调用如java.version, java.home 等环境变量的,使用的是enum了,比如:
StandardSystemProperty.JAVA_VERSION.value();
// [result] 1.7.0_25

StandardSystemProperty.JAVA_VERSION.key();
// [result] java.version
3 EvictingQueue
  这个是一个非阻塞的队列,当队列长度满了后,自动移除头元素,比如:
  EvictingQueue<String> q = EvictingQueue.create(3);
q.add("one");
q.add("two");
q.add("three");
q.add("four");
// the head of the queue is evicted after adding the fourth element
// queue contains: [two, three, four]

4
fileTreeTraverser 文件遍历递归利器
   这个方法可以快速遍历某个文件目录下的所有文件,比如:
FluentIterable<File> iterable = Files.fileTreeTraverser().breadthFirstTraversal(new File("/var/tmp"));
for (File f : iterable) {
    System.out.println(f.getAbsolutePath());
}
其中f.getAbsolutePath()用的是jdk 7中的方法
3
3
分享到:
评论
1 楼 cywhoyi 2013-10-07  
Guava确实提供N多好的属性,最近我都在公司项目中替换掉,玩函数式编程,我之前写的bloghttp://cywhoyi.iteye.com/admin/blogs/1943291

相关推荐

Global site tag (gtag.js) - Google Analytics