1. 程式人生 > >Guava學習筆記:Guava新增集合型別-Multimap

Guava學習筆記:Guava新增集合型別-Multimap

  在日常的開發工作中,我們有的時候需要構造像Map<K, List<V>>或者Map<K, Set<V>>這樣比較複雜的集合型別的資料結構,以便做相應的業務邏輯處理。例如:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.Test;

public class MultimapTest {

    Map<String, List<StudentScore>> StudentScoreMap = new
HashMap<String, List<StudentScore>>(); @Test public void testStudentScore(){ for(int i=10;i<20;i++){ StudentScore studentScore=new StudentScore(); studentScore.CourseId=1001+i; studentScore.score=100-i; addStudentScore(
"peida",studentScore); } System.out.println("StudentScoreMap:"+StudentScoreMap.size()); System.out.println("StudentScoreMap:"+StudentScoreMap.containsKey("peida")); System.out.println("StudentScoreMap:"+StudentScoreMap.containsKey("jerry")); System.out.println(
"StudentScoreMap:"+StudentScoreMap.size()); System.out.println("StudentScoreMap:"+StudentScoreMap.get("peida").size()); List<StudentScore> StudentScoreList=StudentScoreMap.get("peida"); if(StudentScoreList!=null&&StudentScoreList.size()>0){ for(StudentScore stuScore:StudentScoreList){ System.out.println("stuScore one:"+stuScore.CourseId+" score:"+stuScore.score); } } } public void addStudentScore(final String stuName,final StudentScore studentScore) { List<StudentScore> stuScore = StudentScoreMap.get(stuName); if (stuScore == null) { stuScore = new ArrayList<StudentScore>(); StudentScoreMap.put(stuName, stuScore); } stuScore.add(studentScore); } } class StudentScore{ int CourseId; int score; }

  說明:想 Map<String, List<StudentScore>> StudentScoreMap = new HashMap<String, List<StudentScore>>()這樣的資料結構,自己實現起來太麻煩,你需要檢查key是否存在,不存在時則建立一個,存在時在List後面新增上一個。這個過程是比較痛苦的,如果你希望檢查List中的物件是否存在,刪除一個物件,或者遍歷整個資料結構,那麼則需要更多的程式碼來實現。

  Multimap

  Guava的Multimap就提供了一個方便地把一個鍵對應到多個值的資料結構。讓我們可以簡單優雅的實現上面複雜的資料結構,讓我們的精力和時間放在實現業務邏輯上,而不是在資料結構上,下面我們具體來看看Multimap的相關知識點。

  上面的程式碼和資料結構用Multimap來實現,程式碼結構清晰簡單了很多吧,具體程式碼如下:

  @Test
    public void teststuScoreMultimap(){
        Multimap<String,StudentScore> scoreMultimap = ArrayListMultimap.create(); 
        for(int i=10;i<20;i++){
            StudentScore studentScore=new StudentScore();
            studentScore.CourseId=1001+i;
            studentScore.score=100-i;
            scoreMultimap.put("peida",studentScore);
        }
        System.out.println("scoreMultimap:"+scoreMultimap.size());
        System.out.println("scoreMultimap:"+scoreMultimap.keys());
    }

  呼叫Multimap.get(key)會返回這個鍵對應的值的集合的檢視(view),沒有對應集合就返回空集合。對於ListMultimap來說,這個方法會返回一個List,對於SetMultimap來說,這個方法就返回一個Set。修改資料是通過修改底層Multimap來實現的。例如:

@Test
    public void teststuScoreMultimap(){
        Multimap<String,StudentScore> scoreMultimap = ArrayListMultimap.create(); 
        for(int i=10;i<20;i++){
            StudentScore studentScore=new StudentScore();
            studentScore.CourseId=1001+i;
            studentScore.score=100-i;
            scoreMultimap.put("peida",studentScore);
        }
        System.out.println("scoreMultimap:"+scoreMultimap.size());
        System.out.println("scoreMultimap:"+scoreMultimap.keys());
        
        Collection<StudentScore> studentScore = scoreMultimap.get("peida");
        studentScore.clear();
        StudentScore studentScoreNew=new StudentScore();
        studentScoreNew.CourseId=1034;
        studentScoreNew.score=67;
        studentScore.add(studentScoreNew);
        
        System.out.println("scoreMultimap:"+scoreMultimap.size());
        System.out.println("scoreMultimap:"+scoreMultimap.keys());
    }

    Multimap也支援一系列強大的檢視功能:
  1.asMap把自身Multimap<K, V>對映成Map<K, Collection<V>>檢視。這個Map檢視支援remove和修改操作,但是不支援put和putAll。嚴格地來講,當你希望傳入引數是不存在的key,而且你希望返回的是null而不是一個空的可修改的集合的時候就可以呼叫asMap().get(key)。(你可以強制轉型asMap().get(key)的結果型別-對SetMultimap的結果轉成Set,對ListMultimap的結果轉成List型-但是直接把ListMultimap轉成Map<K, List<V>>是不行的。)
  2.entries檢視是把Multimap裡所有的鍵值對以Collection<Map.Entry<K, V>>的形式展現。
  3.keySet檢視是把Multimap的鍵集合作為檢視
  4.keys檢視返回的是個Multiset,這個Multiset是以不重複的鍵對應的個數作為檢視。這個Multiset可以通過支援移除操作而不是新增操作來修改Multimap。
  5.values()檢視能把Multimap裡的所有值“平展”成一個Collection<V>。這個操作和Iterables.concat(multimap.asMap().values())很相似,只是它返回的是一個完整的Collection。

  儘管Multimap的實現用到了Map,但Multimap<K, V>不是Map<K, Collection<V>>。因為兩者有明顯區別:
  1.Multimap.get(key)一定返回一個非null的集合。但這不表示Multimap使用了記憶體來關聯這些鍵,相反,返回的集合只是個允許新增元素的檢視。
  2.如果你喜歡像Map那樣當不存在鍵的時候要返回null,而不是Multimap那樣返回空集合的話,可以用asMap()返回的檢視來得到Map<K, Collection<V>>。(這種情況下,你得把返回的Collection<V>強轉型為List或Set)。
  3.Multimap.containsKey(key)只有在這個鍵存在的時候才返回true。
  4.Multimap.entries()返回的是Multimap所有的鍵值對。但是如果需要key-collection的鍵值對,那就得用asMap().entries()。
  5.Multimap.size()返回的是entries的數量,而不是不重複鍵的數量。如果要得到不重複鍵的數目就得用Multimap.keySet().size()。

  @Test
    public void teststuScoreMultimap(){
        Multimap<String,StudentScore> scoreMultimap = ArrayListMultimap.create(); 
        for(int i=10;i<20;i++){
            StudentScore studentScore=new StudentScore();
            studentScore.CourseId=1001+i;
            studentScore.score=100-i;
            scoreMultimap.put("peida",studentScore);
        }
        System.out.println("scoreMultimap:"+scoreMultimap.size());
        System.out.println("scoreMultimap:"+scoreMultimap.keys());
        
        Collection<StudentScore> studentScore = scoreMultimap.get("peida");
        StudentScore studentScore1=new StudentScore();
        studentScore1.CourseId=1034;
        studentScore1.score=67;
        studentScore.add(studentScore1);
        
        StudentScore studentScore2=new StudentScore();
        studentScore2.CourseId=1045;
        studentScore2.score=56;
        scoreMultimap.put("jerry",studentScore2);
        
        System.out.println("scoreMultimap:"+scoreMultimap.size());
        System.out.println("scoreMultimap:"+scoreMultimap.keys());
        
        
        for(StudentScore stuScore : scoreMultimap.values()) {
            System.out.println("stuScore one:"+stuScore.CourseId+" score:"+stuScore.score);
        }
        
        scoreMultimap.remove("jerry",studentScore2); 
        System.out.println("scoreMultimap:"+scoreMultimap.size());
        System.out.println("scoreMultimap:"+scoreMultimap.get("jerry"));
        
        scoreMultimap.put("harry",studentScore2);
        scoreMultimap.removeAll("harry");
        System.out.println("scoreMultimap:"+scoreMultimap.size());
        System.out.println("scoreMultimap:"+scoreMultimap.get("harry"));
    }

  Multimap的實現

Multimap提供了豐富的實現,所以你可以用它來替代程式裡的Map<K, Collection<V>>,具體的實現如下:
  Implementation            Keys 的行為類似          Values的行為類似
  ArrayListMultimap         HashMap                     ArrayList
  HashMultimap               HashMap                     HashSet
  LinkedListMultimap        LinkedHashMap*              LinkedList*
  LinkedHashMultimap      LinkedHashMap                LinkedHashSet
  TreeMultimap                TreeMap                          TreeSet
  ImmutableListMultimap  ImmutableMap                 ImmutableList
  ImmutableSetMultimap  ImmutableMap                 ImmutableSet

  
  以上這些實現,除了immutable的實現都支援null的鍵和值。
  1.LinkedListMultimap.entries()能維持迭代時的順序。

  2.LinkedHashMultimap維持插入的順序,以及鍵的插入順序。
  要注意並不是所有的實現都正真實現了Map<K, Collection<V>>!(尤其是有些Multimap的實現為了最小話開銷,使用了自定義的hash table)

相關推薦

Guava學習筆記Guava新增集合型別-Multiset

  Guava引進了JDK裡沒有的,但是非常有用的一些新的集合型別。所有這些新集合型別都能和JDK裡的集合平滑整合。Guava集合非常精準地實現了JDK定義的介面。Guava中定義的新集合有:  Multiset  SortedMultiset  Multimap  ListMultimap  SetMult

Guava學習筆記Guava新增集合型別-Multimap

  在日常的開發工作中,我們有的時候需要構造像Map<K, List<V>>或者Map<K, Set<V>>這樣比較複雜的集合型別的資料結構,以便做相應的業務邏輯處理。例如: import java.util.ArrayList; import java

Guava學習筆記Guava新增集合型別-Bimap

  BiMap提供了一種新的集合型別,它提供了key和value的雙向關聯的資料結構。  通常情況下,我們在使用Java的Map時,往往是通過key來查詢value的,但是如果出現下面一種場景的情況,我們就需要額外編寫一些程式碼了。首先來看下面一種表示標識序號和檔名的map結構。 @Test

Guava學習筆記Guava集合-Table等

  Table  當我們需要多個索引的資料結構的時候,通常情況下,我們只能用這種醜陋的Map<FirstName, Map<LastName, Person>>來實現。為此Guava提供了一個新的集合型別-Table集合型別,來支援這種資料結構的使用場景。Table支援“row”和“

Guava學習筆記Guava cache

  快取,在我們日常開發中是必不可少的一種解決效能問題的方法。簡單的說,cache 就是為了提升系統性能而開闢的一塊記憶體空間。   快取的主要作用是暫時在記憶體中儲存業務系統的資料處理結果,並且等待下次訪問使用。在日常開發的很多場合,由於受限於硬碟IO的效能或者我們自

Guava學習筆記Immutable(不可變)集合

  不可變集合,顧名思義就是說集合是不可被修改的。集合的資料項是在建立的時候提供,並且在整個生命週期中都不可改變。   為什麼要用immutable物件?immutable物件有以下的優點:     1.對不可靠的客戶程式碼庫來說,它使用安全,可以在未受信任的類庫中安全的使用這些物件    2.執行緒安全

Guava學習筆記Optional優雅的使用null

asset 不包含 你在 rgs 命名 靜態 不清晰 ces throw 在我們學習和使用Guava的Optional之前,我們需要來了解一下Java中null。因為,只有我們深入的了解了null的相關知識,我們才能更加深入體會領悟到Guava的Optional設計和使用上

Guava學習筆記Preconditions優雅的檢驗引數

在日常開發中,我們經常會對方法的輸入引數做一些資料格式上的驗證,以便保證方法能夠按照正常流程執行下去。對於可預知的一些資料上的錯誤,我們一定要做事前檢測和判斷,來避免程式流程出錯,而不是完全通過錯誤處理來保證流程正確執行,畢竟錯誤處理是比較消耗資源的方式。在平常情況下我們對引數的判斷都需要自己來逐個寫方法

Guava學習筆記EventBus

  EventBus是Guava的事件處理機制,是設計模式中的觀察者模式(生產/消費者程式設計模型)的優雅實現。對於事件監聽和釋出訂閱模式,EventBus是一個非常優雅和簡單解決方案,我們不用建立複雜的類和介面層次結構。   Observer模式是比較常用的設計模式之一,雖然有時候在具體程式碼裡,它不一定

Guava學習筆記複寫的Object常用方法

  在Java中Object類是所有類的父類,其中有幾個需要override的方法比如equals,hashCode和toString等方法。每次寫這幾個方法都要做很多重複性的判斷, 很多類庫提供了覆寫這幾個方法的工具類, Guava也提供了類似的方式。下面我們來看看Guava中這幾個方法簡單使用。   e

Guava學習筆記簡化異常處理的Throwables類

  有時候, 當我們我們捕獲異常, 並且像把這個異常傳遞到下一個try/catch塊中。Guava提供了一個異常處理工具類, 可以簡單地捕獲和重新丟擲多個異常。例如: import java.io.IOException; import org.junit.Test; import com.goog

Guava學習筆記Range

  在Guava中新增了一個新的型別Range,從名字就可以瞭解到,這個是和區間有關的資料結構。從Google官方文件可以得到定義:Range定義了連續跨度的範圍邊界,這個連續跨度是一個可以比較的型別(Comparable type)。比如1到100之間的整型資料。   在數學裡面的範圍是有邊界和無邊界之分

Guava學習筆記Ordering犀利的比較器

  Ordering是Guava類庫提供的一個犀利強大的比較器工具,Guava的Ordering和JDK Comparator相比功能更強。它非常容易擴充套件,可以輕鬆構造複雜的comparator,然後用在容器的比較、排序等操作中。   本質上來說,Ordering 例項無非就是一個特殊的Comparat

Go語言學習筆記(三) 集合型別

陣列 陣列中包含的每個資料被稱為陣列元素(element),一個數組包含的元素個數被稱為陣列的長度。 [32]byte // 長度為32的陣列,每個元素為一個位元組 [2*N] struct { x, y int32 } // 複雜型別陣列 [1000]*float64 // 指標陣列 [3

ElasticSearch 6.x 學習筆記12.欄位型別

12.1 欄位型別概述 一級分類 二級分類 具體型別 核心型別 字串型別 string,text,keyword 整數型別 integer,long,

go學習筆記2.變數、型別、常量

基本型別 1.基本型別列表 複製程式碼程式碼如下: 型別        長度     說明 bool         1      true/false,預設false, 不能把非0值當做true(不用數字代表true/false) byte        

Guava學習筆記(二)基礎(Joiner,Objects,Splitter及Strings)

nonnull obj expect null dto 字符 情況 core cte 添加Maven依賴 JoinerTest import com.google.common.base.Joiner; import org.junit.Assert; import org

Guava學習筆記(二)Google Guava (瓜娃)的API快速熟悉使用

1,大綱 讓我們來熟悉瓜娃,並體驗下它的一些API,分成如下幾個部分: IntroductionGuava Collection APIGuava Basic UtilitiesIO APICache API 2,為神馬選擇瓜娃? 瓜娃是java API蛋糕上的冰

Guava新增集合型別的例子

Multimap ArrayListMultimap實現 Multimap也稱為Multihash,它是Map的變體,它允許用相同的鍵關聯多個值或者物件。顧名思義,Multimap基於儲存鍵值對的Map介面。它允許重複的鍵值對存在。例如,Amaury V

Guava學習筆記目錄

筆記 htm 目錄 join cnblogs string com log href 源碼 Guava學習筆記(一):Maven Guava學習筆記(二):基礎(Joiner,Objects,Splitter及Strings) Guava學習筆記(三):集合 Guava學