Google Guava新手教程

???? 以下資料整理自網絡

一、Google Guava入門介紹

引言

Guavaproject包括了若干被Google的 Java項目廣泛依賴 的核心庫,比如:集合 [collections] 、緩存 [caching] 、原生類型支持 [primitives support] 、并發庫 [concurrency libraries] 、通用注解 [common annotations] 、字符串處理 [string processing] 、I/O 等等。 全部這些工具每天都在被Google的project師應用在產品服務中。

查閱Javadoc并不一定是學習這些庫最有效的方式。在此,我們希望通過此文檔為Guava中最流行和最強大的功能。提供更具可讀性和解釋性的說明。

譯文格式說明

  • Guava中的類被首次引用時,都會鏈接到Guava的API文檔。如:Optional<T>。
  • Guava和JDK中的方法被引用時。一般都會鏈接到Guava或JDK的API文檔,一些人所共知的JDK方法除外。如:Optional.of(T), Map.get(key)。

  • 譯者對文檔的額外說明以斜體顯示,而且以“譯者注:”開始。

文件夾

1. 基本工具 [Basic utilities]

讓使用Java語言變得更舒適

1.1 使用和避免null:null是模棱兩可的,會引起令人困惑的錯誤。有些時候它讓人非常不舒服。

非常多Guava工具類用高速失敗拒絕null值,而不是盲目地接受

1.2 前置條件: 讓方法中的條件檢查更簡單

1.3 常見Object方法: 簡化Object方法實現。如hashCode()和toString()

1.4 排序: Guava強大的”流暢風格比較器”

1.5 Throwables:簡化了異常和錯誤的傳播與檢查

2. 集合[Collections]

Guava對JDK集合的擴展,這是Guava最成熟和為人所知的部分

2.1 不可變集合: 用不變的集合進行防御性編程和性能提升。

2.2 新集合類型: multisets, multimaps, tables, bidirectional maps等

2.3 強大的集合工具類: 提供java.util.Collections中沒有的集合工具

2.4 擴展工具類:讓實現和擴展集合類變得更easy。比方創建Collection的裝飾器,或實現迭代器

3. 緩存[Caches]

Guava Cache:本地緩存實現,支持多種緩存過期策略

4. 函數式風格[Functional idioms]

Guava的函數式支持能夠顯著簡化代碼。但請慎重使用它

5. 并發[Concurrency]

強大而簡單的抽象,讓編寫正確的并發代碼更簡單

5.1 ListenableFuture:完畢后觸發回調的Future

5.2 Service框架:抽象可開啟和關閉的服務,幫助你維護服務的狀態邏輯

6. 字符串處理[Strings]

非常實用的字符串工具。包括切割、連接、填充等操作

7. 原生類型[Primitives]

擴展 JDK 未提供的原生類型(如int、char)操作, 包括某些類型的無符號形式

8. 區間[Ranges]

可比較類型的區間API。包括連續和離散類型

9. I/O

簡化I/O尤其是I/O流和文件的操作,針對Java5和6版本號

10. 散列[Hash]

提供比Object.hashCode()更復雜的散列實現。并提供布魯姆過濾器的實現

11. 事件總線[EventBus]

公布-訂閱模式的組件通信,但組件不須要顯式地注冊到其它組件中

12. 數學運算[Math]

優化的、充分測試的數學工具類

13. 反射[Reflection]

Guava 的 Java 反射機制工具類

???? 中文參考站點:http://ifeve.com/google-guava/

二、Guava學習筆記:Preconditions優雅的檢驗參數

  在日常開發中。我們常常會對方法的輸入參數做一些數據格式上的驗證,以便保證方法能夠依照正常流程運行下去。對于可預知的一些數據上的錯誤。我們一定要做事前檢測和推斷,來避免程序流程出錯,而不是全然通過錯誤處理來保證流程正確運行,畢竟錯誤處理是比較消耗資源的方式。

在尋常情況下我們對參數的推斷都須要自己來逐個寫方法推斷,代碼量不少而且復用性不高,例如以下所看到的:

復制代碼
import org.junit.Test;public class PreconditionsTest {@Testpublic void Preconditions() throws Exception { getPerson(8,"peida");getPerson(-9,"peida");getPerson(8,"");getPerson(8,null);}public static void getPerson(int age,String neme)throws Exception{if(age>0&&neme!=null&&neme.isEmpty()!=true){System.out.println("a person age:"+age+",neme:"+neme);}else{System.out.println("參數輸入有誤。");}}
}
復制代碼

  說明:參數驗證。我們每次都要加入if語句來做推斷, 反復的工作會做好多次。getPerson方法僅僅有2個參數。驗證規則也不是非常復雜,假設參數過度,驗證規則復雜后,上面代碼的可讀性都會非常差的,復用性就更談不上了。

  Guava類庫中提供了一個作參數檢查的工具類--Preconditions類,?該類能夠大大地簡化我們代碼中對于參數的預推斷和處理。讓我們對方法輸入參數的驗證實現起來更加簡單優雅。以下我們看看Preconditions類的使用實例:

Preconditions里面的方法:

  1 .checkArgument(boolean) :
  功能描寫敘述:檢查boolean是否為真。 用作方法中檢查參數
  失敗時拋出的異常類型: IllegalArgumentException

  2.checkNotNull(T): ?? ?
  功能描寫敘述:檢查value不為null, 直接返回value;
  失敗時拋出的異常類型:NullPointerException

  3.checkState(boolean):
  功能描寫敘述:檢查對象的一些狀態。不依賴方法參數。 比如, Iterator能夠用來next是否在remove之前被調用。
  失敗時拋出的異常類型:IllegalStateException

  4.checkElementIndex(int index, int size):
  功能描寫敘述:檢查index是否為在一個長度為size的list, string或array合法的范圍。 index的范圍區間是[0, size)(包括0不包括size)。

無需直接傳入list, string或array, 僅僅需傳入大小。返回index。??
  失敗時拋出的異常類型:IndexOutOfBoundsException


  5.checkPositionIndex(int index, int size):
  功能描寫敘述:檢查位置index是否為在一個長度為size的list。 string或array合法的范圍。 index的范圍區間是[0, size)(包括0不包括size)。無需直接傳入list。 string或array, 僅僅需傳入大小。返回index。


  失敗時拋出的異常類型:IndexOutOfBoundsException

  6.checkPositionIndexes(int start, int end, int size):
  功能描寫敘述:檢查[start, end)是一個長度為size的list。 string或array合法的范圍子集。

伴隨著錯誤信息。


  失敗時拋出的異常類型:IndexOutOfBoundsException

  一個比較實用實例:

復制代碼
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import com.google.common.base.Preconditions;public class PreconditionsTest {@Testpublic void Preconditions() throws Exception { getPersonByPrecondition(8,"peida");try {getPersonByPrecondition(-9,"peida");} catch (Exception e) {System.out.println(e.getMessage());}try {getPersonByPrecondition(8,"");} catch (Exception e) {System.out.println(e.getMessage());}try {getPersonByPrecondition(8,null);} catch (Exception e) {System.out.println(e.getMessage());}List<Integer> intList=new ArrayList<Integer> ();for(int i=0;i<10;i++){            try {checkState(intList,9);intList.add(i);} catch (Exception e) {System.out.println(e.getMessage());}}try {checkPositionIndex(intList,3);    } catch (Exception e) {System.out.println(e.getMessage());}try {checkPositionIndex(intList,13);    } catch (Exception e) {System.out.println(e.getMessage());}try {checkPositionIndexes(intList,3,7);} catch (Exception e) {System.out.println(e.getMessage());}try {checkPositionIndexes(intList,3,17);} catch (Exception e) {System.out.println(e.getMessage());}try {checkPositionIndexes(intList,13,17);} catch (Exception e) {System.out.println(e.getMessage());}try {checkElementIndex(intList,6);} catch (Exception e) {System.out.println(e.getMessage());}try {checkElementIndex(intList,16);} catch (Exception e) {System.out.println(e.getMessage());}}public static void getPersonByPrecondition(int age,String neme)throws Exception{Preconditions.checkNotNull(neme, "neme為null");Preconditions.checkArgument(neme.length()>0, "neme為\'\'");Preconditions.checkArgument(age>0, "age 必須大于0");System.out.println("a person age:"+age+",neme:"+neme);}public static void checkState(List<Integer> intList,int index)throws Exception{//表達式為true不拋異常Preconditions.checkState(intList.size()<index, " intList size 不能大于"+index);}public static void checkPositionIndex(List<Integer> intList,int index) throws Exception{Preconditions.checkPositionIndex(index, intList.size(), "index "+index+" 不在 list中。 List size為:"+intList.size());}public static void checkPositionIndexes(List<Integer> intList,int start,int end) throws Exception{Preconditions.checkPositionIndexes(start, end, intList.size());}public static void checkElementIndex(List<Integer> intList,int index) throws Exception{Preconditions.checkElementIndex(index, intList.size(),"index 為 "+index+" 不在 list中, List size為: "+intList.size());}
}
復制代碼

  輸出結果:

復制代碼
a person age:8,neme:peida
age 必須大于0
neme為''
neme為nullintList size 不能大于9
index 13 不在 list中, List size為:9 (13) must not be greater than size (9)
end index (17) must not be greater than size (9)
start index (13) must not be greater than size (9)
index 為 16 不在 list中, List size為: 9 (16) must be less than size (9)
復制代碼

?

  Guava的preconditions有這樣幾個長處:

  在靜態導入后, 方法非常明白無歧義, checkNotNull能夠清楚地告訴你它是干什么的, 它會拋出如何的異常.
  checkNotNull在驗證通過后直接返回, 能夠這樣方便地寫代碼: this.field = checkNotNull(field).
????? 簡單而又強大的可變參數'printf'風格的自己定義錯誤信息.

參考文章:http://www.cnblogs.com/peida/archive/2013/06/17/Guava_Preconditions.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390588.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390588.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390588.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

HTML DOM方法

querySelector() (querySelector()) The Document method querySelector() returns the first element within the document that matches the specified selector, or group of selectors. If no matches are found, null is returned.Document方法querySelector()返回文檔中與…

leetcode 773. 滑動謎題

題目 在一個 2 x 3 的板上&#xff08;board&#xff09;有 5 塊磚瓦&#xff0c;用數字 1~5 來表示, 以及一塊空缺用 0 來表示. 一次移動定義為選擇 0 與一個相鄰的數字&#xff08;上下左右&#xff09;進行交換. 最終當板 board 的結果是 [[1,2,3],[4,5,0]] 謎板被解開。…

數據科學領域有哪些技術_領域知識在數據科學中到底有多重要?

數據科學領域有哪些技術Jeremie Harris: “In a way, it’s almost like a data scientist or a data analyst has to be like a private investigator more than just a technical person.”杰里米哈里斯(Jeremie Harris) &#xff1a;“ 從某種意義上說&#xff0c;這就像是數…

python 算術運算

1. 算術運算符與優先級 # -*- coding:utf-8 -*-# 運算符含有,-,*,/,**,//,% # ** 表示^ , 也就是次方 a 2 ** 4 print 2 ** 4 , aa 16 / 5 print 16 / 5 , aa 16.0 / 5 print 16.0 / 5 , a# 結果再進行一次floor a 16.0 // 5.0 print 16.0 // 5.0 , aa 16 // 5 print …

c語言編程時碰到取整去不了_碰到編程墻時如何解開

c語言編程時碰到取整去不了Getting stuck is part of being a programmer, no matter the level. The so-called “easy” problem is actually pretty hard. You’re not exactly sure how to move forward. What you thought would work doesn’t.無論身在何處&#xff0c;陷…

初創公司怎么做銷售數據分析_為什么您的初創企業需要數據科學來解決這一危機...

初創公司怎么做銷售數據分析The spread of coronavirus is delivering a massive blow to the global economy. The lockdown and work from home restrictions have forced thousands of startups to halt expansion plans, cancel services, and announce layoffs.冠狀病毒的…

leetcode 909. 蛇梯棋

題目 N x N 的棋盤 board 上&#xff0c;按從 1 到 N*N 的數字給方格編號&#xff0c;編號 從左下角開始&#xff0c;每一行交替方向。 例如&#xff0c;一塊 6 x 6 大小的棋盤&#xff0c;編號如下&#xff1a; r 行 c 列的棋盤&#xff0c;按前述方法編號&#xff0c;棋盤格…

Python基礎之window常見操作

一、window的常見操作&#xff1a; cd c:\ #進入C盤d: #從C盤切換到D盤 cd python #進入目錄cd .. #往上走一層目錄dir #查看目錄文件列表cd ../.. #往上上走一層目錄 二、常見的文件后綴名&#xff1a; .txt 記事本文本文件.doc word文件.xls excel文件.ppt PPT文件.exe 可執行…

WPF效果(GIS三維篇)

二維的GIS已經被我玩爛了&#xff0c;緊接著就是三維了&#xff0c;哈哈&#xff01;先來看看最簡單的效果&#xff1a; 轉載于:https://www.cnblogs.com/OhMonkey/p/8954626.html

css注釋_CSS注釋示例–如何注釋CSS

css注釋Comments are used in CSS to explain a block of code or to make temporary changes during development. The commented code doesn’t execute.CSS中使用注釋來解釋代碼塊或在開發過程中進行臨時更改。 注釋的代碼不執行。 Both single and multi-line comments in…

r軟件時間序列分析論文_高度比較的時間序列分析-一篇論文評論

r軟件時間序列分析論文數據科學 &#xff0c; 機器學習 (Data Science, Machine Learning) In machine learning with time series, using features extracted from series is more powerful than simply treating a time series in a tabular form, with each date/timestamp …

leetcode 168. Excel表列名稱

題目 給你一個整數 columnNumber &#xff0c;返回它在 Excel 表中相對應的列名稱。 例如&#xff1a; A -> 1 B -> 2 C -> 3 … Z -> 26 AA -> 27 AB -> 28 … 示例 1&#xff1a; 輸入&#xff1a;columnNumber 1 輸出&#xff1a;“A” 示例 2&…

飛機訂票系統

1 #include <stdio.h>2 #include <stdlib.h>3 #include <string.h>4 #include <conio.h>5 typedef struct flightnode{6 char flight_num[10]; //航班號7 char start_time[10]; //起飛時間8 char end_time[10]; //抵達時間9 char st…

解決Mac10.13 Pod報錯 -bash: /usr/local/bin/pod: /System/Library/Frameworks/Ruby.fram

升級10.13以后Pod命令失效&#xff0c;解決辦法如下&#xff1a; 終端執行 brew link --overwrite cocoapods 復制代碼嘗試 Pod 命令是否已經恢復 若報錯繼續執行 brew reinstall cocoapodsbrew install rubybrew link --overwrite cocoapods 復制代碼嘗試 Pod 命令是否已經恢復…

angular示例_用示例解釋Angular動畫

angular示例為什么要使用動畫&#xff1f; (Why use Animations?) Modern web components frequently use animations. Cascading Style-sheets (CSS) arms developers with the tools to create impressive animations. Property transitions, uniquely named animations, mu…

selenium抓取_使用Selenium的網絡抓取電子商務網站

selenium抓取In this article we will go through a web scraping process of an E-Commerce website. I have designed this particular post to be beginner friendly. So, if you have no prior knowledge about web scraping or Selenium you can still follow along.在本文…

劍指 Offer 37. 序列化二叉樹

題目 序列化是將一個數據結構或者對象轉換為連續的比特位的操作&#xff0c;進而可以將轉換后的數據存儲在一個文件或者內存中&#xff0c;同時也可以通過網絡傳輸到另一個計算機環境&#xff0c;采取相反方式重構得到原數據。 請設計一個算法來實現二叉樹的序列化與反序列化…

ie8 ajaxSubmit 上傳文件提示下載

轉載 解決ie下ajaxsubmit上傳文件提示下載文件問題 主要是應為放回類型為json&#xff0c;返回text/html轉載于:https://www.cnblogs.com/yang-C-J/p/8963278.html

一個簡單的 js 時間對象創建

JS中獲取時間很常見&#xff0c;湊湊熱鬧&#xff0c;也獲取一個時間對象試試 首先&#xff0c;先了解js的獲取時間函數如下&#xff1a; var myDate new Date(); //創建一個時間對象 myDate.getYear(); // 獲取當前年份&#xff08;2位&#x…

裁判打分_內在的裁判偏見

裁判打分News flash: being an umpire is hard. Their job is to judge whether a ball that’s capable of moving upwards of 100 MPH or breaking 25 inches crossed through an imaginary zone before being caught. I don’t think many would argue that they have it ea…