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

猜你喜欢

转载自www.cnblogs.com/wangliangwu/p/10020420.html