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

【譯】Google Guava 的 Table 接口介紹

這篇具有很好參考價值的文章主要介紹了【譯】Google Guava 的 Table 接口介紹。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

原文:https://www.baeldung.com/guava-table

1. 概述

在本教程中,我們將展示如何使用 Google Guava 的 Table 接口及其多個實現(xiàn)。
Guava 的 Table 是一種集合,表示包含行、列和相關(guān)單元格值的表結(jié)構(gòu),行和列充當(dāng)有序的鍵對。

2. Google Guava的 Table

讓我們看看如何使用 Table 類。

2.1. Maven依賴

首先,在 pom.xml 中添加 Google Guava 庫的依賴項:

<dependency>
  <groupId>com.google.guava</groupId>
  <artifactId>guava</artifactId>
  <version>31.0.1-jre</version>
</dependency>

最新版本的依賴項可以在這里查看。

2.2. 核心庫中的等價表示

如果我們用 Java 核心庫中的 Collections 來表示 Guava 的 Table,那么結(jié)構(gòu)將是一個行的映射,其中每一行包含一個列的映射以及相關(guān)的單元格值。
Table 表示一種特殊的映射,其中可以以組合的方式指定兩個鍵來引用單個值。
這類似于創(chuàng)建一個映射的映射,例如 Map<UniversityName, Map<CoursesOffered, SeatAvailable>>。Table 也是表示戰(zhàn)艦游戲棋盤的完美方式。

3. 創(chuàng)建

您可以以多種方式創(chuàng)建 Table的實例:

  • 使用類 HashBasedTable中的 create方法,它在內(nèi)部使用 LinkedHashMap
Table<String, String, Integer> universityCourseSeatTable 
    = HashBasedTable.create();
  • 如果我們需要一個行鍵和列鍵按照它們的自然排序或提供比較器進(jìn)行排序的 Table,可以使用名為TreeBasedTable 的類中的 create 方法創(chuàng)建 Table 的實例,它在內(nèi)部使用TreeMap
Table<String, String, Integer> universityCourseSeatTable
    = TreeBasedTable.create();
  • 如果我們事先知道行鍵和列鍵,并且表的大小是固定的,請使用類ArrayTable中的create方法:
List<String> universityRowTable 
    = Lists.newArrayList("Mumbai", "Harvard");
List<String> courseColumnTables 
    = Lists.newArrayList("Chemical", "IT", "Electrical");
Table<String, String, Integer> universityCourseSeatTable
    = ArrayTable.create(universityRowTable, courseColumnTables);
  • 如果我們打算創(chuàng)建 Table的不可變實例,其內(nèi)部數(shù)據(jù)永遠(yuǎn)不會改變,請使用 ImmutableTable類(創(chuàng)建方式采用構(gòu)建器模式):
Table<String, String, Integer> universityCourseSeatTable
    = ImmutableTable.<String, String, Integer> builder()
    .put("Mumbai", "Chemical", 120).build();

4. 使用

讓我們從一個簡單的示例開始,展示如何使用 Table。

4.1. 檢索

如果我們知道行鍵和列鍵,那么我們可以獲取與行鍵和列鍵關(guān)聯(lián)的值:

@Test
    public void givenTable_whenGet_returnsSuccessfully() {
        Table<String, String, Integer> universityCourseSeatTable 
            = HashBasedTable.create();
        universityCourseSeatTable.put("Mumbai", "Chemical", 120);
        universityCourseSeatTable.put("Mumbai", "IT", 60);
        universityCourseSeatTable.put("Harvard", "Electrical", 60);
        universityCourseSeatTable.put("Harvard", "IT", 120);

        int seatCount 
            = universityCourseSeatTable.get("Mumbai", "IT");
        Integer seatCountForNoEntry 
            = universityCourseSeatTable.get("Oxford", "IT");

        assertThat(seatCount).isEqualTo(60);
        assertThat(seatCountForNoEntry).isEqualTo(null);
    }

4.2. 檢查元素是否存在

我們可以根據(jù)以下方式檢查Table中的元素是否存在:

  • 行鍵
  • 列鍵
  • 行鍵和列鍵都存在

讓我們看看如何檢查元素是否存在:

@Test
    public void givenTable_whenContains_returnsSuccessfully() {
        Table<String, String, Integer> universityCourseSeatTable 
            = HashBasedTable.create();
        universityCourseSeatTable.put("Mumbai", "Chemical", 120);
        universityCourseSeatTable.put("Mumbai", "IT", 60);
        universityCourseSeatTable.put("Harvard", "Electrical", 60);
        universityCourseSeatTable.put("Harvard", "IT", 120);

        boolean entryIsPresent
            = universityCourseSeatTable.contains("Mumbai", "IT");
        boolean courseIsPresent 
            = universityCourseSeatTable.containsColumn("IT");
        boolean universityIsPresent 
            = universityCourseSeatTable.containsRow("Mumbai");
        boolean seatCountIsPresent 
            = universityCourseSeatTable.containsValue(60);

        assertThat(entryIsPresent).isEqualTo(true);
        assertThat(courseIsPresent).isEqualTo(true);
        assertThat(universityIsPresent).isEqualTo(true);
        assertThat(seatCountIsPresent).isEqualTo(true);
    }

4.3. 刪除

我們可以通過提供行鍵和列鍵來從Table中刪除元素:

@Test
    public void givenTable_whenRemove_returnsSuccessfully() {
        Table<String, String, Integer> universityCourseSeatTable
            = HashBasedTable.create();
        universityCourseSeatTable.put("Mumbai", "Chemical", 120);
        universityCourseSeatTable.put("Mumbai", "IT", 60);

        int seatCount 
            = universityCourseSeatTable.remove("Mumbai", "IT");

        assertThat(seatCount).isEqualTo(60);
        assertThat(universityCourseSeatTable.remove("Mumbai", "IT")).
            isEqualTo(null);
    }

4.4. 行鍵到單元格值的映射

通過提供列鍵,就可以獲取以行鍵為鍵、單元格值的 Map 映射。

@Test
    public void givenTable_whenColumn_returnsSuccessfully() {
        Table<String, String, Integer> universityCourseSeatTable 
            = HashBasedTable.create();
        universityCourseSeatTable.put("Mumbai", "Chemical", 120);
        universityCourseSeatTable.put("Mumbai", "IT", 60);
        universityCourseSeatTable.put("Harvard", "Electrical", 60);
        universityCourseSeatTable.put("Harvard", "IT", 120);

        Map<String, Integer> universitySeatMap 
            = universityCourseSeatTable.column("IT");

        assertThat(universitySeatMap).hasSize(2);
        assertThat(universitySeatMap.get("Mumbai")).isEqualTo(60);
        assertThat(universitySeatMap.get("Harvard")).isEqualTo(120);
    }

4.5. 表的_Map_表示

我們可以使用 columnMap方法來獲得一個Map<UniversityName, Map<CoursesOffered, SeatAvailable>>的表示:

@Test
    public void givenTable_whenColumnMap_returnsSuccessfully() {
        Table<String, String, Integer> universityCourseSeatTable 
            = HashBasedTable.create();
        universityCourseSeatTable.put("Mumbai", "Chemical", 120);
        universityCourseSeatTable.put("Mumbai", "IT", 60);
        universityCourseSeatTable.put("Harvard", "Electrical", 60);
        universityCourseSeatTable.put("Harvard", "IT", 120);

        Map<String, Map<String, Integer>> courseKeyUniversitySeatMap 
            = universityCourseSeatTable.columnMap();

        assertThat(courseKeyUniversitySeatMap).hasSize(3);
        assertThat(courseKeyUniversitySeatMap.get("IT")).hasSize(2);
        assertThat(courseKeyUniversitySeatMap.get("Electrical")).hasSize(1);
        assertThat(courseKeyUniversitySeatMap.get("Chemical")).hasSize(1);
    }

4.6. 列鍵到單元格值的映射

我們可以通過提供行鍵來獲取以列鍵為鍵,單元格值的 Map 映射:

@Test
    public void givenTable_whenRow_returnsSuccessfully() {
        Table <String, String, Integer> universityCourseSeatTable 
            = HashBasedTable.create();
        universityCourseSeatTable.put("Mumbai", "Chemical", 120);
        universityCourseSeatTable.put("Mumbai", "IT", 60);
        universityCourseSeatTable.put("Harvard", "Electrical", 60);
        universityCourseSeatTable.put("Harvard", "IT", 120);

        Map<String, Integer> courseSeatMap 
            = universityCourseSeatTable.row("Mumbai");

        assertThat(courseSeatMap).hasSize(2);
        assertThat(courseSeatMap.get("IT")).isEqualTo(60);
        assertThat(courseSeatMap.get("Chemical")).isEqualTo(120);
    }

4.7. 獲取不重復(fù)的行鍵

我們可以使用 rowKeySet方法從表中獲取所有的行鍵:

@Test
    public void givenTable_whenRowKeySet_returnsSuccessfully() {
        Table<String, String, Integer> universityCourseSeatTable
            = HashBasedTable.create();
        universityCourseSeatTable.put("Mumbai", "Chemical", 120);
        universityCourseSeatTable.put("Mumbai", "IT", 60);
        universityCourseSeatTable.put("Harvard", "Electrical", 60);
        universityCourseSeatTable.put("Harvard", "IT", 120);

        Set<String> universitySet = universityCourseSeatTable.rowKeySet();

        assertThat(universitySet).hasSize(2);
    }

4.8. 獲取不重復(fù)的列鍵

我們可以使用 columnKeySet 方法從表中獲取所有的列鍵:

@Test
    public void givenTable_whenColKeySet_returnsSuccessfully() {
        Table<String, String, Integer> universityCourseSeatTable
            = HashBasedTable.create();
        universityCourseSeatTable.put("Mumbai", "Chemical", 120);
        universityCourseSeatTable.put("Mumbai", "IT", 60);
        universityCourseSeatTable.put("Harvard", "Electrical", 60);
        universityCourseSeatTable.put("Harvard", "IT", 120);

        Set<String> courseSet = universityCourseSeatTable.columnKeySet();

        assertThat(courseSet).hasSize(3);
    }

5. 結(jié)論

在本教程中,我們介紹了 Guava 庫中 Table 類的方法。Table 類提供了一種集合,表示包含行、列和相關(guān)單元格值的表結(jié)構(gòu)。

以上示例的代碼可以在GitHub項目中找到,這是一個基于 Maven 的項目,因此可以輕松導(dǎo)入和運(yùn)行。


創(chuàng)作不易,如果本文對你有幫助,歡迎點贊、收藏加關(guān)注,你的支持和鼓勵,是我創(chuàng)作的最大動力。
【譯】Google Guava 的 Table 接口介紹

歡迎加入我的知識星球,知識星球ID:15165241 一起交流學(xué)習(xí)。
https://t.zsxq.com/Z3bAiea 申請時標(biāo)注來自CSDN。文章來源地址http://www.zghlxwxcb.cn/news/detail-464422.html

到了這里,關(guān)于【譯】Google Guava 的 Table 接口介紹的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

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

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 推薦Java開發(fā)常用的工具類庫google guava

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

    2024年02月09日
    瀏覽(24)
  • Google 開源庫Guava詳解(集合工具類)—Maps、Multisets、Multimaps

    Maps有許多很酷的實用程序,值得單獨解釋。 Maps.uniqueIndex(Iterable,F(xiàn)unction)解決了一個常見的情況,即有一堆對象,每個對象都有一些唯一的屬性,并希望能夠根據(jù)該屬性查找這些對象。 假設(shè)我們有一堆字符串,我們知道它們有唯一的長度,我們希望能夠查找具有特定長度

    2024年02月03日
    瀏覽(27)
  • 【開源與項目實戰(zhàn):開源實戰(zhàn)】81 | 開源實戰(zhàn)三(上):借Google Guava學(xué)習(xí)發(fā)現(xiàn)和開發(fā)通用功能模塊

    【開源與項目實戰(zhàn):開源實戰(zhàn)】81 | 開源實戰(zhàn)三(上):借Google Guava學(xué)習(xí)發(fā)現(xiàn)和開發(fā)通用功能模塊

    上幾節(jié)課,我們拿 Unix 這個超級大型開源軟件的開發(fā)作為引子,從代碼設(shè)計編寫和研發(fā)管理兩個角度,講了如何應(yīng)對大型復(fù)雜項目的開發(fā)。接下來,我們再講一下 Google 開源的 Java 開發(fā)庫 Google Guava。 Google Guava 是一個非常成功、非常受歡迎的開源項目。它在 GitHub 上由近 3.7 萬

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

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

    2024年04月22日
    瀏覽(25)
  • 【開源與項目實戰(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è)務(wù)開發(fā)中,發(fā)現(xiàn)跟業(yè)務(wù)無關(guān)、可以復(fù)用的通用功能模塊,并將它們從業(yè)務(wù)代碼中抽離出來,設(shè)計開發(fā)成獨立的類庫、框架或功能組件。 今天,我們再來學(xué)習(xí)一下,Google Guava 中用到的幾種經(jīng)典設(shè)計模式:

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

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

    2024年02月11日
    瀏覽(20)
  • com.google.common.collect 是 Google Guava 庫中的一個包,它提供了一系列擴(kuò)展和增強(qiáng) Java 集合框架的工具類和數(shù)據(jù)結(jié)構(gòu)

    com.google.common.collect 是 Google Guava 庫中的一個包,它提供了一系列擴(kuò)展和增強(qiáng) Java 集合框架的工具類和數(shù)據(jù)結(jié)構(gòu)

    com.google.common.collect 是 Google Guava 庫中的一個包,它提供了一系列擴(kuò)展和增強(qiáng) Java 集合框架的工具類和數(shù)據(jù)結(jié)構(gòu)。Guava 的集合工具在設(shè)計上強(qiáng)調(diào)性能、不可變性、功能性和易用性。 以下是 com.google.common.collect 包中一些重要的類和接口: Immutable Collections: ImmutableSet:一個不可變

    2024年03月19日
    瀏覽(37)
  • Guava Cache 介紹

    Guava 是 Google 提供的一套 Java 工具包,而 Guava Cache 是該工具包中提供的一套完善的 JVM 級別高并發(fā)緩存框架;本文主要介紹它的相關(guān)功能及基本使用,文中所使用到的軟件版本:Java 1.8.0_341、Guava 32.1.3-jre。 緩存在很多情況下非常有用。例如,當(dāng)某個值的計算或檢索代價很高,

    2024年02月05日
    瀏覽(20)
  • Guava Cache介紹-面試用

    Guava Cache介紹-面試用

    Guava Cache是本地緩存,數(shù)據(jù)讀寫都在一個進(jìn)程內(nèi),相對于分布式緩存redis,不需要網(wǎng)絡(luò)傳輸?shù)倪^程,訪問速度很快,同時也受到 JVM 內(nèi)存的制約,無法在數(shù)據(jù)量較多的場景下使用。 基于以上特點,本地緩存的主要應(yīng)用場景為以下幾種: 對于訪問速度有較大要求 存儲的數(shù)據(jù)不經(jīng)

    2024年02月07日
    瀏覽(22)
  • https://chrome.google.com/webstore/category/extensions 無法訪問

    \\\"https://chrome.google.com/webstore/category/extensions\\\" 網(wǎng)站無法訪問可能是由于以下幾種原因之一: 網(wǎng)絡(luò)連接問題:檢查您的網(wǎng)絡(luò)連接是否正常,確保您能夠訪問其他網(wǎng)站。 網(wǎng)站維護(hù):該網(wǎng)站可能正在維護(hù)中,請稍后再試。 瀏覽器問題:請確保您使用的是最新版本的Chrome瀏覽器,并且沒

    2024年02月11日
    瀏覽(18)

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

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

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

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

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包