国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Google 開源庫Guava詳解(集合工具類)—Maps、Multisets、Multimaps

這篇具有很好參考價值的文章主要介紹了Google 開源庫Guava詳解(集合工具類)—Maps、Multisets、Multimaps。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

一、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類提供了一些方便的實用程序。

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)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權(quán),不承擔相關(guān)法律責任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務器費用

相關(guān)文章

  • 【開源與項目實戰(zhàn):開源實戰(zhàn)】82 | 開源實戰(zhàn)三(中):剖析Google Guava中用到的幾種設(shè)計模式

    【開源與項目實戰(zhàn):開源實戰(zhàn)】82 | 開源實戰(zhàn)三(中):剖析Google Guava中用到的幾種設(shè)計模式

    上一節(jié)課,我們通過 Google Guava 這樣一個優(yōu)秀的開源類庫,講解了如何在業(yè)務開發(fā)中,發(fā)現(xiàn)跟業(yè)務無關(guān)、可以復用的通用功能模塊,并將它們從業(yè)務代碼中抽離出來,設(shè)計開發(fā)成獨立的類庫、框架或功能組件。 今天,我們再來學習一下,Google Guava 中用到的幾種經(jīng)典設(shè)計模式:

    2024年02月11日
    瀏覽(38)
  • 推薦Java開發(fā)常用的工具類庫google guava

    Guava Guava是一個Google開源的Java核心庫,它提供了許多實用的工具和輔助類,使Java開發(fā)更加簡潔、高效、可靠。目前和 hutool 一起,是業(yè)界常用的工具類庫。 shigen 也比較喜歡使用,在這里列舉一下常用的工具類庫和使用的案例。 參考: 整理一波Guava的使用技巧 - 掘金 Guava中這

    2024年02月09日
    瀏覽(23)
  • 【開源與項目實戰(zhàn):開源實戰(zhàn)】83 | 開源實戰(zhàn)三(下):借Google Guava學習三大編程范式中的函數(shù)式編程

    現(xiàn)在主流的編程范式主要有三種,面向過程、面向?qū)ο蠛秃瘮?shù)式編程。在理論部分,我們已經(jīng)詳細講過前兩種了。今天,我們再借機會講講剩下的一種,函數(shù)式編程。 函數(shù)式編程并非一個很新的東西,早在 50 多年前就已經(jīng)出現(xiàn)了。近幾年,函數(shù)式編程越來越被人關(guān)注,出現(xiàn)

    2024年02月11日
    瀏覽(19)
  • 設(shè)計模式學習筆記 - 開源實戰(zhàn)三(下):借助Google Guava學習三大編程范式中的函數(shù)式編程

    現(xiàn)在主流的編程范式主要有三種,面向過程、面向?qū)ο蠛秃瘮?shù)式編程。在理論部分,已經(jīng)介紹了前面兩種編程范式。本章再講講剩下的編程范式,函數(shù)式編程。 函數(shù)式編程并非是一個很新的東西,早在 50 年前就已經(jīng)出現(xiàn)。近幾年,函數(shù)式編程越來越被人關(guān)注,出現(xiàn)了很多新

    2024年04月22日
    瀏覽(24)
  • com.google.guava:guava 組件安全漏洞及健康分析

    com.google.guava:guava 組件安全漏洞及健康分析

    維護者 google組織 許可證類型 Apache-2.0 首次發(fā)布 2010 年 4 月 26 日 最新發(fā)布時間 2023 年 8 月 1 日 GitHub Star 48189 GitHub Fork 10716 依賴包 28,694 依賴存儲庫 219,576 Guava 是 Google 的一組核心 Java 庫,其中包括新的集合類型(例如 multimap 和 multiset)、不可變集合、圖形庫以及用于并發(fā)、

    2024年02月10日
    瀏覽(24)
  • Google的guava緩存學習使用

    導入依賴 使用1 項目中使用到了緩存,定義一個切面,攔截類或方法上存在@SysDataCache注解請求,對于這些方法的返回值進行緩存。項目中主要還是使用在緩存常量,一些不易改變的值 定義注解 定義切面和初始化緩存容器并使用緩存 項目中緩存使用 使用2 作為性能緩存工具,

    2024年01月25日
    瀏覽(22)
  • 【譯】Google Guava 的 Table 接口介紹

    【譯】Google Guava 的 Table 接口介紹

    原文:https://www.baeldung.com/guava-table 在本教程中,我們將展示如何使用 Google Guava 的 Table 接口及其多個實現(xiàn)。 Guava 的 Table 是一種集合,表示包含行、列和相關(guān)單元格值的表結(jié)構(gòu),行和列充當有序的鍵對。 讓我們看看如何使用 Table 類。 2.1. Maven依賴 首先,在 pom.xml 中添加 Goo

    2024年02月06日
    瀏覽(25)
  • github中Keyless Google Maps API在網(wǎng)頁中顯示地圖和標記 無需api key

    github中Keyless Google Maps API在網(wǎng)頁中顯示地圖和標記 無需api key

    使用Google Maps API在網(wǎng)頁中顯示地圖和標記的示例博客。以下是一個簡單的示例: C:pythoncodebloggoogle-map-markers-gh-pagesgoogle-map-markers-gh-pagesindex.html 在本篇博客中,我們將學習如何使用Google Maps API在網(wǎng)頁中顯示地圖,并在地圖上添加標記。Google Maps API提供了豐富的功能和靈活性

    2024年02月12日
    瀏覽(20)
  • 使用Guava輕松創(chuàng)建和管理不可變集合

    使用Guava輕松創(chuàng)建和管理不可變集合

    第1章:引言 大家好,我是小黑。今天,我們來聊聊一個在Java編程里超有用的話題:使用Guava創(chuàng)建和管理不可變集合。首先,咱們得明白,什么是不可變集合。簡單來說,不可變集合就是一旦創(chuàng)建就不能被修改的集合。 為啥要用不可變集合呢?想象一下,你寫了一段代碼,把

    2024年02月04日
    瀏覽(18)
  • Go 1.21新增的 maps 包詳解

    maps 包提供了幾個非常有用的用于操作 map 類型(任何類型的 map)的函數(shù),本文接下來詳細介紹下這幾個函數(shù)。 定義如下: 返回 m 的一個副本,因為新的鍵和值是使用賦值方式復制的,所以這是一個淺克隆。簡單示例如下: 定義如下: 復制 src 中的所有鍵值對并添加到 dst

    2024年02月11日
    瀏覽(24)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包