Google-Collections

GTUG - Using the Google Collections Library for Java (1 of 2)

Google Collections Library
Site: http://code.google.com/p/google-collections/

說起該類庫的適用性,Google 有它在實際的生產部署記錄,並有著超過25,000個詳盡測試的單元測試套件。

儘管 Google Collections 看起來非常有前景,但他們也有替代的選擇。JDK的每一次迭代都從新的集合改善有所收穫。Apache commons collections 專案也有一些非常有趣的集合實作,而且比該 Google 類庫出現早很多年,儘管你發現他們並不是泛型友好的

往前看,Kevin Bourrillion 說過該專案的是為了讓該類庫正式化後並提交給 JCP。





概述


GTUG - Using the Google Collections Library for Java (2 of 2)

Google Collections 套件庫提供了新的工具實作方法和一組關注共時同作(Concurrent)的類別庫,其中包括
  • 不可變集合的實作。「不可變」 保證了系統中沒有其他的因素能改變集合的初始狀態,即使是該實作本身。
  • 而「不可修改」(就像JDK的 java.util.Collections 類別的不可修改的工廠方法)只是保證集合的客戶端/用戶永遠不能改變集合。
  • 通常,這些實現不是JDK封裝器,而是通過適度地調節,更多 地考慮RAM來重新實作介面。
  • Google Collections 庫提供了很多靜態工廠方法和程序來創建對象。有時候這些是顛覆冗餘類型聲明的簡單工具方法。
Ref, Google Collection 1.0增強了對Java集合框架的支持, InfoQ


The Google Collections Library 1.0 是一個新的集合型別的套件, Google 帶給您優秀且實作於 Java 5 或者更高版本. 它是自然延搌於您熟悉且愛好的 Java Collections Framework.

The Google Collections Library 1.0 is a set of new collection types, implementations and related goodness for Java 5 and higher, brought to you by Google. It is a natural extension of the Java Collections Framework.



Download Google Collections Library 1.0-final now

What's in here?

  • New Collection types: Multimap, Multiset, BiMap and others
  • High-performance immutable implementations of the standard collection types, for example ImmutableSet
  • MapMaker, a builder for concurrent hash maps with many advanced features
  • Ordering, which can only be described as a "Comparator on steroids"
  • Iterators and Iterables utility classes: element-based equality, cycle, concat, partition, filter with predicate, transform with function, and much more
  • Lists, Sets and Maps utility classes: a plethora of convenient factory methods and much more
  • Forwarding collections, such as ForwardingSet, allowing you to customize collection behavior without subclassing
  • Implementation helpers like AbstractIterator
  • and still more


Maven


<dependency>
<groupId>com.google.collections</groupId>
<artifactId>google-collections</artifactId>
<version>1.0</version>
</dependency>



Multi*Collections


該類庫也提供了一些唯一的實現方法,包括MultiMap和MultiSet 集合(它們描述了支持查詢對象在集合中的基數或頻率的集合)。這樣,為了保存同一個鍵關聯的多個值,你可能需要用到如下方法:

Multimap<String, Integer> personAndFavoriteNumbers = ArrayListMultimap.create();
personAndFavoriteNumbers.put("josh", 42);
personAndFavoriteNumbers.put("josh", 7);
Collection<Integer>numbers = personAndFavoriteNumbers.get("josh"); // doesn't return Integer
System.out.println(numbers .size()) ; // == 2

注意:get(String) 返回一個鍵相對應的視圖。如果一個鍵在map裡沒有對應的值,就會返回一個空的集合。如果向該視圖集合加入項目,他們就會反映到Multimap:

Collection<Integer>numbers = personAndFavoriteNumbers.get("josh");
System.out.println(numbers .size()) ; // == 2
numbers.add( 0) ;
System.out.println(numbers .size()) ; // == 3
System.out.println(personAndFavoriteNumbers.get("josh").size() ) ; // == 3


http://www.blogjava.net/jiangshachina/archive/2010/01/08/308638.html


Concurrent*


對於各種自定義介面有很多可用的並發實現方法:

ConcurrentHashMultiset<String> concurrentHashMultiset =
ConcurrentHashMultiset.create(Arrays.asList("a,b,c,d,e,f".split(",")));

Google Collections庫通過com.google.common.base.Predicate類提供了和各種編程語言協作的友善標籤。

import com.google.common.collect.*;

Iterable<Integer> filteredSet = Iterables.filter( someIterable, new Predicate<Integer>(){
public boolean apply( Integer integer) {
return integer > 0 ;
}
}) ;



Resources