一、Maps
Maps有許多很酷的實用程序,值得單獨解釋。
1、uniqueIndex
Maps.uniqueIndex(Iterable,F(xiàn)unction)解決了一個常見的情況,即有一堆對象,每個對象都有一些唯一的屬性,并希望能夠根據(jù)該屬性查找這些對象。
假設(shè)我們有一堆字符串,我們知道它們有唯一的長度,我們希望能夠查找具有特定長度的字符串。
ImmutableMap<Integer, String> stringsByIndex = Maps.uniqueIndex(strings, new Function<String, Integer> () {
public Integer apply(String string) {
return string.length();
}
});
如果索引不是唯一的,請參閱下面的Multimaps.index。
2、difference
Maps.difference(Map,Map)允許您比較兩張地圖之間的所有差異。它返回一個MapDifference對象,該對象將Venn圖分解為:
Method |
Description |
entriesInCommon() |
The entries which are in both maps, with both matching keys and values. |
entriesDiffering() |
具有相同鍵但不同值的條目。此映射中的值屬于MapDifference.ValueDifference類型,可以查看左右值。 |
entriesOnlyOnLeft() |
Returns the entries whose keys are in the left but not in the right map. |
entriesOnlyOnRight() |
Returns the entries whose keys are in the right but not in the left map. |
Map<String, Integer> left = ImmutableMap.of("a", 1, "b", 2, "c", 3);
Map<String, Integer> right = ImmutableMap.of("b", 2, "c", 4, "d", 5);
MapDifference<String, Integer> diff = Maps.difference(left, right);
diff.entriesInCommon(); // {"b" => 2}
diff.entriesDiffering(); // {"c" => (3, 4)}
diff.entriesOnlyOnLeft(); // {"a" => 1}
diff.entriesOnlyOnRight(); // {"d" => 5}
3、BiMap?
BiMap上的Guava實用程序位于Maps類中,因為BiMap也是Map。
BiMap utility |
Corresponding Map utility |
synchronizedBiMap(BiMap) |
Collections.synchronizedMap(Map) |
unmodifiableBiMap(BiMap) |
Collections.unmodifiableMap(Map) |
4、Static Factories
映射提供以下靜態(tài)工廠方法。
Implementation |
Factories |
HashMap |
basic, from Map, with expected size |
LinkedHashMap |
basic, from Map |
TreeMap |
basic, from Comparator, from SortedMap |
EnumMap |
from Class, from Map |
ConcurrentMap |
basic |
IdentityHashMap |
basic |
二、Multisets
標準集合操作(如containsAll)忽略多集合中元素的計數(shù),只關(guān)心元素是否在多集合中。多集提供了許多考慮多集中元素多重性的操作。
Method |
Explanation |
Difference from Collection method |
containsOccurrences(Multiset sup, Multiset sub) |
Returns true if sub.count(o) <= super.count(o) for all o. |
Collection.containsAll忽略計數(shù),只測試是否包含元素。 |
removeOccurrences(Multiset removeFrom, Multiset toRemove) |
Removes one occurrence in removeFrom for each occurrence of an element in toRemove. |
Collection.removeAll將刪除在to Remove中出現(xiàn)一次的任何元素的所有實例。 |
retainOccurrences(Multiset removeFrom, Multiset toRetain) |
Guarantees that removeFrom.count(o) <= toRetain.count(o) for all o. |
Collection.retail將所有出現(xiàn)的元素保留為Retain。 |
intersection(Multiset, Multiset) |
返回兩個多集的交集的視圖;一種非破壞性的替代方案。 |
Has no analogue. |
Multiset<String> multiset1 = HashMultiset.create();
multiset1.add("a", 2);
Multiset<String> multiset2 = HashMultiset.create();
multiset2.add("a", 5);
multiset1.containsAll(multiset2); // returns true: all unique elements are contained,
// even though multiset1.count("a") == 2 < multiset2.count("a") == 5
Multisets.containsOccurrences(multiset1, multiset2); // returns false
Multisets.removeOccurrences(multiset2, multiset1); // multiset2 now contains 3 occurrences of "a"
multiset2.removeAll(multiset1); // removes all occurrences of "a" from multiset2, even though multiset1.count("a") == 2
multiset2.isEmpty(); // returns true
Multiset中的其他實用程序包括:
Method |
Description |
copyHighestCountFirst(Multiset) |
返回multiset的不可變副本,該副本按頻率降序迭代元素。 |
unmodifiableMultiset(Multiset) |
Returns an unmodifiable view of the multiset. |
unmodifiableSortedMultiset(SortedMultiset) |
Returns an unmodifiable view of the sorted multiset. |
Multiset<String> multiset = HashMultiset.create();
multiset.add("a", 3);
multiset.add("b", 5);
multiset.add("c", 1);
ImmutableMultiset<String> highestCountFirst = Multisets.copyHighestCountFirst(multiset);
// highestCountFirst, like its entrySet and elementSet, iterates over the elements in order {"b", "a", "c"}
三、Multimaps
Multimaps提供了許多通用的實用程序操作,值得單獨解釋。
1、index
Maps.uniqueIndex,Multimaps.index(Iterable,F(xiàn)unction)的表親回答了這樣一種情況,即當您希望能夠查找具有某些特定共同屬性的所有對象時,這些屬性不一定是唯一的。
假設(shè)我們希望根據(jù)字符串的長度對其進行分組。
ImmutableSet<String> digits = ImmutableSet.of(
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine");
Function<String, Integer> lengthFunction = new Function<String, Integer>() {
public Integer apply(String string) {
return string.length();
}
};
ImmutableListMultimap<Integer, String> digitsByLength = Multimaps.index(digits, lengthFunction);
/*
* digitsByLength maps:
* 3 => {"one", "two", "six"}
* 4 => {"zero", "four", "five", "nine"}
* 5 => {"three", "seven", "eight"}
*/
2、invertFrom
由于多映射可以將多個關(guān)鍵點映射到一個值,也可以將一個關(guān)鍵點映像到多個值,因此反轉(zhuǎn)多映射非常有用。Guava提供inverteFrom(Multimap to Invert,Multimap dest),讓您無需選擇實現(xiàn)即可完成此操作。
注意:如果您使用的是ImmutableMultimap,請考慮使用ImmutableMultimap.inverse()。
ArrayListMultimap<String, Integer> multimap = ArrayListMultimap.create();
multimap.putAll("b", Ints.asList(2, 4, 6));
multimap.putAll("a", Ints.asList(4, 2, 1));
multimap.putAll("c", Ints.asList(2, 5, 3));
TreeMultimap<Integer, String> inverse = Multimaps.invertFrom(multimap, TreeMultimap.<Integer, String>create());
// note that we choose the implementation, so if we use a TreeMultimap, we get results in order
/*
* inverse maps:
* 1 => {"a"}
* 2 => {"a", "b", "c"}
* 3 => {"c"}
* 4 => {"a", "b"}
* 5 => {"c"}
* 6 => {"b"}
*/
3、forMap
需要在地圖上使用Multimap方法嗎?forMap(Map)將Map視為SetMultimap。例如,與Multimaps.inverteFrom組合使用時,這一功能特別有用。
Map<String, Integer> map = ImmutableMap.of("a", 1, "b", 1, "c", 2);
SetMultimap<String, Integer> multimap = Multimaps.forMap(map);
// multimap maps ["a" => {1}, "b" => {1}, "c" => {2}]
Multimap<Integer, String> inverse = Multimaps.invertFrom(multimap, HashMultimap.<Integer, String> create());
// inverse maps [1 => {"a", "b"}, 2 => {"c"}]
四、Tables
Tables類提供了一些方便的實用程序。文章來源:http://www.zghlxwxcb.cn/news/detail-778571.html
1、customTable
與Multimaps.newXXXMultimap(Map,Supplier)實用程序類似,Tables.newCustomTable(Map,供應商<Map>)允許您使用任何喜歡的行或列映射指定表實現(xiàn)。
文章來源地址http://www.zghlxwxcb.cn/news/detail-778571.html
// use LinkedHashMaps instead of HashMaps
Table<String, Character, Integer> table = Tables.newCustomTable(
Maps.<String, Map<Character, Integer>>newLinkedHashMap(),
new Supplier<Map<Character, Integer>> () {
public Map<Character, Integer> get() {
return Maps.newLinkedHashMap();
}
});
到了這里,關(guān)于Google 開源庫Guava詳解(集合工具類)—Maps、Multisets、Multimaps的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!