使用場景java
在平常的開發工做中,咱們有的時候須要構造像Map<K, List<V>>或者Map<K, Set<V>>這樣比較複雜的集合類型的數據結構,以便作相應的業務邏輯處理數據結構
說明:想 Map<String, List<StudentScore>> StudentScoreMap = new HashMap<String, List<StudentScore>>()這樣的數據結構,本身實現起來太麻煩,你須要檢查key是否存在,不存在時則建立一個,存在時在List後面添加上一個。這個過程是比較痛苦的,若是你但願檢查List中的對象是否存在,刪除一個對象,或者遍歷整個數據結構,那麼則須要更多的代碼來實現。google
import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.Multimap; import com.google.common.collect.Multiset; import org.junit.Test; import java.util.*; /** * @author Kevin * @description * @date 2016/8/5 */ public class MultimapTest { Map<String, List<StudentScore>> studentScoreMap = new HashMap<String, List<StudentScore>>(); // 普通實現 @Test public void testStudentScore() { for (int i = 0; i < 10; i++) { StudentScore studentScore = new StudentScore(); studentScore.courseId = i; studentScore.score = 100 - i; addStudentScore("Han", studentScore); } System.out.println("studentScoreMap size = " + studentScoreMap.size()); System.out.println("studentScoreMap contains Han? " + studentScoreMap.containsKey("Han")); } private void addStudentScore(String name, StudentScore studentScore) { List<StudentScore> list = studentScoreMap.get(name); if (list == null) { list = new ArrayList<StudentScore>(); studentScoreMap.put(name, list); } list.add(studentScore); } // guava實現 @Test public void testMultimap() { Multimap<String, StudentScore> studentScoreMap = ArrayListMultimap.create(); for (int i = 0; i < 10; i++) { StudentScore studentScore = new StudentScore(); studentScore.courseId = i; studentScore.score = 100 - i; studentScoreMap.put("Han", studentScore); } // size = 10 System.out.println("studentScoreMap size = " + studentScoreMap.size()); // true System.out.println("studentScoreMap contains Han? " + studentScoreMap.containsKey("Han")); // ==================================================== Collection<StudentScore> studentScores = studentScoreMap.get("Han"); StudentScore studentScore = new StudentScore(); studentScore.courseId = 100; studentScore.score = 100; studentScores.add(studentScore); // size = 11 System.out.println("studentScoreMap size = " + studentScoreMap.size()); // 清空數據同時,對應的key也同時刪除 studentScores.clear(); // size = 0 System.out.println("studentScoreMap size = " + studentScoreMap.size()); // false System.out.println("studentScoreMap contains Han? " + studentScoreMap.containsKey("Han")); studentScores.add(studentScore); Map<String, Collection<StudentScore>> asMap = studentScoreMap.asMap(); asMap.remove("Han"); // asMap unsupport put and putAll System.out.println("studentScoreMap contains Han? " + studentScoreMap.containsKey("Han")); Collection<Map.Entry<String, StudentScore>> entries = studentScoreMap.entries(); // entries unsupport add and addAll Set<String> keySet = studentScoreMap.keySet(); // keySet unsupport add and addAll Multiset<String> keys = studentScoreMap.keys(); // keys unsupport add and addAll Collection<StudentScore> values = studentScoreMap.values(); // values unsupport add and addAll } } class StudentScore { int courseId; int score; }
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。code
儘管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()。對象
Multimap的實現ip
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)rem