學習筆記(二) 瓜娃(guava)的API快速熟悉使用

來源:互聯網
上載者:User

標籤:doc   擴充   app   over   void   etag   ase   工作   hash   

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滿足了這個目的。 除了繼承自Map介面,ClassToInstaceMap提供了方法 T getInstance(Class<T>) 和 T putInstance(Class<T>, T),消除了強制類型轉換。 該類有一個簡單類型的參數,通常稱為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只是一個對象。guava提供了有用的實現, MutableClassToInstanceMap 和 ImmutableClassToInstanceMap.重點:像其他的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);
}
}

學習筆記(二) 瓜娃(guava)的API快速熟悉使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.