1. 程式人生 > >Guava之ArrayListMultimap

Guava之ArrayListMultimap

0.class

  ArrayListMultimap

1.All Implemented Interfaces

  ListMultimap

2.簡介 

  Implementation of Multimap that uses an ArrayList to store the values for a given key. A HashMap associates each key with an ArrayList of values.When iterating through the collections supplied by this class, the ordering of values for a given key agrees with the order in which the values were added.This multimap allows duplicate key-value pairs. After adding a new key-value pair equal to an existing key-value pair, the ArrayListMultimap will contain entries for both the new value and the old value.Keys and values may be null. All optional multimap methods are supported, and all returned views are modifiable.The lists returned by get(K), removeAll(java.lang.Object), and replaceValues(K, java.lang.Iterable
  簡言之: ArrayListMultimap 底層可以認為是 Map<K, Collection<V>>

,且key可以重複.

3.常用 API

  boolean put(K key, V value) //說明: Stores a key-value pair in the multimap.

  List<V> get(K key)//說明: Returns a view collection of the values associated with key in this multimap, if any.

  Map<K,Collection<V>> asMap()//說明: Returns a view of this multimap as a Map from each distinct key to the nonempty collection of that key’s associated values.

  Collection<Map.Entry<K,V>> entries()//說明: Returns a view collection of all key-value pairs contained in this multimap, as Map.Entry instances.

  boolean containsEntry(Object key, Object value)//說明: Returns true if this multimap contains at least one key-value pair with the key key and the value value.

  boolean containsKey(Object key)//說明: Returns true if this multimap contains at least one key-value pair with the key key.

  boolean containsValue(Object value)//說明: Returns true if this multimap contains at least one key-value pair with the value value.

  int size()//說明: Returns the number of key-value pairs in this multimap.

  Multiset<K> keys()//說明: Returns a view collection containing the key from each key-value pair in this multimap, without collapsing duplicates.

  Collection<V> values()//說明: Returns a view collection containing the value from each key-value pair contained in this multimap, without collapsing duplicates (so values().size() == size()). 

4.示例

 

//傳統的場景:  Map<String,List<MyClass>> map = new HashMap<String,List<MyClass>>();   

//缺點:向map裡面新增元素不太方便,需要這樣實現

    void putMyObject(String key, Object value) {
        List<Object> myClassList = myClassListMap.get(key);
        if(myClassList == null) {
            myClassList = new ArrayList<object>();
            myClassListMap.put(key,myClassList);
        }
        myClassList.add(value);
    }

//上面傳統的場景,可以使用ArrayListMultimap  
  Multimap<String, String> multimap = ArrayListMultimap.create();
  multimap.put("fruit", "bannana");
  multimap.put("fruit", "apple");//key可以重複
  multimap.put("fruit", "apple");//value可以重複,不會覆蓋之前的
  multimap.put("fruit", "peach");
  multimap.put("fish","crucian");//歐洲鯽魚
  multimap.put("fish","carp");//鯉魚

  System.err.println(multimap.size());//6

  Collection<String> fruits = multimap.get("fruit");
  System.err.println(fruits);//[bannana, apple, apple, peach]

  for (String s : multimap.values()) {
      System.err.print(s + " , ");//bannana , apple , apple , peach , crucian , carp ,
  }

  multimap.remove("fruit","apple");
  System.err.println(fruits);//[bannana, apple, peach]   注意:這裡只remove了一個apple,因此還有一個apple

  multimap.removeAll("fruit");
  System.err.println(fruits);//[]
//get(key) 返回的是collection,如果希望返回的是list,可以選擇ListMultimap來接收create()的返回值

    ListMultimap<String, String> listMultimap = ArrayListMultimap.create();
    listMultimap.put("fruit", "bannana");
    listMultimap.put("fruit", "apple");
    listMultimap.put("fruit", "peach");
    listMultimap.put("fish","crucian");//歐洲鯽魚
    listMultimap.put("fish","carp");//鯉魚
    List<String> fruits = listMultimap.get("fruit");
    System.err.println(fruits);//[bannana, apple, peach]
    
    
    
//對比 HashMultimap

    Multimap<String,String> multimap= HashMultimap.create();
    multimap.put("fruit", "bannana");
    multimap.put("fruit", "apple");
    multimap.put("fruit", "apple");
    System.err.println(multimap.size());//2
    System.err.println(multimap.get("fruit"));//[apple, bannana]     注意: 這裡只有一個apple