1. 程式人生 > >(guava)的API快速熟悉使用

(guava)的API快速熟悉使用

1,大綱

讓我們來熟悉瓜娃,並體驗下它的一些API,分成如下幾個部分:

  • Introduction
  • Guava Collection API
  • Guava Basic Utilities
  • IO API
  • Cache API

2,為神馬選擇瓜娃?

  • 瓜娃是java API蛋糕上的冰激凌(精華)
  • 高效設計良好的API.
  • 被google的開發者設計,實現和使用。
  • 遵循高效的java這本書的好的語法實踐。
  • 使程式碼更刻度,簡潔,簡單。
  • 使用java 1.5的特性,
  • 流行的API,動態的開發
  • 它提供了大量相關的應用類,集合,多執行緒,比較,字串,輸入輸出,快取,網路,原生型別,數學,反射等等
  • 百分百的單元測試,被很多的專案使用,幫助開發者專注業務邏輯而不是寫java應用類
  • 節省時間,資源,提高生產力
  • 我的目的是為基本的java特徵提供開原始碼的支援,而不是自己再寫一個
  • Apache Common庫-Apache是一個很好的成熟的庫,但是不支援泛型,Apache對早起的java版本很有用,(1.5之前的)
  • java7,java8 最新的java支援一些guava的API

guava最新的正式版本是14.0-rc2,這個版本需要java1.6支援.

最新的maven座標是:

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

3,集合API的使用

  3.1簡化工作

可以簡化集合的建立和初始化;

類別 原來的寫法 guava的寫法
集合建立

Map<String, Map<String, String>> map = new HashMap<String, Map<String,String>>();

List<List<Map<String, String>>> list = new ArrayList<List<Map<String,String>>>();

Map<String, Map<String, String>> map = Maps.newHashMap();

List<List<Map<String, String>>> list = Lists.newArrayList();

//1,簡化集合的建立
List<Person> personList= Lists.newLinkedList();
Set<Person> personSet= Sets.newHashSet();
Map<String,Person> personMap= Maps.newHashMap();
Integer[] intArrays= ObjectArrays.newArray(Integer.class,10);

集合初始化  

Set<String> set = new HashSet<String>();

set.add("one");

set.add("two");

set.add("three");

 

Set<String> set = Sets.newHashSet("one","two","three");

List<String> list = Lists.newArrayList("one","two","three");

Map<String, String> map = ImmutableMap.of("ON","TRUE","OFF","FALSE");

//2,簡化集合的初始化
List<Person> personList2= Lists.newArrayList(new Person(1, 1, "a", "46546", 1, 20),

new Person(2, 1, "a", "46546", 1, 20));
Set<Person> personSet2= Sets.newHashSet(new Person(1,1,"a","46546",1,20),

new Person(2,1,"a","46546",1,20));
Map<String,Person> personMap2= ImmutableMap.of("hello",new Person(1,1,"a","46546",1,20),"fuck",new Person(2,1,"a","46546",1,20));

3.2 不變性

很大一部分是google集合提供了不變性,不變對比可變:

  •  資料不可改變
  • 執行緒安全
  • 不需要同步邏輯
  •  可以被自由的共享
  • 容易設計和實現
  •  記憶體和時間高效

不變對比不可修改

google的不變被確保真正不可改變,而不可修改實際上還是可以修改資料;如下所示:

Set<Integer> data = new HashSet<Integer>();

data.addAll(Arrays.asList(10, 20, 30, 40, 50, 60, 70, 80));

Set<Integer> fixedData = Collections.unmodifiableSet(data); // fixedData - [50, 70, 80, 20, 40, 10, 60, 30]

data.add(90); // fixedData - [50, 70, 80, 20, 40, 10, 90, 60, 30]

如何建立不可變的集合:

ImmutableSet<Integer> numbers = ImmutableSet.of(10, 20, 30, 40, 50);

使用copyOf方法

ImmutableSet<Integer> another = mmutableSet.copyOf(numbers);

使用Builder方法

ImmutableSet<Integer> numbers2 = ImmutableSet.<Integer>builder().addAll(numbers) .add(60) .add(70).add(80).build();

//3,建立不可變的集合

ImmutableList<Person> personImmutableList=
ImmutableList.of(new Person(1, 1, "a", "46546", 1, 20), new Person(2, 1, "a", "46546", 1, 20));

ImmutableSet<Person> personImmutableSet=ImmutableSet.copyOf(personSet2);

ImmutableMap<String,Person> personImmutableMap=ImmutableMap.<String,Person>builder()
.put("hell",new Person(1,1,"a","46546",1,20)).putAll(personMap2) .build();

3.3 新的集合型別

The Guava API provides very useful new collection types that work very nicely with existing java collections.

guava API 提供了有用的新的集合型別,協同已經存在的java集合工作的很好。

分別是 MultiMap, MultiSet, Table, BiMap, ClassToInstanceMap

種類 寫的例子
MultiMap

一種key可以重複的map,子類有ListMultimap和SetMultimap,對應的通過key分別得到list和set


Multimap<String, Person> customersByType =ArrayListMultimap.create();customersByType.put("abc", new Person(1, 1, "a", "46546", 1, 20));

customersByType.put("abc", new Person(1, 1, "a", "46546", 1, 30));
customersByType.put("abc", new Person(1, 1, "a", "46546", 1, 40));
customersByType.put("abc", new Person(1, 1, "a", "46546", 1, 50));
customersByType.put("abcd", new Person(1, 1, "a", "46546", 1, 50));
customersByType.put("abcde", new Person(1, 1, "a", "46546", 1, 50));

for(Person person:customersByType.get("abc"))
{
System.out.println(person.getAge());
}

MultiSet

不是集合,可以增加重複的元素,並且可以統計出重複元素的個數,例子如下:

private static void testMulitiSet() {
Multiset<Integer> multiSet = HashMultiset.create();
multiSet.add(10);
multiSet.add(30);
multiSet.add(30);
multiSet.add(40);

System.out.println( multiSet.count(30)); // 2
System.out.println( multiSet.size()); //4
}

Table

相當於有兩個key的map,不多解釋

private static void testTable() {
Table<Integer,Integer,Person> personTable=HashBasedTable.create();
personTable.put(1,20,new Person(1, 1, "a", "46546", 1, 20));
personTable.put(0,30,new Person(2, 1, "ab", "46546", 0, 30));
personTable.put(0,25,new Person(3, 1, "abc", "46546", 0, 25));
personTable.put(1,50,new Person(4, 1, "aef", "46546", 1, 50));
personTable.put(0,27,new Person(5, 1, "ade", "46546",0, 27));
personTable.put(1,29,new Person(6, 1, "acc", "46546", 1, 29));
personTable.put(0,33,new Person(7, 1, "add", "46546",0, 33));
personTable.put(1,66,new Person(8, 1, "afadsf", "46546", 1, 66));

//1,得到行集合
Map<Integer,Person> rowMap= personTable.row(0);
int maxAge= Collections.max(rowMap.keySet());

}

BiMap 是一個一一對映,可以通過key得到value,也可以通過value得到key; 

private static void testBitMap() {
//雙向map
BiMap<Integer,String> biMap=HashBiMap.create();

biMap.put(1,"hello");
biMap.put(2,"helloa");
biMap.put(3,"world");
biMap.put(4,"worldb");
biMap.put(5,"my");
biMap.put(6,"myc");

int value= biMap.inverse().get("my");
System.out.println("my --"+value);

}

ClassToInstanceMap   有的時候,你的map的key並不是一種型別,他們是很多型別,你想通過對映他們得到這種型別,guava提供了ClassToInstanceMap滿足了這個目的。 該類有一個簡單型別的引數,通常稱為B,代表了map控制的上層繫結,例如:
ClassToInstanceMap<Number> numberDefaults = MutableClassToInstanceMap.create();
numberDefaults.putInstance(Integer.class, Integer.valueOf(0));
從技術上來說,ClassToInstanceMap<B> 實現了Map<Class<? extends B>, B>,或者說,這是一個從B的子類到B物件的對映,這可能使得ClassToInstanceMap的泛型輕度混亂,但是隻要記住B總是Map的上層繫結型別,通常來說B只是一個物件。 重點:像其他的Map<Class,Object>,ClassToInstanceMap 含有的原生型別的專案,一個原生型別和他的相應的包裝類可以對映到不同的值;

private static void testClass() {
ClassToInstanceMap<Person> classToInstanceMap =MutableClassToInstanceMap.create();

Person person= new Person(1,20,"abc","46464",1,100);

classToInstanceMap.putInstance(Person.class,person);


// System.out.println("string:"+classToInstanceMap.getInstance(String.class));
// System.out.println("integer:" + classToInstanceMap.getInstance(Integer.class));

Person person1=classToInstanceMap.getInstance(Person.class);

}

3.4 謂詞和篩選

謂詞(Predicate)是用來篩選集合的;

謂詞是一個簡單的介面,只有一個方法返回布林值,但是他是一個很令人驚訝的集合方法,當你結合collections2.filter方法使用,這個篩選方法返回原來的集合中滿足這個謂詞介面的元素;

舉個例子來說:篩選出集合中的女人

public static void main(String[] args)
{
Optional<ImmutableMultiset<Person>> optional=Optional.fromNullable(testPredict());

if(optional.isPresent())
{
for(Person p:optional.get())
{
System.out.println("女人:"+p);
}
}

System.out.println(optional.isPresent());
}


public static ImmutableMultiset<Person> testPredict()
{
List<Person> personList=Lists.newArrayList(new Person(1, 1, "a", "46546", 1, 20),
new Person(2, 1, "ab", "46546", 0, 30),
new Person(3, 1, "abc", "46546", 0, 25),
new Person(4, 1, "aef", "46546", 1, 50),
new Person(5, 1, "ade", "46546",0, 27),
new Person(6, 1, "acc", "46546", 1, 29),
new Person(7, 1, "add", "46546",0, 33));

return ImmutableMultiset.copyOf(Collections2.filter(personList,new Predicate<Person>() {
@Override
public boolean apply( Person input) {
return input.getSex()==0;
}
}));
}

Predicates含有一些內建的篩選方法,比如說 in ,and ,not等,根據實際情況選擇使用。

3.5 功能和轉換

轉換一個集合為另外一個集合;

例項如下:

public static void main(String[] args)
{
Optional<ImmutableMultiset<String>> optional=Optional.fromNullable(testTransform());

if(optional.isPresent())
{
for(String p:optional.get())
{
System.out.println("名字:"+p);
}
}

System.out.println(optional.isPresent());
}


public static ImmutableMultiset<String> testTransform()
{
List<Person> personList=Lists.newArrayList(new Person(1, 1, "a", "46546", 1, 20),
new Person(2, 1, "ab", "46546", 0, 30),
new Person(3, 1, "abc", "46546", 0, 25),
new Person(4, 1, "aef", "46546", 1, 50),
new Person(5, 1, "ade", "46546",0, 27),
new Person(6, 1, "acc", "46546", 1, 29),
new Person(7, 1, "add", "46546",0, 33));

return ImmutableMultiset.copyOf(Lists.transform(personList,new Function<Person, String>() {
@Override
public String apply( Person input) {
return input.getName();
}
}));
}

3.6 排序

 是guava一份非常靈活的比較類,可以被用來操作,擴充套件,當作比較器,排序提供了集合排序的很多控制;

例項如下:

Lists.newArrayList(30, 20, 60, 80, 10);

Ordering.natural().sortedCopy(numbers); //10,20,30,60,80

Ordering.natural().reverse().sortedCopy(numbers); //80,60,30,20,10

Ordering.natural().min(numbers); //10

Ordering.natural().max(numbers); //80

Lists.newArrayList(30, 20, 60, 80, null, 10);

Ordering.natural().nullsLast().sortedCopy(numbers); //10, 20,30,60,80,null

Ordering.natural().nullsFirst().sortedCopy(numbers); //null,10,20,30,60,80

public static void testOrdering()
{
List<Person> personList=Lists.newArrayList(
new Person(3, 1, "abc", "46546", 0, 25),
new Person(2, 1, "ab", "46546", 0, 30),
new Person(5, 1, "ade", "46546",0, 27),
new Person(1, 1, "a", "46546", 1, 20),
new Person(6, 1, "acc", "46546", 1, 29),
new Person(4, 1, "aef", "46546", 1, 50),
new Person(7, 1, "add", "46546",0, 33)
);

Ordering<Person> byAge=new Ordering<Person>() {
@Override
public int compare( Person left, Person right) {
return right.getAge()-left.getAge();
}
};

for(Person p: byAge.immutableSortedCopy(personList))
{
System.out.println(p);
}
}

相關推薦

guavaAPI快速熟悉使用

1,大綱 讓我們來熟悉瓜娃,並體驗下它的一些API,分成如下幾個部分: IntroductionGuava Collection APIGuava Basic UtilitiesIO APICache API 2,為神馬選擇瓜娃? 瓜娃是java API蛋糕上的冰激凌(

瓜娃guavaAPI快速熟悉使用瓜娃是java API蛋糕上的冰激凌精華

1-使用 GOOGLE COLLECTIONS,GUAVA,STATIC IMPORTS 編寫漂亮程式碼 寫在前面: 以前在一個專案中用到過guava,當時匆匆用,也沒細研究,今天偶然在occhina看到這個系列教程的翻譯,感覺不錯,介紹得還比較全面,就一口氣全看完了,但看到第四節,發現還

Guava學習筆記(二):Google Guava 瓜娃API快速熟悉使用

1,大綱 讓我們來熟悉瓜娃,並體驗下它的一些API,分成如下幾個部分: IntroductionGuava Collection APIGuava Basic UtilitiesIO APICache API 2,為神馬選擇瓜娃? 瓜娃是java API蛋糕上的冰

thinkphp5.0學習筆記API後臺處理與命名空間

mac code 輸入 -1 pub 基礎 select() color 第一個 命名空間 先來看命名空間吧; 命名空間是學習TP的基礎, <?php namespace app\lian\c1; class yi{ public $obj = "這是第一個

Hibernate學習筆記1---hibernate快速上手與準備工作

成了 -- 開源 工作 快速 tar ref orm 磁盤 持久層介紹 持久化:將內存中的數據保存在磁盤等存儲設備中。 持久化對象:指已經存儲在數據庫護著磁盤的業務對象 經典的軟件應用體系結構(三層結構) 在三層結構中,由於業務邏輯除了負責業務邏輯以外,還要負責相關的數據

Kotlin基礎Kotlin快速入門

語法 note 初始 字母 文件中 create 列表 orange 快的 Kotlin快速入門 一、函數 1 /* 2 * 1.函數可以定義在文件最外層,不需要把它放在類中 3 * 2.可以省略結尾分號 4 * */ 5 fun main(args: Ar

Ocata Neutron代碼分析——api-paste.ini分析

class create ict 指定 method network version tex cal 在Neutron API啟動過程分析中,曾分析到加載wsgi app是通過load_paste_app函數首先實例化oslo_service.wsgi.py中的Loader

從0開始的微服務架構:如何快速體驗微服務架構?

常常 原來 人員 google tty 打包 第三方 江湖 ces 雖然已經紅了很久,但是“微服務架構”正變得越來越重要,也將繼續火下去。各個公司與技術人員都在分享微服務架構的相關知識與實踐經驗,但我們發現,目前網上的這些相關文章中,要麽上來就是很有借鑒意義的幹貨,要麽就是

*快速學習SSM框架,你需要一套學習曲線平滑的教程

lan 都是 spring 學習方式 能夠 分享圖片 個人能力 很多 data 作者:meepo鏈接:https://www.zhihu.com/question/57719761/answer/156952139來源:知乎著作權歸作者所有。商業轉載請聯系作者獲得授權,非商

Selenium WebDriverPythonAPI

加載 屬性 ... blog cep Go clas style exceptio 1、通過示例介紹Selenium-WebDriver 一個簡單的入門方法就是這個例子,它在Google上搜索術語“Cheese”,然後將結果頁面的標題輸出到控制臺。java csharp p

Golang入門教程beego 快速開發 HTTP 框架

應用 inf ado .com home clas lan mime iyu   beego 是一個快速開發 Go 應用的 HTTP 框架,他可以用來快速開發 API、Web 及後端服務等各種應用,是一個 RESTful 的框架,主要設計靈感來源於 tornado、sina

AppiumPythonAPI

port 觸摸屏 性能 時間 class 顯示 some emulator RR 1、創建新的會話desired_caps = desired_caps = { ‘platformName‘: ‘Android‘, ‘platformVersion‘: ‘7.0‘,

Redis入門到高可用—— API理解和使用

入門到 16px ron 節點 理解 高可用 怎麽 生產 sca 一、通用命令 查看所有key 127.0.0.1:6379> keys * keys命令一般不在生產環境使用! keys命令怎麽用? ①熱備從節點(從節點一般不在生產環境使用,可以在從節點上執

kafka3API使用

tostring 信息 bootstra 列表 con for edt rdb 對象實例 相關依賴 <!-- Kafka 依賴包 --> <dependency> <groupId>org.apache.kafka</grou

[Swift]八大排序算法快速排序

addition 每次 數據交換 uri 基本思想 技術分享 繼續 splay 休眠 排序分為內部排序和外部排序。 內部排序:是指待排序列完全存放在內存中所進行的排序過程,適合不太大的元素序列。 外部排序:指的是大文件的排序,即待排序的記錄存儲在外存儲器上,待排序的文件

離散傅立葉變換DFT快速傅立葉變換FFT原理與實現

目錄 1、影象變換 2、離散傅立葉變換(Discrete Fourier Transform) 3、DFT性質 4、DFT與數字影象處理 5、FFT-快速傅立葉變換 6、DFT與FFT的演算法實現 1. 影象變換 — —數學領域中有很多種變換,如傅立葉變換、拉普拉斯變

安裝SALICONMSCOCOapi

Ubuntu16.04+Anaconda3的python2.7虛擬環境中安裝saliconapi: pip install cython pip install numpy pip install scipy pip install scikit-image pip inst

JAVA基礎31---API之String

String 類代表字串。Java 程式中的所有字串字面值(如 "abc" )都作為此類的例項實現。 字串是常量;它們的值在建立之後不能更改。字串緩衝區支援可變的字串。因為 String 物件是不可變的,所以可以共享。例如:   String str =

JAVA基礎30---API之Object

API Java語言中,已經提供了大量的類庫供程式開發者使用,這些類庫是Java語言提供的、已經寫好的、用來實現常見的和通用的功能的類的集合,我們稱之為“應用程式介面(API-Application Program Interface)” 這些API根據實現的功能不同,劃分為不同的集合,

api閘道器服務 zuul-路由

路由是微服務架構中必須的一部分,比如,“/” 可能對映到你的WEB程式上,”/api/users “可能對映到你的使用者服務上,“/api/shop”可能對映到你的商品服務商。(註解:我理解這裡的這幾個對映就是說通過Zuul這個閘道器把服務對映到不同的服務商去處理,從而變成了微服務!) 通過Zuu