美文网首页
Guava入门

Guava入门

作者: TimX | 来源:发表于2018-01-12 21:27 被阅读0次

   Google Guava是什么东西?首先要追溯到2007年的“Google Collections Library”项目,它提供对Java 集合操作的工具类。后来Guava被进化为Java程序员开发必备的工具。Guava可以对字符串,集合,并发,I/O,反射进行操作。它提供的高质量的 API 可以使你的JAVa代码更加优雅,更加简洁,让你工作更加轻松愉悦。

guava类似Apache Commons工具集

1、Guava地址

     <dependency>
         <groupId>com.google.guava</groupId>
         <artifactId>guava</artifactId>
         <version>14.0</version>
      </dependency>

2、基本工具包Base

基本工具包用很多,这次学到了Collections2

Collections2

Collections2中提供了几个实用的静态方法,今天试用一下。其中filter和transform有函数式的味道。

  • filter主要是提供了对集合的过滤功能。
@Test  
public List<String> whenFilterWithCollections2_thenFiltered() {  
    List<String> names = Lists.newArrayList("John", "Jane", "Adam", "Tom");  
   Collection<String> result  = Collections2.filter(names,
            new Predicate<String>() {
                @Override
                public boolean apply(@Nullable String str) {
                    if (str == null) {
                        return false;
                    }
                    return true;
                }
            });
    result.add("anna");  
    assertEquals(5, names.size());
  return Lists.newArrayList(result);
} 

这里注意的是,Collections2.filter中,当在上面的result中增加了元素后,会直接影响原来的names这个list的,就是names中的集合元素是5了。
com.google.common.base. Predicate : 根据输入值得到 true 或者 false .

  • transform():类型转换
    例:把Lis<Integer>中的Integer类型转换为String , 并添加test作为后缀字符:
public class TransformDemo {
   public static void main(String[] args) {
       Set<Long> times= Sets.newHashSet();
       times.add(91299990701L);
       times.add(9320001010L);
       times.add(9920170621L);
       Collection<String> timeStrCol= Collections2.transform(times, new Function<Long, String>() {
           @Nullable
           @Override
           public String apply(@Nullable Long input) {
               return new SimpleDateFormat("yyyy-MM-dd").format(input);
           }
       });
       System.out.println(timeStrCol);
   }
}

需要说明的是每次调用返回都是新的对象,同时操作过程不是线程安全的。

多个Function组合:

public class TransformDemo {
   public static void main(String[] args) {
       List<String> list= Lists.newArrayList("abcde","good","happiness");
       //确保容器中的字符串长度不超过5
       Function<String,String> f1=new Function<String, String>() {
           @Nullable
           @Override
           public String apply(@Nullable String input) {
               return input.length()>5?input.substring(0,5):input;
           }
       };
       //转成大写
       Function<String,String> f2=new Function<String, String>() {
           @Nullable
           @Override
           public String apply(@Nullable String input) {
               return input.toUpperCase();
           }
       };
       Function<String,String> function=Functions.compose(f1,f2);
       Collection<String> results=Collections2.transform(list,function);
       System.out.println(results);
   }
}

Stopwatch

通常我们进行程序耗时计算和性能调试的时候,需要统计某段代码的运行时间,这个时候就是stopwatch的用武之处。
使用:

Stopwatch stopwatch = Stopwatch.createStarted();
//doSomething();
stopwatch.stop();
long millis = stopwatch.elapsed(MILLISECONDS);
log.info("time: " + stopwatch); // formatted string like "12.3 ms"

相关文章

网友评论

      本文标题:Guava入门

      本文链接:https://www.haomeiwen.com/subject/dwqqoxtx.html