Java Map Add addToListMap(Map> map, K key, V value)

Here you can find the source of addToListMap(Map> map, K key, V value)

Description

Populated a (key,value) pair in a map of lists.

License

Open Source License

Parameter

Parameter Description
map the map of lists
key the list key in the map
value the value to be added to the list at key

Return

a of volume/lun

Declaration

public static <K extends Object, V extends Object> boolean addToListMap(Map<K, List<V>> map, K key, V value) 

Method Source Code


//package com.java2s;

import java.util.ArrayList;

import java.util.List;
import java.util.Map;

public class Main {
    /**/*from www.  j a  va  2s  . c o  m*/
     * Populated a (key,value) pair in a map of lists. It finds the list at key and
     * adds value to the list. If there is no list at key, create a new list and add
     * it to the map.
     * 
     * @param map the map of lists
     * @param key the list key in the map
     * @param value the value to be added to the list at key
     * @return a {@link StringMap} of volume/lun
     */
    public static <K extends Object, V extends Object> boolean addToListMap(Map<K, List<V>> map, K key, V value) {
        if (map == null) {
            return false;
        }
        List<V> list = map.get(key);
        if (list == null) {
            list = new ArrayList<V>();
            map.put(key, list);
        }
        if (!list.contains(value)) {
            list.add(value);
            return true;
        }
        return false;
    }
}

Related

  1. addTo(Map> map, K key, T value)
  2. addTo(Map map, N key, E element, Class collectionClass)
  3. addToAnd(T target, Map... items)
  4. addToList(Map> map, X key, Y value)
  5. addToListMap(Map map, Object key, Object value)
  6. addToListMap(Map> map, String key, String value)
  7. addToListMap(Map> map, T key, T value)
  8. addToListMap(Map> map, T key, V val)
  9. addToListMap(Map> map, TKey key, TValue value)