Guava入門

?? Google Guava是什么東西?首先要追溯到2007年的“Google Collections Library”項(xiàng)目,它提供對Java 集合操作的工具類。后來Guava被進(jìn)化為Java程序員開發(fā)必備的工具。Guava可以對字符串,集合,并發(fā),I/O,反射進(jìn)行操作。它提供的高質(zhì)量的 API 可以使你的JAVa代碼更加優(yōu)雅,更加簡潔,讓你工作更加輕松愉悅。

guava類似Apache Commons工具集

1、Guava地址

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

2、基本工具包Base

基本工具包用很多,這次學(xué)到了Collections2

Collections2

Collections2中提供了幾個(gè)實(shí)用的靜態(tài)方法,今天試用一下。其中filter和transform有函數(shù)式的味道。

  • 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中,當(dāng)在上面的result中增加了元素后,會(huì)直接影響原來的names這個(gè)list的,就是names中的集合元素是5了。
com.google.common.base. Predicate : 根據(jù)輸入值得到 true 或者 false .

  • transform():類型轉(zhuǎn)換
    例:把Lis<Integer>中的Integer類型轉(zhuǎn)換為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);
   }
}

需要說明的是每次調(diào)用返回都是新的對象,同時(shí)操作過程不是線程安全的。

多個(gè)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;
           }
       };
       //轉(zhuǎn)成大寫
       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

通常我們進(jìn)行程序耗時(shí)計(jì)算和性能調(diào)試的時(shí)候,需要統(tǒng)計(jì)某段代碼的運(yùn)行時(shí)間,這個(gè)時(shí)候就是stopwatch的用武之處。
使用:

Stopwatch stopwatch = Stopwatch.createStarted();
//doSomething();
stopwatch.stop();
long millis = stopwatch.elapsed(MILLISECONDS);
log.info("time: " + stopwatch); // formatted string like "12.3 ms"
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,940評論 25 709
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,540評論 19 139
  • Google Guava是一個(gè)比較有趣的框架,它提供了很多有趣的的功能,我們先預(yù)覽一下google Guava 給...
    _aoj閱讀 1,977評論 0 0
  • //Clojure入門教程: Clojure – Functional Programming for the J...
    葡萄喃喃囈語閱讀 4,037評論 0 7
  • 甘德禮(關(guān)注解決)持續(xù)原創(chuàng)分享第165天 房子裝修正在進(jìn)行中。這期間各種不容易和各種意想不到讓我一直要面對很多的問...
    華南帝虎閱讀 186評論 4 5

友情鏈接更多精彩內(nèi)容