用帝國軟件做網(wǎng)站的心得uc瀏覽器網(wǎng)頁版入口
任何具有JDK Collections Framework經(jīng)驗的程序員都知道并喜歡java.util.Collections.Guava提供了更多的實用程序:適用于所有集合的靜態(tài)方法。這些是番石榴最受歡迎和成熟的部分。
對應(yīng)于特定接口的方法以相對直觀的方式分組:
nterface | JDK or Guava? | Corresponding Guava utility class |
Collection | JDK | Collections2 |
List | JDK | Lists |
Set | JDK | Sets |
SortedSet | JDK | Sets |
Map | JDK | Maps |
SortedMap | JDK | Maps |
Queue | JDK | Queues |
Multiset | Guava | Multisets |
Multimap | Guava | Multimaps |
BiMap | Guava | Maps |
Table | Guava | Tables |
一、靜態(tài)構(gòu)造函數(shù)
在JDK 7之前,構(gòu)建新的泛型集合需要令人不愉快的代碼復(fù)制:
List<TypeThatsTooLongForItsOwnGood> list = new ArrayList<TypeThatsTooLongForItsOwnGood>();
我想我們都可以同意,這是令人不愉快的。Guava提供了使用泛型來推斷右側(cè)類型的靜態(tài)方法:
List<TypeThatsTooLongForItsOwnGood> list = Lists.newArrayList();
Map<KeyType, LongishValueType> map = Maps.newLinkedHashMap();
可以肯定的是,JDK 7中的菱形操作符減少了這方面的麻煩:
List<TypeThatsTooLongForItsOwnGood> list = new ArrayList<>();
但Guava走得更遠(yuǎn)。使用工廠方法模式,我們可以非常方便地用集合的起始元素來初始化集合。
Set<Type> copySet = Sets.newHashSet(elements);
List<String> theseElements = Lists.newArrayList("alpha", "beta", "gamma");
此外,通過命名工廠方法(有效Java項1),我們可以提高將集合初始化為大小的可讀性:
List<Type> exactly100 = Lists.newArrayListWithCapacity(100);
List<Type> approx100 = Lists.newArrayListWithExpectedSize(100);
Set<Type> approx100Set = Sets.newHashSetWithExpectedSize(100);
下面列出了提供的精確靜態(tài)工廠方法及其相應(yīng)的實用程序類。
注意:Guava引入的新集合類型不公開原始構(gòu)造函數(shù),或者在實用程序類中具有初始值設(shè)定項。相反,它們直接公開靜態(tài)工廠方法,例如:
Multiset<String> multiset = HashMultiset.create();
二、Iterables
只要可能,Guava更喜歡提供接受Iterable而不是Collection的實用程序。在谷歌,遇到一個“集合”并不罕見,它實際上沒有存儲在主存中,而是從數(shù)據(jù)庫或另一個數(shù)據(jù)中心收集的,并且在不實際獲取所有元素的情況下無法支持size()等操作。
因此,您可能希望看到的所有集合都支持的許多操作都可以在Iterables中找到。此外,大多數(shù)Iterables方法在iterator中都有一個接受原始迭代器的相應(yīng)版本。
Iterables類中的絕大多數(shù)操作都是懶惰的:它們只在絕對必要的時候推進(jìn)支持迭代。返回Iterables的方法返回延遲計算的視圖,而不是在內(nèi)存中顯式地構(gòu)造集合。
從Guava 12開始,Iterables由FluentInterable類補(bǔ)充,該類封裝了Iterable,并為其中許多操作提供了“流暢”的語法。
以下是最常用的實用程序的選擇,盡管在Guava函數(shù)習(xí)語中討論了Iterables中許多更“函數(shù)”的方法。
1、一般使用
Method | Description | See Also |
concat(Iterable<Iterable>) | 返回幾個可迭代項的串聯(lián)的惰性視圖。 | concat(Iterable...) |
frequency(Iterable, Object) | 返回對象的出現(xiàn)次數(shù)。 | Compare Collections.frequency(Collection, Object); see Multiset |
partition(Iterable, int) | 返回一個不可修改的可迭代視圖,該視圖被劃分為指定大小的塊。 | Lists.partition(List, int), paddedPartition(Iterable, int) |
getFirst(Iterable, T default) | 返回可迭代項的第一個元素,如果為空,則返回默認(rèn)值。 | Compare Iterable.iterator().next(), FluentIterable.first() |
getLast(Iterable) | 返回可迭代項的最后一個元素,如果它為空,則會以NoSuchElementException快速失敗。 | getLast(Iterable, T default), FluentIterable.last() |
elementsEqual(Iterable, Iterable) | 如果可迭代項具有相同順序的相同元素,則返回true。 | Compare List.equals(Object) |
unmodifiableIterable(Iterable) | 返回可迭代項的不可修改視圖。 | Compare Collections.unmodifiableCollection(Collection) |
limit(Iterable, int) | 返回一個Iterable,最多返回指定數(shù)量的元素。 | FluentIterable.limit(int) |
getOnlyElement(Iterable) | 返回Iterable中唯一的元素。如果可迭代項為空或具有多個元素,則快速失敗。 | getOnlyElement(Iterable, T default) |
Iterable<Integer> concatenated = Iterables.concat(Ints.asList(1, 2, 3),Ints.asList(4, 5, 6));
// concatenated has elements 1, 2, 3, 4, 5, 6String lastAdded = Iterables.getLast(myLinkedHashSet);String theElement = Iterables.getOnlyElement(thisSetIsDefinitelyASingleton);// if this set isn't a singleton, something is wrong!