Java Map Add addToListMap(Map> map, TKey key, TValue value)

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

Description

Add the given value to the list of the corresponding key.

License

Apache License

Parameter

Parameter Description
map The map containing the lists
key The key to be used
value The value to be put into the list of the corresponding key

Declaration

public static <TKey, TValue> void addToListMap(Map<TKey, List<TValue>> map, TKey key, TValue value) 

Method Source Code


//package com.java2s;
/*/*from  w  ww .j a v  a  2s .c o m*/
 * This file is part of the TSPHP project published under the Apache License 2.0
 * For the full copyright and license information, please have a look at LICENSE in the
 * root folder or visit the project's website http://tsphp.ch/wiki/display/TSPHP/License
 */

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

public class Main {
    /**
     * Add the given value to the list of the corresponding key.
     * <p/>
     * If there is not yet a list for the corresponding key then a new list for the key is created as well.
     *
     * @param map   The map containing the lists
     * @param key   The key to be used
     * @param value The value to be put into the list of the corresponding key
     */
    public static <TKey, TValue> void addToListMap(Map<TKey, List<TValue>> map, TKey key, TValue value) {
        if (map.containsKey(key)) {
            map.get(key).add(value);
        } else {
            List<TValue> list = new ArrayList<>();
            list.add(value);
            map.put(key, list);
        }
    }
}

Related

  1. addToListMap(Map map, Object key, Object value)
  2. addToListMap(Map> map, K key, V value)
  3. addToListMap(Map> map, String key, String value)
  4. addToListMap(Map> map, T key, T value)
  5. addToListMap(Map> map, T key, V val)
  6. addToMap(Map map, int initialKey, T value)
  7. addToMap(Map data, K key, T value, Class clz)
  8. addToMap(Map map, K key, V value)
  9. addToMap(Map map, K key, V value)