Java Map Add addToListMap(Map map, Object key, Object value)

Here you can find the source of addToListMap(Map map, Object key, Object value)

Description

Add a value to a Map from keys to Set of values, creating the value list as needed.

License

GNU General Public License

Parameter

Parameter Description
map the Map
key the key
value the value

Declaration

public static void addToListMap(Map map, Object key, Object value) 

Method Source Code

//package com.java2s;
/*//from  ww  w. ja  v a 2s  .  co  m
 * Copyright (C) 2002-2014 FlyMine
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  See the LICENSE file for more
 * information or http://www.gnu.org/copyleft/lesser.html.
 *
 */

import java.util.ArrayList;

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

public class Main {
    /**
     * Add a value to a Map from keys to Set of values, creating the value list
     * as needed.
     *
     * @param map the Map
     * @param key the key
     * @param value the value
     */
    public static void addToListMap(Map map, Object key, Object value) {
        if (map == null) {
            throw new IllegalArgumentException("invalid map");
        }
        if (key == null) {
            throw new IllegalArgumentException("invalid map key");
        }
        List valuesList = (List) map.get(key);
        if (valuesList == null) {
            valuesList = new ArrayList();
            map.put(key, valuesList);
        }
        valuesList.add(value);
    }
}

Related

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